java使用java heap外(堆外)内存导致的内存泄露

java可以通过 java.nio.ByteBuffer.allocateDirect(capacity) 直接使用non java heap(java堆外)的内存 。

 

一.使用目的:

1。开辟数据缓冲区

2。可以突破jvm内存限制,操作更多的物理内存(不同的jvm实现不一样,sun jvm会被限制,但能增加jvm能够操纵的一倍内存,而jrockit基本上可以将物理内存全部耗光)

 

二.使用问题:

通过ByteBuffer.allocateDirect()使用的内存不能够通过jvm相关内存工具:VisualVM等查看heap的内存占用,所以内存泄露也较难查找。

 

三. 测试如下:

 

 

public class ByteBufferTest {
	
	public static void main(String[] args) throws InterruptedException {
		System.out.println(SystemRuntimeInfo.getMemoryInfo());
		List list = new ArrayList();
		for(int i = 0; i < 100; i++) {
			list.add(ByteBuffer.allocateDirect(Integer.MAX_VALUE / 100));
			System.out.println(i + " " + SystemRuntimeInfo.getMemoryInfo()); // 自己写的内存占用工具类
		}
		System.out.println("start sleep,list.size():"+list.size());
		Thread.sleep(100000);
		System.out.println(list.size());
		
		// java.nio.ByteBuffer.allocate(capacity) 使用 java heap(java堆)分配内存: 可以直接通过工具查看出内存占用
		// java.nio.ByteBuffer.allocateDirect(capacity) 使用non java heap(非java堆)通过操作系统直接分配内存: 会导致查看不出java heap的内存占用,导致泄露看不出来
		
	}
	
}

 

打印结果:

 

 

----------------------
  totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1285MB usedPhysicalMemory:762MB 
0 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1264MB usedPhysicalMemory:783MB 
1 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1244MB usedPhysicalMemory:803MB 
2 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1223MB usedPhysicalMemory:824MB 
3 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1203MB usedPhysicalMemory:844MB 
4 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1182MB usedPhysicalMemory:865MB 
5 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1162MB usedPhysicalMemory:885MB 
6 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1141MB usedPhysicalMemory:906MB 
7 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1121MB usedPhysicalMemory:926MB 
8 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1100MB usedPhysicalMemory:947MB 
9 totalHeapMemory:15MB  freeHeapMemory:14MB  maxHeapMemory:247MB totalPhysicalMemorySize:2047MB freePhysicalMemorySize:1079MB usedPhysicalMemory:968MB 
 

 

VisualVM内存查看:

java使用java heap外(堆外)内存导致的内存泄露_第1张图片

Heap内存分配

java使用java heap外(堆外)内存导致的内存泄露_第2张图片

 

 

实际java进程内存占用:

 

 

通过VisualVM可以发现,java heap根本没有使用,但java进程的物理内存已经使用400MB+

 

四.测试结论:

 

1. 使用ByteBuffer.allocateDirect()分配出去的内存无法通过Heap查看

2. ByteBuffer.allocateDirect()分配的内容可以被GC回收,但泄露的话较难查找

 

 

 

你可能感兴趣的:(java)