Results 1 to 6 of 6
Thread: Garbage Collector and finalize()
- 01-09-2010, 02:40 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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] } }
- 01-09-2010, 02:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-09-2010, 04:09 PM #3
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.
- 01-09-2010, 08:43 PM #4
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
Hello
I get this error:What is the exact error you receive?....
Java Code:can not make static reference to method void gc() in class java.lang.Runtime.
This program is just for practice. when i read a new subject and did not get that result, it became a problem for me.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 is also a question for me that is manual garbage collection is necessary in java?
- 01-09-2010, 09:00 PM #5
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.This is also a question for me that is manual garbage collection is necessary in java?
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.
- 01-09-2010, 09:04 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
finalize keyword
By bugger in forum New To JavaReplies: 6Last Post: 10-01-2008, 12:28 PM -
Garbage collector and its impacts
By RadhaJayalakshmi in forum Advanced JavaReplies: 1Last Post: 07-23-2008, 11:56 AM -
Interacting with the Java Garbage Collector
By Java Tip in forum Java TipReplies: 0Last Post: 03-28-2008, 08:04 PM -
How to use the garbaje collector
By Eric in forum Advanced JavaReplies: 2Last Post: 06-29-2007, 01:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks