Monday 23 February 2015

Find Out Your Java Heap Memory Size and increase it's Size:-

Find Out Your Java Heap Memory Size:-

It's work in the principle of
Ergonomics in the 5.0 Java TM Virtual Machine
how ever it may vary from VM to VM
http://www.oracle.com/technetwork/java/ergo5-140223.html
-------------------------------------------------------------------
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 popular java.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

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.
-----------------------------------------------------------------------------------------
C:\>java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadSta
ckSize"
   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
------------------------------------------------------
It is possible to increase heap size allocated by the JVM by using command line options Here we have 3 options

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

java -Xms16m -Xmx64m ClassName
------------------------------------------------------------------------------------------------------

How many threads can a Java VM support?

--------------------------------
his depends on the CPU you're using, on the OS
and let's done it by a small program:-
public class CountingOfThread {
    private static Object s = new Object();
    private static int count = 0;
    public static void main(String[] argv){
        for(;;){
            new Thread(new Runnable(){
                    public void run(){
                        synchronized(s){
                            count += 1;
                            System.err.println("New thread #"+count);
                        }
                        for(;;){
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e){
                                System.err.println(e);
                            }
                        }
                    }
                }).start();
        }
    }
}
output:-
New thread #4971
New thread #4972
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Unknown Source)
at DieLikeADog.main(DieLikeADog.java:20)

No comments:

Post a Comment