This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection example code from K&B

 
rengarajan vaikuntam
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would assume that there was other stuff that was running in the Background that also freed up memory.
 
r phipps
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Joe Borderi
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic