Solved

Garbage Collection in Java, Could you please make corrections for my HEAP/STACK diagram?

Posted on 2011-02-26
5
523 Views
Last Modified: 2012-05-11
This is a question about garbage collection. I want to construct a HEAP/STACK diagram for the code given below to determine the number of Objects created, and number of Objects eligible for garbage collection.

I have already drawn the HEAP/STACK diagram, which is still incomplete. The locations assigned for each objects are randomly given e.g. 100,200,300.  

1. When the line “//do stuff” is reached, how many objects will be eligible for garbage
    collections? This correct answer is only one object is eligible but I need to know why?
2. Could you please make corrections for the HEAP/STACK diagram I constructed so that I could
    get the correct answer?
3. I am stuck with “static Beta b1”, since I am not use how to use static reference variable in the
    HEAP/STACK diagram.


class Beta {}
class Alpha
{
   static Beta b1;
   Beta b2;
}

public class Tester
{
   public static void main(String [] args)
   {
      Beta b1 = new Beta();
      Beta b2 = new Beta();
      Alpha a1 = new Alpha();
      Alpha a2 = new Alpha();
     
      a1.b1 = b1;
      a1.b2 = b1;
      a2.b2 = b2;
     
      a1 = null;
      b1 = null;
     b2 = null;
      //do stuff
   }
}

The Incomplete HEAP/STACK Diagram:

HEAP
100: Beta referenceBy b1
200: Beta referenceBy b2
300: Alpha referenceBy a1
400: BetaObject referenceBy 300
500: Alpha referenceBy a2
600: BetaObject referenceBy 500

STACK
b1 : 100
b2 : 200
a1 : 300
a2 : 500

Could you please make corrections to my diagram above, so that I could determine the number of objects eligible for Garbage Collection?

Thank you in advance :)
0
Comment
Question by:techbro
  • 2
  • 2
5 Comments
 
LVL 26

Expert Comment

by:dpearson
Comment Utility
This looks like it could be a homework problem to me - which we don't do here.

But to answer your question of how a static reference in a class works this will create a single reference to a single object that is shared by all references to the class.  So *any* Alpha object in your case will be able to access Alpha.b1 (note how you can use the class's name rather than the variable's name like a1.b1).

I hope that helps you figure out the garbage collection logic.

Doug
0
 

Author Comment

by:techbro
Comment Utility
Hi Doug,

Appreciate your response.

I have learnt that every Alpha object constructed, objects inside the Alpha are also created.
For instance, in the code below, Can I conclude that every time "new Alpha" is used to create an Alpha Object, two Beta Objects are created as well (total 3 objects are constructed)?

class Alpha 
{
   Beta b1;
   Beta b2;
}

Open in new window


You mentioned that a static reference in a class will create a single reference. My Second Question is, how how many objects are created when "new Alpha" is used to construct an Alpha object where "static Beta b1" is inside it? . Will the code create only 2 Objects unlike the first code?

class Alpha 
{
   static Beta b1;
   Beta b2;
}

Open in new window


My Third Question, The reference variable "b1" and "b2" used for constructing "new Beta()" Objects in my previous problem. Is b1 and b1 the same reference variable "static Beta b1" and "Beta b2" declared inside Alpha Object?

public class Tester
{
   public static void main(String [] args)
   {
      Beta b1 = new Beta();
      Beta b2 = new Beta();
      Alpha a1 = new Alpha();
      Alpha a2 = new Alpha();
   }
}

Open in new window


This is not a homework. I am studying for a certification exam - "Oracle Certified Professional, Java SE 6 Programmer". I approached this GC problem in the book written by Sierra & Bates. GC concept is an important concept for me to understand to score more in the exam.

Thanks Again,
Morshed


0
 
LVL 5

Expert Comment

by:jaiminpsoni
Comment Utility
1. Can I conclude that every time "new Alpha" is used to create an Alpha Object, two Beta Objects are created as well (total 3 objects are constructed)?

No, when you use new Alpha, only Alpha object is created, B1 and B2 are initialized to NULL.

2. My Second Question is, how how many objects are created when "new Alpha" is used to construct an Alpha object where "static Beta b1" is inside it?

Again, there is no difference, when you create Alpha object, only one object is creared.

3. The reference variable "b1" and "b2" used for constructing "new Beta()" Objects in my previous problem. Is b1 and b1 the same reference variable "static Beta b1" and "Beta b2" declared inside Alpha Object?

They are certainly different.
b1 and b2 in main are local references to main. The one inside Alpha objects are member variables of Alpha.


And, there will certainly be 1 object elligible for Garbage collection.

And that only object will be A1.
0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 250 total points
Comment Utility
As jaiminpsoni has explained in Java objects are *only* created through a call to new.  If there's no "new X" line, nothing is being allocated and no objects are being created.

This is quite hard to grasp if you're used to C or C++, but in Java this is always the rule.  It's one way it makes the language simpler to understand.

The tricky part of this problem is that after these lines:
      a1 = null;
      b1 = null;
     b2 = null;

It's clear that only a2 remains and points to an object (an Alpha).  So you might be tempted to believe that a1, b1 and b2 can be garbage collected.  But that's not correct.

It's not hard to see that b2 won't be garbage collected because of this line:
   a2.b2 = b2;

so the object a2 has reference (via its b2 member) to the object b2.  It's not helpful that these have the same names, it might be clearer if this was
  a2.mychild = b2 ;

Anyway I hope you can see that b2 is still referenced.

What about b1?  This line:
    a1.b1 = b1;
sets the static member variable b1 to the object b1.

If this was NOT a static variable, then after
   a1 = null;
both a1 and b1 would be available for garbage collection.

But because it's a static variable it's stored with the Alpha class - not the a1 instance of that class.  And since the class is still referenced (classes once loaded generally never go away) it will keep the variable b1 in memory too.

I hope that all makes sense,

Doug

P.S. Sorry I thought this might be homework.  Studying for an exam is different :)
0
 
LVL 5

Accepted Solution

by:
jaiminpsoni earned 250 total points
Comment Utility
The diagram will look like this...

100: Beta referenceBy b1
200: Beta referenceBy b2
300: Alpha referenceBy a1
100: BetaObject referenceBy 300
500: Alpha referenceBy a2
100: BetaObject referenceBy 500 (As its static)
200: BetaObject referenceBy 500

So, the only reference elligible for GC is 300. All other objects will be used by a2.

Hope this helps.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now