Find Out Your Java Heap Memory Size

Stella981
• 阅读 740

1. Java Heap Size
Place to store objects created by your Java application, this is where Garbage Collection takes place, the memory used by your Java application. For a heavy Java process, insufficient Heap size will cause the popularjava.lang.OutOfMemoryError: Java heap space.

-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size

2. Perm Gen Size
Place to store your loaded class definition and metadata. If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.

-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.

3. Java Stack Size
Size of a Java thread. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.
-Xss = set java thread stack size

$ java -Xss512k JavaApp

Note
The default value for heap size, perm gen, or stack size is differ from different JVMs. The best practice is always defining your own value.

2. Ubuntu

This is the testing environment :

OS  : Ubuntu 13 (64 bits) (Under VirtualBox)RAM : 4G
CPU : 1 x Processors
JDK : 1.7.0_51

$ java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize' 
    uintx InitialHeapSize                          := 64781184        {product}   
    uintx MaxHeapSize                              := 1038090240      {product} 
    uintx PermSize                                  = 21757952        {pd product}
    uintx MaxPermSize                               = 174063616       {pd product}
     intx ThreadStackSize                           = 1024            {pd product}          java version "1.7.0_51"OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.10.1)OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

In above environment, JVM allocated following default values :

  1. Java heap size
    InitialHeapSize = 64781184 bytes (61.7M) and MaxHeapSize = 1038090240 bytes (990M).

  2. PermGen Size
    PermSize = 21757952 bytes (20.75M), MaxPermSize = 174063616 bytes (166M)

  3. Thread Stack Size
    ThreadStackSize = 1024 kilobytes (1M)

The allocated heap memory size is quite close to the ergonomics result.

#ergonomics algorithm 
Initial heap size = 4096M/64 = 64M
Maximum heap size = 4096M/4 = 1024M

3. Mac OSX

This is the testing environment :

OS  : Mac OSX 10.9RAM : 8G
CPU : 4 x Processors
JDK : 1.7.0_05

$ java -XX:+PrintFlagsFinal -version | grep -iE 'heapsize|permsize|threadstacksize' 
    uintx InitialHeapSize                          := 20655360        {product}
    uintx MaxHeapSize                              := 331350016       {product}
    uintx PermSize                                  = 21757952        {pd product}
    uintx MaxPermSize                               = 85983232        {pd product}
     intx ThreadStackSize                           = 1024            {pd product}java version "1.7.0_05"Java(TM) SE Runtime Environment (build 1.7.0_05-b05)Java HotSpot(TM) 64-Bit Server VM (build 23.1-b03, mixed mode)
  1. Java heap size
    InitialHeapSize = 20655360 bytes (19.69M) and MaxHeapSize = 331350016 bytes (316M).

  2. PermGen Size
    PermSize = 21757952 bytes (20.75M), MaxPermSize = 85983232 bytes (82M).

  3. Java Stack Size
    ThreadStackSize = 1024 kilobytes (1M)

The allocated heap memory size is totally irrelevant if compare to the following ergonomics result.

#ergonomics algorithm 
Initial heap size = 8192M/64 = 128M
Maximum heap size = 8192M/4 = 2048M

4. Windows

There is no grep in Windows, instead, we use findstr.

This is the testing environment :

OS  : Windows 8RAM : 16G
CPU : 8 x Processors
JDK : 1.7.0_40

C:\>java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize" 
    uintx InitialHeapSize                          := 266634176       {product}
    uintx MaxHeapSize                              := 4267704320      {product}
    uintx PermSize                                  = 21757952        {pd product}
    uintx MaxPermSize                               = 85983232        {pd product}
     intx ThreadStackSize                           = 0               {pd product}java version "1.7.0_40"Java(TM) SE Runtime Environment (build 1.7.0_40-b43)Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
  1. Java heap size
    InitialHeapSize = 266634176 bytes (256M) and MaxHeapSize = 4266146816 bytes (4068M).

  2. PermGen Size
    PermSize = 21757952 bytes (20.75M), MaxPermSize = 85983232 bytes (823. M).

  3. Java Stack Size
    ThreadStackSize = 0 kilobytes. (weird…)

The allocated heap memory size is almost same with the ergonomics result :

#ergonomics algorithm
Initial heap size = 16384/64 = 256M
Maximum heap size = 16384/4 = 4096M

5. Suggested Java Memory

Below is my suggested value for a small to medium Java application :)

  1. Heap = -Xms512m -Xmx1024m

  2. PermGen = -XX:PermSize=64m -XX:MaxPermSize=128m

  3. Thread = -Xss512k

P.S For most Java projects, 512k for a thread is sufficient.

$ java -XX:+PrintFlagsFinal -Xms512m -Xmx1024m -Xss512k -XX:PermSize=64m -XX:MaxPermSize=128m    -version | grep -iE 'HeapSize|PermSize|ThreadStackSize' 
    uintx InitialHeapSize                          := 536870912       {product}
    uintx MaxHeapSize                              := 1073741824      {product}
    uintx PermSize                                 := 67108864        {pd product}  
    uintx MaxPermSize                              := 134217728       {pd product}  
     intx ThreadStackSize                          := 512             {pd product}
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Java修道之路,问鼎巅峰,我辈代码修仙法力齐天
<center<fontcolor00FF7Fsize5face"黑体"代码尽头谁为峰,一见秃头道成空。</font<center<fontcolor00FF00size5face"黑体"编程修真路破折,一步一劫渡飞升。</font众所周知,编程修真有八大境界:1.Javase练气筑基2.数据库结丹3.web前端元婴4.Jav
Stella981 Stella981
3年前
Python+Selenium自动化篇
本篇文字主要学习selenium定位页面元素的集中方法,以百度首页为例子。0.元素定位方法主要有:id定位:find\_element\_by\_id('')name定位:find\_element\_by\_name('')class定位:find\_element\_by\_class\_name(''
Stella981 Stella981
3年前
JavaScript heap out of memory解决方法
在npm打包的时候,node环境控制台报错了,JavaScriptheapoutofmemory解决方法如下:不管你是angular还是其他的,找到\node_modules\.bin\xxx.cmd如果是angular,那就找ng.cmd 如果是antdesignpro,那就找roadhog.cmd加上这个
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。