Results 1 to 6 of 6
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default Garbage Collector and finalize()

    Hello
    I used from Garbage Collector and finalize(), but System.out.println("Error: checked Out"); does not run.
    Java Code:
    class Book
    {
    	boolean checkedOut=false;
    	
    	Book(boolean checkOut)
    	{
    		checkedOut=checkOut;
    		System.out.println("checkedOut is= "+checkedOut);
    	}
    	void checkIn()
    	{
    		checkedOut=false;
    	}
    	protected void finalize()
    	{
    		if(checkedOut)
    		{
    			System.out.println("Error: checked Out");
    		}
    	}
    }
    public class TerminationCondition
    {
    	public static void main(String args[])
    	{
    		Book ob = new Book(true);
    		ob.checkIn();
    		new Book(true);
    		System.gc();
                              [b]//Runtime.gc();has Error![/b]
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I used from Garbage Collector and finalize(), but System.out.println("Error: checked Out"); does not run.
    Java Code:
    System.gc();
    [b]//Runtime.gc();has Error![/b]
    What is the exact error you receive? Please don't make us guess; be as exact as possible without guessing yourself.

    kind regards,

    Jos

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    Also, may I ask why you feel that you need to perform a manual garbage collection cycle? Unless this is just a quick example from a much bigger program, there isn't any reason I can think of that would necessitate calls to garbage collection in this program.

    Usually you System.gc(); in between timed tests or when you're working with many megs of data, and even then its generally not required.

  4. #4
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    What is the exact error you receive?....
    I get this error:
    Java Code:
    can not make static reference to method void gc() in class java.lang.Runtime.
    Also, may I ask why you feel that you need to perform a manual garbage collection cycle? Unless this is just a quick example from a much bigger program, there isn't any reason I can think of that would necessitate calls to garbage collection in this program.

    Usually you System.gc(); in between timed tests or when you're working with many megs of data, and even then its generally not required.
    This program is just for practice. when i read a new subject and did not get that result, it became a problem for me.
    This is also a question for me that is manual garbage collection is necessary in java?

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    This is also a question for me that is manual garbage collection is necessary in java?
    Rarely. I wrote an app that was loading 50+ large images into memory as buffered images, which can rapidly use 1+ gigs of ram. The images were used quickly and discarded, but the garbage collection did not happen rapidly enough to make way for new images in a timely fashion. I made a call to System.gc() in between sets of images to force the collection to occur - this greatly reduced the memory footprint and increased performance.

    However, other than in a few very specific test cases (trying to accurately time certain algorithms), I've never used it otherwise. Calling it frequently actually slows your program down, since frequent garbage collection cycles have a lot of overhead.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    I get this error:
    Java Code:
    can not make static reference to method void gc() in class java.lang.Runtime.
    Yes, you didn't read the API documentation for the Runtime class close enough; do this instead:

    Java Code:
    Runtime.getRuntime().gc();
    kind regards,

    Jos

Similar Threads

  1. finalize keyword
    By bugger in forum New To Java
    Replies: 6
    Last Post: 10-01-2008, 12:28 PM
  2. Garbage collector and its impacts
    By RadhaJayalakshmi in forum Advanced Java
    Replies: 1
    Last Post: 07-23-2008, 11:56 AM
  3. Interacting with the Java Garbage Collector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 08:04 PM
  4. How to use the garbaje collector
    By Eric in forum Advanced Java
    Replies: 2
    Last Post: 06-29-2007, 01: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
  •