• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did an exercice and I do not agree with the answer (assume that the Teste class is correct)



In what line the object refer by t may be garbage collected???
Line 1 or only when the method main finish? The response of exercice said after line 1 completes...

But think that: and if in method print() of t, the object is assign to another reference variable with use of "this" ???
 
Greenhorn
Posts: 28
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.
you must not think what print() method do. The line 1 is eligible for garbage collected. And also this is not a variable. You cant write this=null; or this= new Test();

thanks..
[ December 29, 2007: Message edited by: ilkin esrefli ]
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alexsandra Carvalho:
But think that: and if in method print() of t, the object is assign to another reference variable with use of "this" ???



Howdy, Le.

The answer is correct, after line one completes, the "t" object will be ready to be garbage collected.
If I understood what you said, I think you want to say something like this:



Is this it? If so, then, as soon as this method finishes, the "hello" object will be gone, since it is local.
After that, when you say "t = null;", in this case, there are no other references to the "new Teste()" object. So, it may be garbage collected.
To recommend JVM to collect this garbage, you can either call System.gc(); or Runtime.getRuntime().gc(); (it's just a recommendation, this will not force the VM to collect this garbage).
 
Alexsandra Carvalho
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto,

I think you are rigth. I think that indeed does not exists a situation written by me.

Thanks!!!
Alexsandra
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic