• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Garbage Collection in Java

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone...lets half some comments on Garbage Collection in java. I have read it so many times at so many places that programmer can't do much about it. It is all dependent on the JVM
well when i pick this option in the some of the mock exams, my answere is declared wrong. WHY.
For example
Which of the following statements related to Garbage Collection are correct. Select all correct answers.
A. It is possible for a program to free memory at a given time.
B. Garbage Collection feature of Java ensures that the program never runs out of memory.
C. It is possible for a program to make an object available for Garbage Collection.
D. The finalize method of an object is invoked before garbage collection is performed on the object.
Please help me sort this confusion.
Thanks
Nasir
 
Ahmed Nasir
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add to my previous post..The Question copied is from the exam on javaprepare site..So please don't trash the post this time. as I didn't know the rules. Now I just read that the exam has to be mentioned.
Thanks
Nasir
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahmed Nasir,
The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in the background by a low-priority thread that is referred to as the garbage collector. When the garbage collector finds memory that is no longer accessible from any live thread, it takes steps to release it back into the heap for re-use
1. it is possible for a program to free memory at any given time by running garbage collector. the methods used are System.gc() and Runtime.gc().
2.garbage collector cannot ensure that a program runs out of memory.
3.it is possible for program to make an object available for garbage collection by assigning a null value to the object reference.
4.and the last condition is also true
 
Sheriff
Posts: 9103
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sasidandi,
I disagree with your first answer. Although you can call the garbage collector as you said, it is a low priority thread and may not run at the time you call it. Therefore "at a given time" makes "A" an incorrect answer.
I agree with what you intended to say in your answer to #2
2.garbage collector cannot ensure that a program does not run out of memory.
Marilyn
 
Ranch Hand
Posts: 275
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You cant kill an object by killing its handle. Unknown to you
the object still persists in the background. Killing the handle is superfluous as you would only kill it when you no longer require it. In any case your object handle would have gone out of scope releasing the lock on the object and thereby making the object eligible for garbage collection . Or you can use Bruce Eckel's method of forcing the garbage collector to kick in. Create some huge strings or stuff like that and make the garbage collector nervous about memory.
Rgds
Sahir
 
sasidandi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marilyn,
i agree with u ,it is not possible to run the garbage collector AT A GIVEN TIME. but it is possible to run garbage collector before the VM does it in its own.
i agree with u too Sahir. what i meant was when an object no longer has an reference in any part of the program the it can be garbage collected.
sasidandi
 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some please help with this problem from Sun tutorial...
Question: The following code creates one Point object and one Rectangle object. How many references to those objects exist after the code executes? Is either object eligible for garbage collection?
...
Point point = new Point(2,4);
Rectangle rectangle = new Rectangle(point, 20, 20);
point = null;
...
Answer: There is one reference to the Point object and one to the Rectangle object. Neither object is eligible for garbage collection.
I thought that when an object is set to null, then it becomes eligible for garbage collection?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If u look closer on this line...
Rectangle rectangle = new Rectangle(point, 20, 20);
So reference to point does exists!
Jeban.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to remember when you pass an argument to a method or class as part of its constructor, you are passing by reference. So eventhough you set point = null, you created another reference for it when you passed it to the Rectangle object.
As for the original question, I agree that A and B is not correct since you can't guarantee that GC will run even when you call it as Marilyn pointed out, and we all agree that B is not correct. I also agree that C and D are correct. So, what was the final answer though since some people have some disagreement over answer A?
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
As We know by the concept that
"The runtime system keeps track of the memory that is allocated and is able to determine
whether that memory is still useable. This work is usually done in the background by a
low-priority thread that is referred to as the garbage collector."
So the answer 1> is could not be correct because of the TIME FACTOR"AT A CERTAIN TIME".
I agrred with the following concept
"it is possible for a program to free memory at any given time by running garbage
collector. the methods used are System.gc() and Runtime.gc()."
But What is the guarantee that Garbage Collectore can be called at the certain time.

IMHO , option 3 & 4 are the correct answer.


3.it is possible for program to make an object available for garbage collection by
assigning a null value to the object reference.
4.and the last condition is also true

If anyone has objection about it, Pl. correct me.

HTH
Jaydeep
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to throw a bit of confusion into number 4: you should remember that there is no guarantee that an object will be individually garbage collected. Your object's memory could be freed without finalize() ever running.
From Thinking in Java:

Your objects might not get garbage-collected. You might find that the storage for an object never gets released because your program never nears the point of running out of storage. If your program completes and the garbage collector never gets around to releasing the storage for any of your objects, that storage will be returned to the operating system en masse as the program exits. This is a good thing, because garbage collection has some overhead, and if you never do it you never incur that expense.


So it is true that your object's finalize() method will run before your object is garbage collected BUT there is no guarantee that your object will ever be garbage collected even when you no longer reference it. And if it isn't garbage collected, but instead is returned en masse, your finalize() will never run. Java 1.1 introduced the method System.runFinalizersOnExit( ) but this turned out to be a buggy solution and was deprecated in Java 1.2.
[This message has been edited by Thomas Paul (edited October 12, 2000).]
 
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic