您的位置:首页 > 编程语言 > Java开发

Java - Java Heap Space or Java Heap Memory

2015-07-20 10:44 826 查看
http://javarevisited.blogspot.sg/2011/05/java-heap-space-memory-size-jvm.html

http://javarevisited.blogspot.sg/2012/01/find-max-free-total-memory-in-java.html

http://javarevisited.blogspot.de/2011/08/increase-heap-size-maven-ant.html

Because I can't open these pages without proper proxy configuration, so I have to copy the contents here.

What is Heap space in Java?

When a Java program started Java Virtual Machine gets some memory from Operating System. Java Virtual Machine or JVM uses this memory for all its need and part of this memory is call java heap memory. Heap in Java generally located
at bottom of address space and move upwards. whenever we create object using new operator or by any another means, object is allocated memory from Heap and When object dies or garbage collected ,memory goes back to Heap space in Java.

How to increase heap size in Java

Default size of Heap space in Java is 128MB on most of 32 bit Sun's JVM but its highly varies from JVM to JVM. e.g. default maximum and start heap size for the 32-bit Solaris Operating System (SPARC Platform Edition) is -Xms=3670K
and -Xmx=64M and Default values of heap size parameters on 64-bit systems have been increased up by approximately 30%. Also if you are using throughput garbage collector in Java 1.5 default maximum heap size of JVM would be Physical Memory/4 and default initial
heap size would be Physical Memory/16. Another way to find default heap size of JVM is to start an application with default heap parameters and monitor in using JConsole which is available on JDK 1.5 onwards, on VMSummary tab you will be able to see maximum
heap size.

By the way you can increase size of java heap space based on your application need and I always recommend this to avoid using default JVM heap values. If your application is large and lots of object created you can change size
of heap space by using JVM options -Xms and -Xmx. Xms denotes starting size of Heap while -Xmx denotes maximum size of Heap in Java. There is another parameter called -Xmn which denotes Size of new generation of Java Heap Space. Only thing is you can not
change the size of Heap in Java dynamically, you can only provide Java Heap Size parameter while starting JVM.

How to get max memory, free memory and total memory in Java

How to get free Memory in Java

In order to get currently free Memory available for creating object use Runtime.getRuntime().freeMemory() method, this will return size in bytes, which you convert in Mega Bytes for better readability.

How to get total Memory in Java

You can use Runtime.getRuntime.totalMemory() to get total memory from JVM which represent current heap size of JVM which is combination of used memory currently occupied by objects and free memory available for new objects.

How to get initial Heap Size from Java Program

We specify initial heap space by using -Xms and JVM creates initial heap with this much size. in order to get this size from Java program call Runtime.getRuntime.totalMemory() before creating any object.

How to get maximum Heap Size from Java Program

This is relatively easy as maximum heap space is not going to change over JVM life cycle and call to Runtime.getRuntime.maxMemory() will return value close to -Xmx but keep in mind exact value will be little less than what have
you set.

How to get Used Memory in JVM

By using Runtime.getRuntime.totalMemory() and Runtime.getRuntime.freeMemory() we can calculate how much space has been currently occupied by Java object.

Java Heap and Garbage Collection

As we know objects are created inside heap memory and Garbage collection is a process which removes dead objects from Java Heap space and returns memory back to Heap in Java. For the sake of Garbage collection, Heap is divided
into three main regions, named as New Generation, Old or Tenured Generation and Perm space (Permanent space).

New Generation of Java Heap is part of Java Heap memory where newly created object are stored.

During the course of application many objects created and died but those remain live they got moved to Old or Tenured Generation by Java Garbage collector thread on Major or full garbage collection.

Perm space of Java Heap is where JVM stores Meta data about classes and methods, String pool and Class level details. When a class is loaded by a classloader it got stored in PermGen space, it gets unloaded only when the classloader
which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen
space.

OutOfMemoryError in Java Heap

When JVM starts, JVM heap space is equal to the initial size of Heap specified by -Xms parameter, as application progress more objects get created and heap space is expanded to accommodate new objects. JVM also run garbage collector
periodically to reclaim memory back from dead objects. JVM expands Heap in Java some where near to Maximum Heap Size specified by -Xmx and if there is no more memory left for creating new object in java heap , JVM throws java.lang.OutOfMemoryError and your
application dies.

