Wednesday, 25 June 2014

Memory Leaks

Memory Leaks in Java


What is a memory leak?
         A memory leak is repetitive allocation of memory without consequential release of it when no longer used, leading to increase in consumption of memory. This may not be a common scenario in most Java standalone applications because Java has inbuilt Garbage Collection.

Garbage Collection in Java  


         Objects in Java are stored in Heap space , while static members are stored in memory area of the Java memory space. In java automatic Garbage collection is done using the Garbage Collection daemon thread.Whenever the object is not referenced by the code it becomes a candidate for garbage collection. Before  removing an object from Garbage Collection thread invokes finalize() method of that object so that it can perform any sort of cleanup.
         
          As a programmer you cannot invoke Garbage collection thread it will get invoked only if the JVM thinks that it needs Garbage Collection based on the Java Memory Space. You can invoke System.gc()  or Runtime.gc() to send request to Garbage Collection but it is not guaranteed that Garbage Collection thread will be invoked.


Types of Garbage Collection in Java 



  • Serial GC - 

    •  With the serial GC it uses internal logic for Garbage collection but in addition , it uses a compaction logic where it collects all the objects to the upper part of the heap, so that the new objects are allocated in continuous locations.


  • Concurrent Low Pause Collector-

    •  This Collector is used if the -Xingc or -XX:+UseConcMarkSweepGC is passed on the command line. This is also referred as Concurrent Mark Sweep Garbage collector. The concurrent collector is used to collect the tenured generation and does most of the collection concurrently with the execution of the application. The application is paused for short periods during the collection. A parallel version of the young generation copying collector is sued with the concurrent collector. Concurrent Mark Sweep Garbage collector is most widely used garbage collector in java and it uses algorithm to first mark object which needs to collected when garbage collection triggers.


  • The Incremental (Sometimes called train) low pause collector - 

    This collector is used only if -XX:+UseTrainGC is passed on the command line. This garbage collector has not changed since the java 1.4.2 and is currently not under active development. It will not be supported in future releases so avoid using this and please see 1.4.2 GC Tuning document for information on this collector. Important point to not is that -XX:+UseParallelGC should not be used with -XX:+UseConcMarkSweepGC. The argument passing in the J2SE platform starting with version 1.4.2 should only allow legal combination of command line options for garbage collector but earlier releases may not find or detect all illegal combination and the results for illegal combination are unpredictable. It’s not recommended to use this garbage collector in java.



           

No comments: