Search This Blog

Saturday, May 17, 2014

Simulate Java StackOverFlow Exception



Stack memory:
  Let me describe  high level detail of stack memory, To execute any program we need to allocate two type of memory in processor(RAM).
     One is stack Memory -  The memory used for executing the program in processor.
   Second is Heap Memory- This memory is nothing but to Store the data(Object) in processor.


Stack Memory  will be start used, when program start from main method to the other  method execution.

Example :
Start using stack memory:  main-> method1-> Method 2->..... > Method N
Release stack memory :Method N (execution Over) -> Method N-1 ->  .............. > Method1->main

Below Program  stack memory are not released due to recursive   call of method1(). So it will throw stack over flow exception.




package com.blogspot.microtechinfo;

public class JavaStackOverFlow {

          private static int count=0;
         
          /**
           * @param args
           */
          public static void main(String[] args) {
                   // TODO Auto-generated method stub
                  
                             method1();
                  
          }
          private static void method1()
          {
                  
                   try{
                            
                             System.out.println("count="+ ++count);
                  method1();
                   }
                   catch (Exception e) {
                             // TODO: handle exception
                             e.printStackTrace();
                             System.exit(1);
                   }
                  
                  
          }
         
}


 Output:
  count=4484
count=4485
count=4486
count=4487
count=4488
count=4489
count=4490
Exception in thread "main" java.lang.StackOverflowError
          at sun.nio.cs.SingleByteEncoder.encodeArrayLoop(Unknown Source)
          at sun.nio.cs.SingleByteEncoder.encodeLoop(Unknown Source)
          at java.nio.charset.CharsetEncoder.encode(Unknown Source)
          at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
          at sun.nio.cs.StreamEncoder.write(Unknown Source)
          at java.io.OutputStreamWriter.write(Unknown Source)
          at java.io.BufferedWriter.flushBuffer(Unknown Source)
          at java.io.PrintStream.write(Unknown Source)
          at java.io.PrintStream.print(Unknown Source)
          at java.io.PrintStream.println(Unknown Source)

          at com.blogspot.microtechinfo.JavaStackOverFlow.method1(JavaStackOverFlow.java:21)



Simulate Java OutOfMemoryError

Simulate Java OutOfMemoryError
Here am simulating java OutOfMemoryError, lets discuss brief details about OutOfMemoryError.

Why OutOfMemoryError error will arise?
          OutOfMemoryError will arise when allotted Heap size of the Java virtual will be exceeds during runtime.

Where object will be saved in Java?
          Basically all java class objects will be saved during runtime in HeapMemory.

Sample Java Program to simulate OutOfMemoryError

In below program name is object of String, so name value will be saved to Heap space of JVM.
          Calling method1 infinite time will cause java.lang.OutOfMemoryError: Java heap space error, because below line in method1 is causing this error. If you comments below line you don’t get this error.
                         name+=";"+name;


package com.blogspot.microtechinfo;

public class JavaOutOfMemory {

            private static String  name="Marimuthu Rajagopal";
          private static int count=0;
         
          /**
           * @param args
           */
          public static void main(String[] args) {
                   // TODO Auto-generated method stub
                        while(true)
                   {
                             method1();
                   }
          }
          private static void method1()
          {
                  
                         name+=";"+name;
                   System.out.println("count="+ ++count);
                  
          }
         
}

 Output:
  
when count is 20 i will be getting  java.lang.OutOfMemoryError: Java heap space,It will vary based on machine.

count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
count=11
count=12
count=13
count=14
count=15
count=16
count=17
count=18
count=19
count=20
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
          at java.util.Arrays.copyOfRange(Unknown Source)
          at java.lang.String.(Unknown Source)
          at java.lang.StringBuilder.toString(Unknown Source)
          at com.blogspot.microtechinfo.JavaOutOfMemory.method1(JavaOutOfMemory.java:21)
          at com.blogspot.microtechinfo.JavaOutOfMemory.main(JavaOutOfMemory.java:15)