Find out your Java heap memory size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm.
Heap sizes
Initial heap size of 1/64 of physical memory up to 1Gbyte
Maximum heap size of 1/4 of physical memory up to 1Gbyte
However, above algorithms are just for reference, it may vary in different VM.
1. Java Memory Overview
A quick review of Java memory structure :
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
$ java -Xms512m -Xmx1024m JavaApp
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.
$ java -XX:PermSize=64m -XX:MaxPermSize=128m JavaApp
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
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 :
- Java heap size
InitialHeapSize = 64781184 bytes (61.7M) and MaxHeapSize = 1038090240 bytes (990M). - PermGen Size
PermSize = 21757952 bytes (20.75M), MaxPermSize = 174063616 bytes (166M) - 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.9
RAM : 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)
- Java heap size
InitialHeapSize = 20655360 bytes (19.69M) and MaxHeapSize = 331350016 bytes (316M). - PermGen Size
PermSize = 21757952 bytes (20.75M), MaxPermSize = 85983232 bytes (82M). - 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 8
RAM : 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)
- Java heap size
InitialHeapSize = 266634176 bytes (256M) and MaxHeapSize = 4266146816 bytes (4068M). - PermGen Size
PermSize = 21757952 bytes (20.75M), MaxPermSize = 85983232 bytes (823. M). - 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 :)
- Heap = -Xms512m -Xmx1024m
- PermGen = -XX:PermSize=64m -XX:MaxPermSize=128m
- 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}
6. FAQs
Q. What is -version?
A. Avoid the complaints from Java compiler, replace the “-version” with your Java application name.
$ java -XX:+PrintFlagsFinal {your-java-program} | grep HeapSize
Q. What is -XX:+PrintCommandLineFlags?
A. This -XX:+PrintCommandLineFlags is used to print out the values that modified by VM only (indicated by this :=symbol).
7. Conclusion
Finally, the default values of heap memory, perm gem and stack size is different from each JVMs, do not expect JVM will assign the optimal values for your Java application. The best practice is found out your memory detail, then fine tune the values accordingly.
Just some find out and sharing, do let me know your comment.
Find out your Java heap memory size的更多相关文章
- Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)
--reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- DTrace to Troubleshoot Java Native Memory Problems
How to Use DTrace to Troubleshoot Java Native Memory Problems on Oracle Solaris 11 Hands-On Labs of ...
- 报错:out of memory java heap space
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出,解决 ...
- Java heap size
今天在性能诊断工作中遇到 Java heap size, 下面是它的相关的概念. 什么是Java heap size ? Java heap size 堆栈大小, 指Java 虚拟机的内存大小.我的理 ...
- netbean out of memory java heap space
When run test file in netbean. all dependency and resource are right, but it raise up java.lang.OutO ...
- Memory Analyzer Tool定位Java heap space内存泄漏
java heap space是一个很蛋疼的问题,如果开发调试时遇到还好,如果是在项目上线后运行一段时间后,才抛出该异常,那真的很悲剧(那你得找代码中到底是哪里内存泄露了),这真是一个悲伤的故事. 1 ...
- JVM--你常见的jvm 异常有哪些? 代码演示:StackOverflowError , utOfMemoryError: Java heap space , OutOfMemoryError: GC overhead limit exceeded, Direct buffer memory, Unable_to_create_new_native_Thread, Metaspace
直接上代码: public class Test001 { public static void main(String[] args) { //java.lang.StackOverflowErro ...
- java.util.jar.JarFile cause native heap memory leak
最近项目中使用了JarFile 这个类 来load jar包中的 configuration,大致的情况如下 public void processJarEntries(JarFile paramJa ...
随机推荐
- 关于Floyd-Warshall算法由前趋矩阵计算出的最短路径反映出了算法的执行过程特性的证明
引言:Floyd-Warshall算法作为经典的动态规划算法,能够在O(n3)复杂度之内计算出所有点对之间的最短路径,且由于其常数较小,对于中等规模数据运行效率依然可观.算法共使用n此迭代,n为顶点个 ...
- Flask - 第一篇
首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 一. Python 现阶段三大主流Web框 ...
- Squid 代理服务器日志管理
简介: Squid 服务器日志增长是很快的,如果不做处理的话,可以会由于系统限制单文件大小,而导致 Squid 服务停止,太大的日志文件也不适合分析. 一.日志配置 shell > grep ' ...
- Linux 登陆提示文字
/etc/issue是从本地登陆显示的信息 /etc/issue.net是从网络登陆显示的信息 /etc/motd内容由系统管理员确定,常用于通告信息,如计划关机时间的警告等 每次用户登录时,/etc ...
- C#关于using用法的总结
1 作为指令,引入命名空间 using 命名空间的名字,这样可以直接使用命名空间中的类型,而不必指定类型的详细命名空间. 2 作为指令,定义别名 using 别名=详细命名空间信息的具体的类型. 3 ...
- MySQL数据库篇之数据类型
主要内容: 一.数值类型 二.日期类型 三.字符串类型 四.枚举类型与集合类型 1️⃣ 数值类型 1.整数类型:tinyint smallint mediumint int bigint 作用 ...
- 用Pylint规范化Python代码,附PyCharm配置
Pylint一个可以检查Python代码错误,执行代码规范的工具.它还可以对代码风格提出建议. 官网:https://pylint.readthedocs.io pip install pylint ...
- java基础之HashSet如何保证对象的唯一性
首先Set集合是无序的 不可重复的 add的时候判断对象是否重复是用的equals HashSet<String> 存储String类型的数据时是可以保证数据的唯一性的 因为String类 ...
- java动态规划取硬币问题
最近一直在研究动态规划的问题.今天遇到了取硬币问题. 其实动态规划还是,我从底部向顶部,依次求出每个状态的最小值,然后就可以标记上. 这道题目就是,假如有1,5,7,10这四种币值的硬币,我取14元, ...
- 【HDU2825】Wireless Password【AC自动机,状态压缩DP】
题意 题目给出m(m<=10)个单词,每个单词的长度不超过10且仅由小写字母组成,给出一个正整数n(n<=25)和正整数k,问有多少方法可以组成长度为n的文本且最少包含k个给出的单词. 分 ...