V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
c4f36e5766583218
V2EX  ›  问与答

Java : HEAP Memory 参数设置

  •  
  •   c4f36e5766583218 · 2019-05-16 01:17:16 +08:00 · 1613 次点击
    这是一个创建于 1801 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 ?
    第 1 条附言  ·  2019-05-16 16:03:10 +08:00
    For example, setting -XX:NewRatio=3 means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one-fourth of the total heap size.
    
    For example, -XX:SurvivorRatio=6 sets the ratio between eden and a survivor space to 1:6. In other words, each survivor space will be one-sixth the size of eden, and thus one-eighth the size of the young generation (not one-seventh, because there are two survivor spaces).
    
    3 条回复    2019-05-16 19:01:46 +08:00
    c4f36e5766583218
        1
    c4f36e5766583218  
    OP
       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
        2
    c4f36e5766583218  
    OP
       2019-05-16 16:58:02 +08:00
    猜测: XX:NewRatio XX:SurvivorRatio 可以使用 committed 列来算,可能之前选的值不太好,试了下```-XX:NewRatio=3 -XX:SurvivorRatio=6```是符合的
    c4f36e5766583218
        3
    c4f36e5766583218  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1107 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 18:31 · PVG 02:31 · LAX 11:31 · JFK 14:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.