我们知道,CMSGC在老生代回收时产生的内存碎片会导致老生代的利用率变低;或者可能在老生代总内存大小足够的情况下,却不能容纳新生代的晋升行为(由于没有连续的内存空间可用),导致触发FullGC。针对这个问题,Sun官方给出了以下的四种解决方法:

  • 增大Xmx或者减少Xmn
  • 在应用访问量最低的时候,在程序中主动调用System.gc(),比如每天凌晨。
  • 在应用启动并完成所有初始化工作后,主动调用System.gc(),它可以将初始化的数据压缩到一个单独的chunk中,以腾出更多的连续内存空间给新生代晋升使用。
  • 降低-XX:CMSInitiatingOccupancyFraction参数以提早执行CMSGC动作,虽然CMSGC不会进行内存碎片的压缩整理,但它会合并老生代中相邻的free空间。这样就可以容纳更多的新生代晋升行为。

原文引用:

引用
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.

Or you can try to make you application do a full, compacting
collection at a time which will not disturb your users. If your
application can go for a day without hitting a fragmentation problem,
try a System.gc() 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 a System.gc()
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 pause collector tries to start a concurrent collection just in time
(with some safety factor) to collect the tenured generation before it is
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 coalese 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 cause a concurrent
collection to occur early by setting the option
-XX:CMSInitiatingOccupancyFraction= where NNN is the percentage of the
tenured generation that is in use above which a concurrent collection is
started. This will increase the overall time you spend doing GC but may
avoid the fragmentation problem. And this will be more effective with
5.0 because a single contiguous chunk of space is not required for
promotions.

我在实际应用中的调优是:

  • 在应用启动并完成所有初始化工作后,主动调用System.gc()。
  • 在Xmx不变的情况下,保持SuvivorSpace不变(为了不让每次MinorGC的晋升大小增加),降低Xmn。
  • 降低-XX:CMSInitiatingOccupancyFraction

我认为在程序中调用System.gc()并不是一个很好的选择,因为:

  • FullGC一定会导致应用暂停,而有些高并发应用是不允许有一次FullGC的,so...
  • 在jdk1.6的某个版本中,调用System.gc()并且应用中有nio操作的话,会导致应用挂起的Bug

另外的一种方法是(没用过,仅供参考):

http://kenwublog.com/avoid-full-gc-in-hbase-using-arena-allocation

CMSGC造成内存碎片的解决方法的更多相关文章

  1. 分享.net常见的内存泄露及解决方法

    分享.net常见的内存泄露及解决方法 关于内存泄漏的问题,之前也为大家介绍过,比如:<C++中内存泄漏的检测方法介绍>,是关于C++内存泄漏的.今天为大家介绍的是关于.NET内存泄漏的问题 ...

  2. Tcp编程常见问题及解决方法总结

    问题1.粘包问题 解决方法一:TCP提供了强制数据立即传送的操作指令push,TCP软件收到该操作指令后,就立即将本段数据发送出去,而不必等待发送缓冲区满: 解决方法二:发送固定长度的消息 解决方法三 ...

  3. 服务器php-cgi.exe进程过多,导致CPU占用100%的解决方法

    再使用iis服务器中经常会出现php-cgi.exe进程过多,导致CPU占用100%,最终造成网站运行过慢甚至卡死的情况,重启iis会好一会,过一段时间久出现这种情况,为什么会出现这种情况呢,应该怎么 ...

  4. IE6、7下html标签间存在空白符,导致渲染后占用多余空白位置的原因及解决方法

    直接上图:原因:该div包含的内容是靠后台进行print操作,输出的.如果没有输出任何内容,浏览器会默认给该空白区域添加空白符.在IE6.7下,浏览器解析渲染时,会认为空白符也是占位置的,默认其具有字 ...

  5. MVVM框架从WPF移植到UWP遇到的问题和解决方法

    MVVM框架从WPF移植到UWP遇到的问题和解决方法 0x00 起因 这几天开始学习UWP了,之前有WPF经验,所以总体感觉还可以,看了一些基础概念和主题,写了几个测试程序,突然想起来了前一段时间在W ...

  6. iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法

    问题描述: iPhone越狱了,之后在Cydia中安装Anywhere虚拟定位,但是打开app提示:后台服务未启动,请重新安装应用后使用. 程序无法正常使用... 解决方法: 打开Cydia-已安装, ...

  7. python中IndentationError: expected an indented block错误的解决方法

    IndentationError: expected an indented block 翻译为IndentationError:预期的缩进块 解决方法:有冒号的下一行要缩进,该缩进就缩进

  8. js闭包for循环总是只执行最后一个值得解决方法

    <style> li{ list-style: none;width:40px;height: 40px;text-align:center;line-height: 40px;curso ...

  9. mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法!

    mysql5.x升级至mysql5.7后导入之前数据库date出错的解决方法! 修改mysql5.7的配置文件即可解决,方法如下: linux版:找到mysql的安装路径进入默认的为/usr/shar ...

随机推荐

  1. synchronized-锁重入

    public class MyThread5_synchronized1 { /** * 父子类同步必须 都 使用synchronized关键字 */ static class Main { publ ...

  2. 统一D3D与OpenGL坐标系统

    作者:游蓝海(http://blog.csdn.net/you_lan_hai) DirectX 3D与OpenGL坐标系统的差异性,给我们带来非常大的麻烦.让跨平台编程的新手非常困惑.近期在做一个跨 ...

  3. django 模板实现换行

    django中的模板并不能实现自动换行,遇到/n的话不会自动换行,毕竟/n是python里面的换行符,html怎么可能认识呢? 那如何实现在模板中换行呢 <p> {{ article.co ...

  4. windows下 memcached 和 redis 服务器安装

    memcached 安装: 1.下载memcached 文件: 2.拷贝到运行目录: 3.命令行进入到程序目录: 运行命令: memcached -d install 如果没有报错说明安装成功 4.打 ...

  5. 一步一步学RenderMonkey(5)--渲染到纹理(RTT) 【转】

    转载请注明出处:  http://blog.csdn.net/tianhai110 渲染到纹理: 新建一个空effect; 添加渲染目标纹理, Add Texture-> Add Render ...

  6. TCP为何采用三次握手来建立连接,若采用二次握手可以吗?

    首先简单介绍一下TCP三次握手     在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_ ...

  7. linux解压分卷压缩的zip文件

    zip -s 0 records.zip --out 1.zip unzip 1.zip

  8. vector iterator not incrementable For information on how your program can cause an an assertion Failure, see the Visual c + + documentation on asserts

    #include <list> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { list<int> sl ...

  9. wifi简单笔记

    什么是wifi: Wi-Fi是一种可以将个人电脑.手持设备(如PDA.手机)等终端以无线方式互相连接的技术.Wi-Fi是一个无线网路通信技术的品牌,由Wi-Fi联盟(Wi-Fi Alliance)所持 ...

  10. 如何使用WinDriver为PCIe采集卡装驱动

    如何使用WinDriver为PCIe采集卡装驱动 第一步:使用WinDriver生成驱动 1.运行Drier Wizard 2.点击New host driverproject 3.在列表中,选择待安 ...