Thursday 18 April 2013

Increasing Heap Size

How to increase Heap Size for Java and Maven

[I] Increasing the Heap size for Maven

OPTION 1 for MAVEN in Windows OS

STEPS:-

1) Goto / Select maven installation dir -> bin
2) Right click on mvn.bat and open it for editing
3) If MAVEN_OPTS is already defined then we can edit their value like below otherwise add the below
    line:-

set MAVEN_OPTS=-Xmx1024m


OPTION 2 for MAVEN in Windows OS

Defining as Environment Variable :- As a User Variable

variable name:  MAVEN_OPTS
variable value : -Xms512m -Xmx1024m


OPTION 3 for MAVEN in different(linux) OS

Setting or Increasing Java heap memory for Maven in linux OS

export MAVEN_OPTS="-Xmx1024m -XX:MaxPermSize=128m"


[II] Increasing the Heap size for JAVA

Setting/Increasing JVM Heap Size for JAVA

We can increase JVM heap size using below 3 options in java:

    -Xms<size>        starting/Initial heap size
    -Xmx<size>        maximum heap size
    -Xss<size>        set java thread stack size


Java handles its memory in two areas. The heap and the stack. Any Java program executes in JVM(Java virtual machine), then it is using heap memory for managing data. If any particular Java program requires a huge amount of memory, then our JVM will throw OutOfMemoryError.

How JVM is managing memory in order to manage application data?

Actualy, JVM is using the below instructions to manage memory :-

(1) When a JVM is invoked to run an application, first it will ask to OS(operating system) for enough memory  to run the JVM itself and some free memory for the application to create new objects.

(2) When a new object is instantiated, the JVM will allocate memory for that object out of the free memory area.

(3) When the free memory area is getting too small, the JVM will ask the operating system for more space.

(4) When Our Application have no reference anymore to objects, then it will be allowed to destroyed by garbage collector. Then the memory occupied by that object is freed up and merged back to the free memory area. When the free memory area is completely used and OS is not providing any more additional memory, the JVM will stop the application and throw "Out of memory error".


About Java Heap Memory

JVM stores all the objects created by java application using the "new" operator is stored in the Heap Memory Java garbage collector (gc) can logically separate the heap into different areas, so that the gc can quickly identify those objects which can get removed from the heap memory.For new objects, memory is allocated on the heap during Runtime. Instance variables live inside the object in which they are declared.

About Java Stack Memory

Method invocations and the local variables are stored in Stack Memory. When a method is invoked then its stack frame is kept onto the top of the call stack. The stack frame holds the state of the method including which line of code is executing and the values of all local variables. The method at the top of the stack is always the current running method for that stack. Threads have their own call stack.

OPTION 1 for JAVA in Windows OS

Similarly as above, we can define Environment variables(as a User Variable) to increase the heap size for java also :-

variable name:  JAVA_OPTS
variable value : -Xms512m -Xmx1024m


OPTION 2 for JAVA

We can place/set minimum JVM Heap size to 64MB and maximum heap size to 256MB for a particular Java class:

java -Xms512M -Xmx1024M HelloWorldExample(class fileName) or package.fileName(without extension)


How to Get JVM heap size using Java program?


public class KnowingJVMHeapSize {

    private final static long BYTES = 1;
    private final static long KILOBYTES = 1024;
    private final static long MEGABYTES = 1048576; // 1024 * 1024(1 MB = 1024 KB
                               // and 1 KB = 1024 Bytes)
    private final static long GIGABYTES = 1073741824;//1024 * 1024 * 1024

    public static void main(String... args) {

        // To Get the jvm heap size in Bytes using totalMemory() method.
        long jvmHeapSizeInBytes = Runtime.getRuntime().totalMemory() / BYTES;

        // To Get the jvm heap size in KB using totalMemory() method.
        long jvmHeapSizeInKB = Runtime.getRuntime().totalMemory() / KILOBYTES;

        // To Get the jvm heap size in MB using totalMemory() method.
        long jvmHeapSizeInMB = Runtime.getRuntime().totalMemory() / MEGABYTES;

        // To Get the jvm heap size in GB using totalMemory() method.
        long jvmHeapSizeInGB = Runtime.getRuntime().totalMemory() / GIGABYTES;

        // For printing the jvm heap size in Bytes.
        System.out.println(jvmHeapSizeInBytes + " BYTES");

        // For printing the jvm heap size in KB.
        System.out.println(jvmHeapSizeInKB + " KB");

        // For printing the jvm heap size in MB.
        System.out.println(jvmHeapSizeInMB + " MB");

        // For printing the jvm heap size in GB.
        System.out.println(jvmHeapSizeInGB + " GB");
    }
}

Note:- If you are working with Eclipse and your eclipse is working slow, then go to the eclipse installation directory, there you will find eclipse.ini(configuration.settings) file, open this file in edit mode and add the below commands(if it is not available otherwise modify this as below):-

-Xms512m
-Xmx1024m

This will improve the performance of Eclipse.

No comments:

Post a Comment