简体   繁体   English

Java上的垃圾问题太多了

[英]Too Many Garbage Problem on Java

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. 我有一个应用程序,基本上,创建一个新的字节数组(小于1K)存储一些数据几秒钟后(通常不到1分钟,但一些数据存储长达1小时)写入磁盘,数据将转为垃圾。 Approximatelly 400 packets per second created. 每秒创建大约400个数据包。 I read some articles that say don't worry about GC especially quickly created and released memory parts (on Java 6). 我读了一些文章,说不要担心GC特别是快速创建和发布的内存部分(在Java 6上)。 GC runs too long cause some problem about on my application. GC运行时间过长导致我的应用程序出现问题。 I set some GC parameters(Bigger XMX and ParalelGC),this decrease Full GC time decrease but not enough yet. 我设置了一些GC参数(Bigger XMX和ParalelGC),这减少了Full GC时间减少但还不够。 I have 2 idea, Am I focus GC parameters or create Byte array memory pool mechanism? 我有2个想法,我是否关注GC参数或创建Byte数组内存池机制? Which one is better? 哪一个更好?

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. 执行GC的频率取决于对象大小,但成本(清理时间)更多地取决于对象的数量。 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. 我建议你尝试使用ByteBuffer来存储数据。 These are like byte[] but have a variable size and can be slightly more efficient if you can use direct byte buffers with NIO. 这些类似于byte []但具有可变大小,如果可以使用带有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. BTW:直接字节缓冲区使用很少的堆空间,因为它们在“C”空间中使用内存。

I suggest you do some analysis into why GC is not working well enough for you. 我建议你分析为什么GC不能很好地适合你。 You can use jmap to dump out the heap and then use jhat or Eclipse Memory Analyser to see what objects are living in it. 您可以使用jmap转储堆,然后使用jhatEclipse Memory Analyzer查看其中包含的对象。 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. GC非常聪明,你可以通过尝试用自己的内存管理代码来超越它,从而使事情变得更糟。 Try tuning the parameters and maybe you can try out the new G1 Garbage Collector too. 尝试调整参数,也许你也可以尝试新的G1垃圾收集器。

Also, remember, that GC loves short-lived, immutable objects. 另外,请记住,GC喜欢短暂的,不可变的对象。

  1. Use profiler to identify the code snippet 使用分析器来标识代码段
  2. Try with WeakReferences. 尝试使用WeakReferences。
  3. Suggest an GC algo to the VM 向VM建议GC算法
 -Xgc: parallel 
  1. Set a big Heap and shared mem 设置一个大堆和共享内存
 -XX:+UseISM -XX:+AggressiveHeap 
  1. set below for garbage collection. 设置如下垃圾收集。

-XX:SurvivorRatio 8

  1. This may help 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM