• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

garbage collection

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
although garbage collection is explained well in K & B but still I am getting confused in it.

Suppose a situation when we are making a reference null, then there should be only one object eligible for null.
but why it is showing that more than one object(incase that object itself contains link to other int or short) eligible for null.

all the question related to garbage collection is confusing me in one way of its answer.

Thanks
 
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont think that more objects will be eligible for gc if the object itself contains int or short.
However if the object contains references to other objects then those will also be eligible for GC since there is no way of reaching them them if you remove the reference to the enclosing object by setting its reference to null.

"An object is eligible for gc when no live thread can reach it" - Quote from K&B

Update:

Here is an example

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
inspecting test chapter 3 question 11:

class Alpha{
static Beta b1;
Beta b2;
}

When it does

Alpha a1=new Alpha();
a1=null;

when it assigns a1=null, it implicitly assigns all a1's members variables to null too, so a1.b1 and a1.b2 loss their references
 
Malte Wannerskog
Ranch Hand
Posts: 92
PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mr.cazurro greenlins wrote:inspecting test chapter 3 question 11:

class Alpha{
static Beta b1;
Beta b2;
}

When it does

Alpha a1=new Alpha();
a1=null;

when it assigns a1=null, it implicitly assigns all a1's members variables to null too, so a1.b1 and a1.b2 loss their references



However, remember that in the question only 1 object is eligible for gc since b1 can still be accessed through a2 since b1 is a static variable.
 
Prakash Mahto
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Mr. greenlins
when we had done with
a1 = null;
We are eligible for one gc as only A object is there.
We have no b1 and b2 object here. there is only reference for them.

What is the benefits/loss in the question if b1 is static variable.
all those are only reference not real time objects on heap.

I am getting confused again in it. Please clarify
Thanks
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Prakash,

One of the big reasons that GC is on the exam, is that if you're not careful, your BIG JAVA PROGRAM, might not ever give the garbage collector objects to collect. So it's important to understand ways in which you can release objects when you're done with them AND it's important to understand bugs that can occur when you think you've released an object, but you really haven't.

So, see if you can answer this question: "What's the lifetime of a static variable?" See if that answer helps you understand this question.
 
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