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:
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
- Automatic deallocation allows a programmer not to worry about memory management, increasing write ability of a system, and decreasing development time and costs.
- It is automatically done by the garbage collector(a part of JVM) so we don't need to make extra efforts.
- Garbage collection is a language requirement for functional languages, which cannot use a stack-based environment because of unpredictable execution patterns.
- Explicit management introduces possibilities for making errors in memory management - for example, memory leaks. Thus, explicit deallocation decreases reliability.
- Java Interview Questions-Core Faq
- Important features of Java
- What is the difference between JDK and JRE?
- What gives Java its 'write once and run anywhere' nature?
- What is JVM and is it platform independent?
- What is Just-In-Time (JIT) compiler?
- What is NullPointerException in Java
- Difference between Stack and Heap memory in Java
- How to set the maximum memory usage for JVM?
- What is numeric promotion?
- Why do we need Generic Types in Java?
- What does it mean to be static in Java?
- What are final variables in Java?
- How Do Annotations Work in Java?
- How do I use the ternary operator in Java?
- What is instanceof keyword in Java?
- How ClassLoader Works in Java?
- What are fail-safe and fail-fast Iterators in Java
- What are method references?
- "Cannot Find Symbol" compile error