标签:磁盘 nal cat ISE mamicode ble 最大值 内存回收 ret
import java.nio.ByteBuffer;
import java.util.Scanner;
/**
* IO NIO (New IO / Non-Blocking IO)
* byte[] / char[] Buffer
* Stream Channel
* <p>
* 查看直接内存的占用与释放
*
* @author kiser
*/
public class BufferTest {
/**
* 1GB
*/
private static final int BUFFER = 1024 * 1024 * 1024;
public static void main(String[] args) {
ByteBuffer byteBuffer = ByteBuffer.allocate(BUFFER);
System.out.println("直接内存分配完毕,请求指示!");
Scanner scanner = new Scanner(System.in);
scanner.next();
System.out.println("直接内存开始释放!");
byteBuffer = null;
System.gc();
scanner.next();
}
}
直接占用了 1G 的本地内存
释放后,Java程序的内存占用明显减少
原来采用BIO的架构,在读写本地文件时,我们需要从用户态切换成内核态
读写文件,需要与磁盘交互,需要由用户态切换到内核态。在内核态时,需要内存如下图的操作。
使用IO见下图,这里需要两份内存存储重复数据,效率低。
NIO 直接操作物理磁盘,省去了中间商赚差价
分别使用 BIO 和 NIO 复制大文件,看看用时差别
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author kiser
*
*/
public class BufferTest1 {
private static final int _100Mb = 1024 * 1024 * 100;
private static final String TO = "D:\\Downloads\\极品爆乳女神『香草少女M』5月新作天台上的情欲优等生.mp4";
@SuppressWarnings("resource")
private static long directBuffer(String src, String dest) {
long start = System.currentTimeMillis();
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(dest).getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_100Mb);
while (inChannel.read(byteBuffer) != -1) {
// 修改为读数据模式
byteBuffer.flip();
outChannel.write(byteBuffer);
// 清空
byteBuffer.clear();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// TODO: handle finally clause
if (inChannel != null) {
try {
inChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (outChannel != null) {
try {
outChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
return end - start;
}
private static long io(String src, String dest) {
long start = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] buffer = new byte[_100Mb];
while (true) {
int len = fis.read(buffer);
if (len == -1) {
break;
}
fos.write(buffer, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// TODO: handle finally clause
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
long sum = 0;
String src = "D:\\Downloads\\极品爆乳女神『香草少女M』5月新作天台上的情欲优等生.mp4";
for (int i = 0; i < 3; i++) {
String dest = "D:\\Downloads\\极品爆乳女神『香草少女M』5月新作天台上的情欲优等生_" + i + ".mp4";
// 6915
sum += io(src, dest);
// 5287
// sum += directBuffer(src, dest);
}
System.out.println("总花费的时间为:" + sum);
}
}
OutOfMemoryError
异常-Xmx
指定的最大堆大小,但是系统内存是有限的,Java堆和直接内存的总和依然受限于操作系统能给出的最大内存。MaxDirectMemorySize
设置-Xmx
参数值一致import java.nio.ByteBuffer;
import java.util.ArrayList;
/**
* 本地内存的OOM: OutOfMemoryError: Direct buffer memory
*
* @author kiser
*/
public class BufferTest2 {
/**
* 20MB
*/
private static final int BUFFER = 1024 * 1024 * 20;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<ByteBuffer> list = new ArrayList<>();
int count = 0;
try {
while (true) {
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER);
list.add(byteBuffer);
count++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} finally {
// TODO: handle finally clause
System.out.println(count);
}
}
}
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class MaxDirectMemorySizeTest {
/**
* 1MB
*/
private static final long _1MB = 1024 * 1024;
/**
* @param args
*/
public static void main(String[] args) throws IllegalAccessException {
// TODO Auto-generated method stub
Field unsafeField = Unsafe.class.getDeclaredFields()[0];
unsafeField.setAccessible(true);
Unsafe unsafe = (Unsafe)unsafeField.get(null);
while(true){
unsafe.allocateMemory(_1MB);
}
}
}
标签:磁盘 nal cat ISE mamicode ble 最大值 内存回收 ret
原文地址:https://www.cnblogs.com/iamfatotaku/p/14407871.html