Results 1 to 4 of 4
  1. #1
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default 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());	
    		}
             }
    }
    How can I tranform this code to see the impression of gc?

    I would be very grateful for any help! :)

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    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.

  3. #3
    Zamioculcas is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    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();
           }}
    the output:

    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

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default

    There are profilers and heap dump tools (and heap analysers) that are far more useful than sticking that sort of code into your system.

Similar Threads

  1. Garbage Collector tuning
    By javaOrC in forum Advanced Java
    Replies: 45
    Last Post: 03-02-2011, 11:51 PM
  2. In defense of the Garbage Collector
    By Katanagas in forum Advanced Java
    Replies: 2
    Last Post: 10-25-2010, 06:40 PM
  3. Q about Garbage Collector
    By m00nchile in forum New To Java
    Replies: 4
    Last Post: 02-05-2010, 05:57 AM
  4. Garbage Collector and finalize()
    By arefeh in forum New To Java
    Replies: 5
    Last Post: 01-09-2010, 09:04 PM
  5. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •