• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

garbage collection

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi i got it from technopark suncertification(Developed by: Giri Mandalika).i dont know the exact website address since i found it through sun discussion forum.

here is the program,
class J
{
static int notFinalized;
public static int notFinalized()
{return notFinalized;}
private K k;
private int name;
public int name()
{return name;}
public J(K k, int i)
{this.k = k; name = i; notFinalized++;}
public void finalize()
{
synchronized (k)
{ System.out.println(J.notFinalized);
System.out.print(name);
notFinalized--;
k.notify();
}
}}

class K
{
private void m1()
{
J j = null;
for (int i = 0; i < 5; i++)
{
j = new J(this, i); // 1
}
Runtime.getRuntime().gc(); // 2
synchronized (this) {
System.out.println("j=" +j.notFinalized); it's giving 5...?!!! and i thought it supposed to give decremented value(i.e 2) of notFinalized which has been done in finalize method.
while (J.notFinalized() > 0)
{
try {wait();}
catch
(InterruptedException ie)
{}
}
}
}
public static void main(String[] args)
{
new K().m1();


}}

output is j=5
5
34
23
12
0
please consider this question. i am being really honest to this forum policy.
i will try to find this website address and will post it for others to use.


Thanks in advance
Preetha
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Preetha Arun ,

First of all, You cannot ORDER the GC to do its work, YOU CAN ONLY REQUEST.
that is the reason you are getting J= 5.
Try running the program few time...
MAY BE you would get the expected result some time.
And if it does the job at the right time, it would print j=1 and not 2.
Try printing like this ...for better understanding.
System.out.println("NUMBER:"+J.notFinalized);
System.out.println("NAME:"+name);


And i STRONGLY reccommend to use code tags.

[ December 03, 2008: Message edited by: James Tharakan ]
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess output of this program cant be predicted.
Because calling gc() doesnt guarantee that objects which are eligible for gc are garbage collected(i.e. finalize() method is called), we cant say what the output will be at runtime.

Try running the program several times, you might see the difference in the program!
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this question is from mockexam-10 SOURCE:www.javabeat.net

When the processing of line 2 begins how many objects of type J that were created at line 1 are
eligible for garbage collection?
(1) 0
(2) 1
(3) 4
(4) 5
(5) Can not be determined without more information
(6) Compiler error
(7) Run time error
(8) None of the above

answer is:3
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If gc identifies some objects which need to be finalized the finalization may not be done immediately.

Runtime.runFinalization() may help.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic