import java.util.*;
class GarbageCollection
{
public static void main(String s[]) throws Exception
{
Runtime rs = Runtime.getRuntime();
System.out.println("Free memory in JVM before Garbage Collection = "+rs.freeMemory());
rs.gc();
System.out.println("Free memory in JVM after Garbage Collection = "+rs.freeMemory());
}
}
Program Explanation: GarbageCollection
This program shows how to manually call garbage collection in Java using the Runtime class. It also shows how to check the free memory in the JVM before and after garbage collection.
Step-by-Step Breakdown:
1. Importing the java.util package:
import java.util.*;
The package for importing all utility classes of Java, java.util is imported, although it’s not strictly necessary since we’re making heavy usage of the Runtime class in this program.
2. Class Definition:
class GarbageCollection
We define a class with the name GarbageCollection. Within the class, we have the main method with which the execution of the program starts.
3. Main Method:
public static void main(String s[]) throws Exception
The main method is the entry of the program. It is written as throws Exception, meaning that this method can throw exceptions, but no explicit exception handling is required in this program.
4. Obtaining a Runtime Object:
Runtime rs = Runtime.getRuntime();
- Runtime.getRuntime() returns the runtime object of the Java application as of the current moment. The class Runtime enables access to the JVM and provides methods to manage the runtime environment.
- We save this runtime object in the variable rs for later use.
5. Print Free Memory Before Garbage Collection:
System.out.println(“Free memory in JVM before Garbage Collection = “+rs.freeMemory());
- The freeMemory() method of the Runtime class returns the amount of free memory available to the JVM at the time this method is invoked.
- This statement prints the free memory in the JVM before the garbage collection process is performed.
6. Invoke Garbage Collection
rs.gc();
- The gc() method is invoked on a Runtime class to pass a hint for the JVM that it should gc.
- A hint, not a request, it never guarantees that, in fact, garbage collection really will happen sooner rather than later. However, it is a recommendation to free-up memory for unreferenced objects.
- After calling the rs.gc(), the JVM will try to garbage collect unreferenced objects and free up memory.
7. Print Free Memory After Garbage Collection:
System.out.println(“Free memory in JVM after Garbage Collection = “+rs.freeMemory());
- Next, call the freeMemory() method again after garbage collection to see how much memory is available to the JVM.
- This line prints the free memory after garbage collection has occurred.
What Happens When the Program Executes:
- First, it will print the available memory in the JVM before doing garbage collection.
- Then it invokes the method rs.gc() that probably tells the JVM to do the garbage collection.
- Finally, it prints out the amount of free memory after garbage collection has been performed.
Expected Output:
The real output will differ with the state of the JVM and the memory of the system. However, the format of the output is something like this:
Free memory in JVM before Garbage Collection = 10245678
Free memory in JVM after Garbage Collection = 10246640
- Before Garbage Collection: The program is likely to print out the amount of free memory available to the JVM before calling the garbage collector.
- After Garbage Collection: The program will print out the number of free memory available to the JVM after garbage collection. It may or may not be higher than before depending on whether the JVM was able to reclaim memory.
Key Points:
- freeMemory(): This method returns the amount of free memory in the JVM at the time it is called.
- gc(): The gc() method invokes garbage collection but does not guarantee that it will occur immediately or at all.
- The memory usage reported may not change much depending on how much memory is actually reclaimed by the garbage collector.
Conclusion:
- The program shows how to interact with the JVM’s memory management using the Runtime class.
- Garbage collection is the process in Java to remove unused objects and free up memory. However, garbage collection is not something that can be forced on the JVM with 100% certainty. This program shows how to invoke garbage collection and how available memory will change.