我是靠谱客的博主 追寻糖豆,最近开发中收集的这篇文章主要介绍memory java heap_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内存查看:

eb9264d3b8c476d921787b18ec2d6504.png

Heap内存分配

885ac97db22cd7c685f73b4a888f7c04.png

实际java进程内存占用:

e6e1b151784546cff995d5067e1bb1d5.png

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

四.测试结论:

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

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

95becaec836f344d7dc60d6887a6cddb.png

大小: 21 KB

e932c25244fc3e3c8a1681d40f173e37.png

大小: 40.2 KB

082ba687f7d08d5efad6b98045d93b57.png

大小: 10.2 KB

1

1

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-04-26 13:09

浏览 14352

评论

最后

以上就是追寻糖豆为你收集整理的memory java heap_java使用java heap外(堆外)内存导致的内存泄露的全部内容,希望文章能够帮你解决memory java heap_java使用java heap外(堆外)内存导致的内存泄露所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部