Quantcast
Channel: CodingThis.com
Viewing all articles
Browse latest Browse all 15

Memory Management in Java

$
0
0

All objects created by a running Java application are stored in the Java Virtual Machine’s heap memory. Once objects are created they are never freed by the code itself as Java has Garbage Collection process which is the process of automatically freeing objects that are no longer referenced or used by the program. The name “garbage collection” is used for objects that are no longer needed by the java application or program are “garbage” and can be thrown away. Garbage collection in java is also known as or more accurate and up-to-date metaphor for Garbage collection is “memory recycling”. It is basically used to recycle the heap space occupied by the object that is no longer referenced by the java program and which can be made available for subsequent new objects. It is the responsibility of garbage collector to somehow determine which objects are no longer required or referenced by the program and remove them from the heap space. In this process garbage collector must run finalize method so that the object that are need or can be referenced in future get a chance for their survival.

JVM Architecture

Diagram below summarizes a JVM Architecture with its key components. Two main components in this diagram or in a JVM Architecture that are related to garbage collection are heap memory and garbage collector.   JVM-Architecture

Garbage-Collection Roots – The Source of All Object Trees 

There are one or more root objects in every object tree. If we can reach these roots in a tree then it applies that the whole object tree is reachable and cannot be garbage collected. We should be able to know when are those root objects are considered reachable? Special objects called garbage-collection roots are always reachable and so is any object that has a garbage-collection root at its own root. There are four kinds of GC roots in Java

  1. Stack of a thread keeps alive the local variables in a java application or program. This is not actually visible as this is not a real object’s virtual reference. So this is why for all intents and purposes, local variables are Garbage collection roots.
  1. Java threads that are active in a java application are always considered live objects and are therefor considered as GC roots. This is especially important for thread local variables.
  1. Static variables also known as class variables are declared with the static keyword in a class are referenced by their classes. This fact itself makes them GC roots. Classes can be garbage collected, which would remove all referenced static variables. This is of special importance when we are using application servers or class loaders in general.
  1. Native code creates JNI (are referred to as java objects) references as a part of a JNI call. Objects created by such native code are treated specially because the JVM does not know if they are being referenced by the native code or not. Such Java objects represent a very special form of GC.

Garbage-collection-roots

 

Garbage Collection Algorithm: Marking and Sweeping Away Garbage

Garbage collection uses mark and sweep algorithm to determine which objects are no longer in use. JVM runs this algorithm intermittently. In Garbage collection algorithm we must perform two basic things(Mark and Sweep).

  • The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive. It must detect the objects which is first marked, that are no longer referenced by the program and which can be garbage collected.
  • All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects. It should reclaim the space used by such objects by sweeping the objects marked and make the space available again to the program so that new objects can be created using this heap space.

Garbage collection is very helpful in memory management as it allows developer to create new objects without worrying about memory allocation and deallocation; because Garbage collector does this for the developer i.e. it automatically reclaims memory for reuse. This helps in faster developments and also helps in eliminating memory leaks and other memory related problems. Garbage collection is intended to remove the cause for classic memory leaks: unreachable but not deleted objects in memory. However this works only for memory leaks in the original sense. Objects that we want to be garbage collected needs to be dereferenced by the developer so that garbage collection can mark it. It’s possible to have unused objects that are still reachable by an application because the developer simply forgot to dereference them. Such objects cannot be garbage collected. Even worse any software cannot detect such a logical memory leak. Java-garbage-collections When objects are no longer referenced directly or indirectly by a GC root, they will be removed. There are no classic memory leaks. Analysis cannot really identify memory leaks, it can only point out suspicious objects. We can invoke Garbage collection manually also by calling System.gc() command But it does not guarantee that garbage collection will be called immediately. Before seep GC calls a finalize method so that objects that are still needed gets a chance to save themselves. After finalize method GC directly calls sweep step and removes the objects that are no longer needed.

Feel free to share your experiences in the comment section.

The post Memory Management in Java appeared first on CodingThis.com.


Viewing all articles
Browse latest Browse all 15

Trending Articles