Before throwing OutOfMemoryError No Space in Java Heap, JVM tries to run garbage collector to free any available space but even after that not much space available on Heap in Java it results into OutOfMemoryError.

To resolve this error you need to understand your application object profile i.e. what kind of object you are creating, which objects are taking how much memory etc. you can use profiler or heap analyzer to troubleshoot OutOfMemoryError
in Java.

"java.lang.OutOfMemoryError: Java heap space" error messages denotes that Java heap does not have sufficient space and cannot be expanded further.

"java.lang.OutOfMemoryError: PermGen space" error message comes when the permanent generation of Java Heap is full, the application will fail to load a class or to allocate an interned string.

Java Heap dump

Java Heap dump is a snapshot of Java Heap Memory at a particular time. This is very useful to analyze or troubleshoot any memory leak in Java or any Java.lang.OutOfMemoryError. There are tools available inside JDK which helps
you to take heap dump and there are heap analyzer available tool which helps you to analyze java heap dump. You can use "jmap" command to get java heap dump, this will create heap dump file and then you can use "jhat - Java Heap Analysis Tool" to analyze those
heap dumps.

How to increase Java heap space on Maven and ANT

Many times we need to increase heap size of Maven or ANT because once number of classes increases build tool requires more memory to process and build and often throw OutOfMemoryError which we can avoid by changing or increase
heap memory of JVM.

How to increase Java Heap size for Maven on Linux/Unix

Open your mvn.sh file and add the following line:

export MAVEN_OPTS=-Xmx512m

this will allow to specify java heap space based on your project needs.

How to increase Java Heap space for Maven on Windows

1) go to C:\apache-maven-2.2.1\bin

2) open your mvn.bat file and add the following line :

set MAVEN_OPTS=-Xmx512m

next time you run maven commands like mvn clean or mvn install it will use this heap size.

How to increase Java Heap size/space/memory for ANT on Linux/Unix

Similar to Maven you can specify ANT_OPTS to specify JVM specific arguments for ANT build tool. Here is step by step guide to change the java heap space for ANT:

1) Go to ANT_HOME directry (directory where Ant has been installed)

2) Open ant.bat file and add following line on it

export ANT_OPTS=-Xmx512m

some time you may need to put the value –Xmx512M inside double quotes.

3) Done next time you run “ant” it will use modified java heap size and will not throw OutOfMemoryError till the new limit reached.

How to increase Java Heap size/space/memory for ANT on Windows

As shown above for Linux ANT_OPTS can also be used specify JVM specific arguments for ANT in windows. Here is step by step guide to increase the java heap memory for ANT in Windows:

1) Go to ANT_HOME directory (directory where Ant has been installed)

2) Open ant.bat file and add following line on it

set ANT_OPTS=-Xmx512m

some time you may need to put the value –Xmx512M inside double quotes.

3) Done next time you run “ant” it will use modified java heap size and will not throw OutOfMemoryError till the new limit reached.

10 Points about Java Heap Space

1. Java Heap Memory is part of memory allocated to JVM by Operating System.

2. Whenever we create objects they are created inside Heap in Java.

3. Java Heap space is divided into three regions or generation. For sake of garbage collection called New Generation, Old or tenured Generation or Perm Space. Permanent generation is garbage collected during full gc in hotspot
JVM.

4. You can increase or change size of Java Heap space by using JVM command line option -Xms, -Xmx and -Xmn. don't forget to add word "M" or "G" after specifying size to indicate Mega or Gig. for example you can set java heap
size to 258MB by executing following command java -Xmx256m HelloWord.

5. You can use either JConsole or Runtime.maxMemory(), Runtime.totalMemory(), Runtime.freeMemory() to query about Heap size programmatic in Java.

6. You can use command "jmap" to take Heap dump in Java and "jhat" to analyze that heap dump.

7. Java Heap space is different than Stack which is used to store call hierarchy and local variables.

8. Java Garbage collector is responsible for reclaiming memory from dead object and returning to Java Heap space.

9. Don’t panic when you get java.lang.OutOfMemoryError, sometimes its just matter of increasing heap size but if it’s recurrent then look for memory leak in Java.

10. Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: