Results 1 to 4 of 4
- 03-19-2011, 06:13 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
How can I see the impression of the Garbage Collector?
Hello Experts,
I want to get acquainted with the Garbage Collector.
I'm pretty new to Java, so far I have written very simple programs. I 'm using the System.gc() statement in my codes, but I can't see the Garbage Collector's influence.
Can someone give me a little code or explaination where I can see clearly the impact of the garbage collector? Is there any function to display the unreferenced objects or memory usage?
At the moment I try to test the functionality of the gc with the following code:
Java Code:import java.util.PriorityQueue; import java.util.Queue; import java.util.Iterator; public class Garbage{ public static void main(String[] args) { Queue qu = new PriorityQueue(); for (int i=0;i< 100;i++) { String str=new String(Integer.toString(i)+"pear"); qu.add(str); [B]System.gc(); [/B] } Iterator it = qu.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
I would be very grateful for any help! :)
- 03-19-2011, 06:36 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
GC is a very mysterious thing in JAVA. :D Calling a function System.gc() only advices the GC to run. Java Virtual Machine decides when to run it by it self. You really should not worry about gc.
Last edited by FlyNn; 03-19-2011 at 06:44 PM.
Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-01-2011, 11:30 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 20
- Rep Power
- 0
Dear FlyNn,
Thank you very much for your quickly answer. GC has got a truly mysterious behaviour:D
At the moment I can find a small utility which gives information about our heap. (But heap /stack is not really clear to me yet:) )
If you run java program from command line, you will specify the -verbose:gc specifier.
for example: java -verbose:gc ForumGC
When you call the System.gc function in your code, the program prints information about your heap.
I test it with the following small code:
Java Code:import java.util.Queue; import java.util.PriorityQueue; public class ForumGC{ public static void main(String[] args) { Queue chain = new PriorityQueue(); chain.offer("apple"); chain.offer("walnut"); chain.offer("pear"); System.out.println("list size: " + chain.size()); System.out.println("call gc"); System.gc(); chain.poll(); chain.poll(); System.out.println("list size: " + chain.size()); System.out.println("call gc"); System.gc(); chain.offer("plum"); chain.offer("cherry"); chain.offer("raspberry"); chain.offer("strawberry"); System.out.println("list size: " + chain.size()); System.out.println("call gc"); System.gc(); }}
list size: 3
call gc
[Full GC 277K->119K(15744K), 0.0147829 secs]
list size: 1
call gc
[Full GC 208K->119K(15872K), 0.0113771 secs]
list size: 5
call gc
[Full GC 208K->119K(15872K), 0.0112291 secs]
My code is too small to show the effects of gc. But I guess, this utility might be useful in large programs.
Best regards
- 04-01-2011, 02:34 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Similar Threads
-
Garbage Collector tuning
By javaOrC in forum Advanced JavaReplies: 45Last Post: 03-02-2011, 11:51 PM -
In defense of the Garbage Collector
By Katanagas in forum Advanced JavaReplies: 2Last Post: 10-25-2010, 06:40 PM -
Q about Garbage Collector
By m00nchile in forum New To JavaReplies: 4Last Post: 02-05-2010, 05:57 AM -
Garbage Collector and finalize()
By arefeh in forum New To JavaReplies: 5Last Post: 01-09-2010, 09:04 PM -
Garbage collector and its impacts
By RadhaJayalakshmi in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 11:56 AM
Bookmarks