Java : HEAP Memory 参数设置

2019-05-16 01:17:16 +08:00
 c4f36e5766583218
  1. https://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html
    -Xmxsize
    Specifies the maximum size (in bytes) of the memory allocation pool in bytes.
    
    -XX:NewRatio=old/young   By default, this option is set to 2.
    
    -XX:SurvivorRatio=eden/survivor   By default, this option is set to 8.
    
  2. 你比如说我设置-Xmx3212M -XX:NewRatio=3 -XX:SurvivorRatio=9
  3. https://github.com/alibaba/arthas/blob/555958c6083dd69fdde25b732c831af99215c53c/core/src/main/java/com/taobao/arthas/core/command/monitor200/DashboardCommand.java#L183
    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    List<MemoryPoolMXBean> memoryPoolMXBeans = ManagementFactory.getMemoryPoolMXBeans();
    
    MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
    System.out.println(heapMemoryUsage);
    for (MemoryPoolMXBean memoryPoolMXBean : memoryPoolMXBeans) {
        if(MemoryType.HEAP.equals(memoryPoolMXBean.getType())) {
            System.out.println(memoryPoolMXBean.getName()+"\t"+memoryPoolMXBean.getUsage());
        }
    }
    
  4. console
    init = 134217728(131072K) used = 1698944(1659K) committed = 131596288(128512K) max = 3291480064(3214336K)
    PS Eden Space   init = 28311552(27648K) used = 1698944(1659K) committed = 28311552(27648K) max = 836763648(817152K)
    PS Survivor Space   init = 2621440(2560K) used = 0(0K) committed = 2621440(2560K) max = 2621440(2560K)
    PS Old Gen  init = 100663296(98304K) used = 0(0K) committed = 100663296(98304K) max = 2526019584(2466816K)
    
  5. Q1: console 和我设的参数是怎么个对应法?
    • init used committed max 它们之间关系?
    • 3291480064=-Xmx3212M ?就是有点小偏差的?
    • -XX:NewRatio=3,指哪列哦? committed ? init ? 3=100663296/(28311552+2621440*2) ?
    • -XX:SurvivorRatio=9,指哪列?怎么算?
  6. Q2: console Survivor 的几个值都是 2621440 ?
1623 次点击
所在节点    问与答
3 条回复
c4f36e5766583218
2019-05-16 16:43:09 +08:00
https://blog.csdn.net/fanwu72/article/details/8936746
```结论:init 约等于 xms 的值,max 约等于 xmx 的值。used 是已经被使用的内存大小,committed 是当前可使用的内存大小(包括已使用的),committed >= used。committed 不足时 jvm 向系统申请,若超过 max 则发生 OutOfMemoryError 错误。``` committed 初始运行时候约等于 init,然后随着使用 /gc 增大 /减小(与 init 无关)
c4f36e5766583218
2019-05-16 16:58:02 +08:00
猜测: XX:NewRatio XX:SurvivorRatio 可以使用 committed 列来算,可能之前选的值不太好,试了下```-XX:NewRatio=3 -XX:SurvivorRatio=6```是符合的
c4f36e5766583218
2019-05-16 19:01:46 +08:00
@c4f36e5766583218 #2 好像不对。重新猜测:
* XX:NewRatio XX:SurvivorRatio 可能之前选的值不太好,试```-XX:NewRatio=3 -XX:SurvivorRatio=6```
* PS Survivor Space 是指其中之一,共 2 个
* 使用 init 值来计算
* HeapMemory.init = (Eden.init + Survivor.init * 2) + Old.init
* Old.init / (Eden.init + Survivor.init * 2) = 3
* Eden.init / Survivor.init = 6

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/564546

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX