当前位置: 动力学知识库 > 问答 > 编程问答 >

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. 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?

网友答案:

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 use jhat 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.

网友答案:

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.

网友答案:
  1. Use profiler to identify the code snippet
  2. Try with WeakReferences.
  3. Suggest an GC algo to the VM
-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
分享给朋友:
您可能感兴趣的文章:
随机阅读: