Java Garbage Collection Basics

Memory management in Java

For many programmers, Java's memory management is one of its most attractive features, because a Java programmer never has to deal directly with memory allocation and recovery. Java allows programmers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. All Java objects automatically grab the memory that they need when they are created, and when the object is no longer need, the Java Garbage Collection process reclaim the memory. That means, the Garbage Collector tracked live objects and everything else designated garbage. Through Garbage collection, most of the memory-management issues are solved, but often at the cost of creating serious performance problems.

How can an object be unreferenced?

When a typical Java program is running, it is creating new objects, such as Strings and Files, but after a certain period, those objects are not used anymore in the application. For example, take a look at the following code:
for (File fl : files) { String str = fl.getName(); }
In the above code, the String str is being created on each iteration of the for loop. That means in every iteration, a little bit of memory is being allocated to make a String object. We can see that once a single iteration is executed, in the next iteration, the String object that was created in the previous iteration is not being used anymore, that object is now considered as "unreferenced object" or "garbage" .

What is Garbage Collection in Java?

In java, garbage means unreferenced objects. Garbage collection is often portrayed as the opposite of manual memory management, which requires the developer to specify which objects to deallocate and return to the memory system. For example, in other languages such as "C" , one needs to perform memory management on their own using functions such as "malloc" and "free" . In Java, JVM automatically calls garbage collector. The garbage collector is a program which runs on the JVM which gets rid of unused objects which are not being used by a Java application anymore. It is a form of automatic memory management . In Java, an in use object, or a referenced object, means that some part of the application still maintains a pointer to that object. An unused object, or unreferenced object, is no longer referenced by any part of the application. So the memory used by an unreferenced object can be reclaimed . The garbage collector will look for objects which aren't being used anymore, and removed them, freeing up the memory so other new objects can use that piece of memory. The finalize() method is invoked each time before the object is garbage collected. This method can be used to perform clean-up processing.

How to force garbage collection in Java?

The best option is to call System.gc() which simply is a hint to the garbage collector that you want it to do a collection. But this does not mean that it'll be executed immediately. Virtual Machine internally uses some algorithm to decide when to make this call. When you make call using System.gc(), it is just a request to JVM and JVM can anytime decide to ignore it.

Is it good practice to call Garbage Collector manually?

No, it is definitely not good practice to call Garbage Collector manually. The garbage collector in Virtual Machine contains a lot of sophisticated logic to determine when and what to cleanup. Tuning it requires knowledge about details on how it works. Just putting a System.gc() somewhere in the code is not likely to help a lot, and in fact, it can even make it worse. Some people sets the relevant object to null or use System.gc() method to remove the memory explicitly. Setting it to null is not a big deal, but calling System.gc() method will affect the system performance drastically, and must not be carried out.

Advantage/Disadvantages of Garbage Collection

  1. Automatic deallocation allows a programmer not to worry about memory management, increasing write ability of a system, and decreasing development time and costs.
  2. It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
  3. Garbage collection is a language requirement for functional languages, which cannot use a stack-based environment because of unpredictable execution patterns.
  4. Explicit management introduces possibilities for making errors in memory management - for example, memory leaks. Thus, explicit deallocation decreases reliability.

When Is The Object Eligible For Garbage Collection?

Generally, an object becomes eligible for garbage collection in Java on following cases:

  1. Any instances that cannot be reached by a live thread.
  2. Circularly referenced instances that cannot be reached by any other instances.
  3. If an object has only lived weak references via WeakHashMap it will be eligible for garbage collection.
  4. The object is created inside a block and reference goes out scope once control exit that block.
More on... Eligible For Garbage Collection

Does GC guarantee that a program will not run out of memory?

No, Garbage collection (GC) does not guarantee that a program will not run out of memory. It is the developer's responsibility to ensure that objects no longer in use are no longer referenced by the application. That way the garbage collector can do its job and reclaim memory used by these objects.

More on.... Does garbage collection guarantee

Does assigning objects to null in Java impact garbage collection?

Not necessarily. An object becomes eligible for garbage collection when there are no live threads anymore that hold a reference to the object. Explicitly setting a reference to null instead of just letting the variable go out of scope, does not help the garbage collector , unless the object held is very large.

More on.... null in Java impact garbage collection