| Author |
Garbage collection example code from K&B
|
rengarajan vaikuntam
Ranch Hand
Joined: Oct 04, 2004
Posts: 37
|
|
The code: This import java.util.Date; public class Garbage { public static void main(String as[]) { Runtime rt = Runtime.getRuntime(); System.out.println("Toatl JVM Memory"+rt.totalMemory()); System.out.println("Memory Before Creating Objects"+rt.freeMemory()); Date d = null; for(int i=0;i<10000;i++) { d=new Date(); d=null; } System.out.println("Memory After Creating Objects"+rt.freeMemory()); rt.gc(); System.out.println("Memory After GCing the Objects"+rt.freeMemory()); } } The output in my system is: Toatl JVM Memory 2031616 Memory Before Creating Objects 1868176 Memory After Creating Objects 1627208 Memory After GCing the Objects 1930304 My doubt is Memory before creating objects is 1868176 but Memory after gcing the objects is 1930304. How there is a increase in the memory? Thanx.
|
 |
r phipps
Ranch Hand
Joined: Sep 14, 2004
Posts: 60
|
|
|
I would assume that there was other stuff that was running in the Background that also freed up memory.
|
 |
r phipps
Ranch Hand
Joined: Sep 14, 2004
Posts: 60
|
|
I see what you mean, i ran it and got the following. Toatl JVM Memory2031616 Memory Before Creating Objects1913032 Memory After Creating Objects1672064 Memory After GCing the Objects1945080 Press any key to continue . . .
|
 |
rengarajan vaikuntam
Ranch Hand
Joined: Oct 04, 2004
Posts: 37
|
|
Phipps I dont see anything like Press any key to continue... in my system. What is this? What im asking is how it could get more memory after gc? Thanks.
|
 |
Joe Borderi
Ranch Hand
Joined: Oct 23, 2004
Posts: 151
|
|
Try running this code: Run this code both with and without the call to gc() commented out. I hope this answers your question.
|
 |
Manikandan Jayaraman
Ranch Hand
Joined: Sep 15, 2004
Posts: 225
|
|
Borderi! The output displays change in free memory as '0' for me. That sounds good, AN IDEAL PROGRAM. The steps you have follwed: 1. You are calling gc initially, to do the garbage collection, before you start creating your objects on the heap. 2. Calculating the free memory before you start creating your objects. 3. Create 10000 objects of Date class. 4. Call gc() again. 5. Compute the change in free memory. But, The output may not be '0' always, as, you calling gc() doesnt mean JVM executes your command for sure! I think, if we all login as different users in one server (server's heap is one single chunk of memory? ) and execute this code, output would be unpredictable rather!
|
Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
|
 |
Joe Borderi
Ranch Hand
Joined: Oct 23, 2004
Posts: 151
|
|
"But, The output may not be '0' always, as, you calling gc() doesnt mean JVM executes your command for sure! " Yes, the output is indeterminate regardless. In this case the call to Thread.sleep() seems to allow the garbage collector thread to run. According to the Java spec, however, this is not guaranteed. If you comment out the first call to Thread.sleep(), you may see this.
|
 |
 |
|
|
subject: Garbage collection example code from K&B
|
|
|