CMSGC造成内存碎⽚的解决⽅法
我们知道,CMSGC在⽼⽣代回收时产⽣的内存碎⽚会导致⽼⽣代的利⽤率变低;或者可能在⽼⽣代总内存⼤⼩⾜够的情况下,却不能容纳新⽣代的晋升⾏为(由于没有连续的内存空间可⽤),导致触发FullGC。针对这个问题,Sun官⽅给出了以下的四种解决⽅法:
增⼤Xmx或者减少Xmn
在应⽤访问量最低的时候,在程序中主动调⽤(),⽐如每天凌晨。
在应⽤启动并完成所有初始化⼯作后,主动调⽤(),它可以将初始化的数据压缩到⼀个单独的chunk中,以腾出更多的连续内存空间给新⽣代晋升使⽤。
aciba>anecdote降低-XX:CMSInitiatingOccupancyFraction参数以提早执⾏CMSGC动作,虽然CMSGC不会进⾏内存碎⽚的压缩整理,但它会合并⽼⽣代中相邻的free空间。这样就可以容纳更多的新⽣代晋升⾏为。
原⽂引⽤:
notatall>shela引⽤
What should you do if you run into a fragmentation problem?
Try 5.0.
Or you could try a larger total heap and/or smaller young generation. If your application is on the edge, it might give you just enough extra space to fit all your live data. But often it just delays the problem.复旦中学吧
oil paintingsOr you can try to make you application do a full, compacting collection at a time which will not disturb your urs. If your application can go for a day without hitting a fragmentation problem, try () in the middle of the night. That will compact the heap and you can hopefully go another day without hitting the fragmentation problem. Clearly no help for an application that does not have a logical "middle of the night".
Or if by chance most of the data in the tenured generation is read in when your application first starts up and you can do () after you complete initialization, that might help by compacting all data into a single chunk leaving the rest of the tenured generation available for promotions. Depending on the allocation pattern of the application, that might be adequate.
Or you might want to start the concurrent collections earlier. The low pau collector tries to start a concurrent collection just in time (with some safety factor) to collect the tenured generation before it i
s full. If you are doing concurrent collections and freeing enough space, you can try starting a concurrent collection sooner so that it finishes before the fragmentation becomes a problem. The concurrent collections don't do a compaction, but they do coale adjacent free blocks so larger chunks of free space can result from a concurrent collection. One of the triggers for starting a concurrent collection is the amount of free space in the tenured generation. You can cau a concurrent collection to occur early by tting the option -XX:CMSInitiatingOccupancyFraction= where NNN is the percentage of the tenured generation that is in u above which a concurrent collection is started. This will increa the overall time you spend doing GC but may avoid the fragmentation problem. And this will be more effective with 5.0 becau a single contiguous chunk of space is not required for promotions.
高中试卷网我在实际应⽤中的调优是:asif
在应⽤启动并完成所有初始化⼯作后,主动调⽤()。
在Xmx不变的情况下,保持SuvivorSpace不变(为了不让每次MinorGC的晋升⼤⼩增加),降低Xmn。corollary
降低-XX:CMSInitiatingOccupancyFraction
我认为在程序中调⽤()并不是⼀个很好的选择,因为:
肿胀FullGC⼀定会导致应⽤暂停,⽽有些⾼并发应⽤是不允许有⼀次FullGC的,so...
在jdk1.6的某个版本中,调⽤()并且应⽤中有nio操作的话,会导致应⽤挂起的