Forcing Garbage Collection to Avoid Memory Leaks in Oracle's JVM
Forcing Garbage Collection to Avoid Memory Leaks in Oracle's JVM
By Megh Thakkar
One of the best features of Java is that it performs automatic garbage collection, or memory management. In this article, Oracle expert Megh Thakkar demonstrates how you can force garbage collection to occur when you need it.
One of the best features of Java is that it performs automatic garbage collection, or memory management. Other languages such as C++ don't perform garbage collection automatically; instead, they leave this task to the developers. Generally, the Java VM performs garbage collection when it needs more memory to continue execution. Even though you might write a program that loses an object reference, thereby making it a candidate for garbage collection, there is no guarantee that the garbage collection will occur at that time. This can be a problem for systems with memory constraints. This article demonstrates how you can force garbage collection to occur when you need it.
Java makes it possible for you to force garbage collection when needed. It is a simple process that involves using the Runtime object.
NOTE
Runtime objects enable you to interface with the environment in which your application is running.
The following Java code demonstrates how to force garbage collection.
Listing 1: Forcing Java Garbage Collection
/* * This Java program shows you how to force garbage collection * Author: Megh Thakkar */ public class CollectGarbage { int ASIZE = 1000000; void useMemory() { int[] intA = new int[ASIZE]; for (int i=0; i<ASIZE; i++) { intA[i] = i*2; } } public static void main (String[] args) { CollectGarbage gct = new CollectGarbage(); // Get a Runtime object Runtime r = Runtime.getRuntime(); // Collect garbage at the start of the program r.gc(); // Let's see how much memory we have at the start long availMem = r.freeMemory(); System.out.println("At program start we have : " + availMem + " bytes"); // Let's use some memory gct.useMemory(); // Let's see how much memory is left long availMem1 = r.freeMemory(); System.out.println("After running the program, we have : " + availMem1 + " bytes"); // Collect garbage r.gc(); //Let's see what we have now long availMem2 = r.freeMemory(); System.out.println("After collecting garbage we have : " + availMem2 + " bytes"); long freedMem = availMem2 - availMem1; System.out.println("Garbage collection freed : " + freedMem + " bytes"); } }
As you can see from this Java code, there are two main steps involved in forcing garbage collection:
- Create a Runtime object.
- Invoke the garbage collector gc() method of the Runtime object.
About the Author
Megh Thakkar is the Director of Database Technologies at Quest Software in Australia. Previously, he worked as a technical specialist at Oracle Corporation. He holds a master's degree in computer science and a bachelor's degree in electronics engineering. Megh also holds several industry vendor certifications, including OCP, MCSE, Novell Certified ECNE, and SCO Unix ACE, and he is a Lotus Notes Certified Consultant. He is a frequent presenter at Oracle OpenWorld and various international Oracle User Groups.
Megh is the author of E-commerce Applications Using Oracle8i and Java from Scratch and SAMS Teach Yourself Oracle8i on Windows NT in 24 Hours. He has also co-authored several books, including Migrating to Oracle8i, Special Edition Using Oracle8/8i, Oracle8 Server Unleashed, C++ Unleashed, COBOL Unleashed, Oracle Certified DBA, and Using Oracle8. Megh is a renowned Oracle specialist who has performed Oracle development, consulting, support, and DBA functions worldwide over the past 10 years.