Java 垃圾过多问题
我有一个应用程序,基本上,创建一个新的字节数组(小于1K),在几秒钟(通常不到1分钟,但有些数据存储长达1小时)后存储一些数据写入磁盘,数据将变成垃圾。每秒创建大约 400 个数据包。我读过一些文章,说不要担心 GC,特别是快速创建和释放内存部分(在 Java 6 上)。 GC 运行时间太长会导致我的应用程序出现一些问题。 我设置了一些GC参数(更大的XMX和ParalelGC),这减少了Full GC时间,但还不够。我有2个想法, 我是关注GC参数还是创建字节数组内存池机制?哪一个更好?
I have an application, basically, create a new byte array (less than 1K) store some data after few seconds (generally less than 1 minute, but some data stored up to 1 hour) write to disk and data will goes to garbage. Approximatelly 400 packets per second created. I read some articles that say don't worry about GC especially quickly created and released memory parts (on Java 6).
GC runs too long cause some problem about on my application.
I set some GC parameters(Bigger XMX and ParalelGC),this decrease Full GC time decrease but not enough yet. I have 2 idea,
Am I focus GC parameters or create Byte array memory pool mechanism? Which one is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
执行 GC 的频率取决于对象大小,但成本(清理时间)更取决于对象的数量。我怀疑长期存在的数组正在空间之间复制,直到它最终进入旧空间并最终被丢弃。清洁旧一代相对昂贵。
我建议你尝试使用ByteBuffer来存储数据。它们类似于 byte[],但具有可变大小,如果您可以将直接字节缓冲区与 NIO 一起使用,效率可能会更高一些。预分配缓冲区可以更有效地预分配缓冲区。 (虽然可能会浪费虚拟内存)
顺便说一句:直接字节缓冲区使用很少的堆空间,因为它们使用“C”空间中的内存。
The frequency of performing a GC is dependant on the object size, but the cost (the clean up time) is more dependant on the number of objects. I suspect the long living arrays are being copied between the spaces until it end up in the old space and finally discarded. Cleaning the old gen is relatively expensive.
I suggest you try using ByteBuffer to store data. These are like byte[] but have a variable size and can be slightly more efficient if you can use direct byte buffers with NIO. Pre-allocating your buffers can be more efficient to preallocate your buffers. (though can waste virtual memory)
BTW: The direct byte buffers use little heap space as they use memory in the "C" space.
我建议您分析一下为什么 GC 对您来说工作得不够好。您可以使用
jmap
转储堆,然后使用jhat
或 Eclipse Memory Analyser 查看其中存在哪些对象。您可能会发现您保留着不再需要的参考资料。GC 非常聪明,如果您尝试使用自己的内存管理代码来智胜它,实际上可能会让事情变得更糟。尝试调整参数,也许您也可以尝试新的 G1 垃圾收集器。
另外,请记住,GC 喜欢短暂的、不可变的对象。
I suggest you do some analysis into why GC is not working well enough for you. You can use
jmap
to dump out the heap and then usejhat
or Eclipse Memory Analyser to see what objects are living in it. You might find that you are holding on to references that you no longer need.The GC is very clever and you could actually make things worse by trying to outsmart it with your own memory management code. Try tuning the parameters and maybe you can try out the new G1 Garbage Collector too.
Also, remember, that GC loves short-lived, immutable objects.
http://download.oracle.com/docs /cd/E12840_01/wls/docs103/perform/JVMTuning.html#wp1130305
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/perform/JVMTuning.html#wp1130305