Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

Garbage Collection in Java, Could you please make “corrections” and provide “clear” explanations for my HEAP/STACK diagram?

Question:
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 not giving me the right answer . The locations assigned for each objects are randomly given e.g. 100,200,300.
Could you please make “corrections” and provide “clear” explanations for the HEAP/STACK diagram?

Thank you in advance :)


Java Code Given:

class Dozens
{
   int [] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
}

public class Eggs
{
   public static void main(String [] args)
   {
      Dozens [] da = new Dozens[3];
      da[0] = new Dozens();
      Dozens d = new Dozens();
      da[1]= d;
      d = null;
      da[1] = null;
      //do stuff
   }
}

Which two are true about the objects created within main(), and eligible for garbage collection when line “//do stuff” is reached?
A.      Three objects were created
B.      Four objects were created
C.      Five objects were created
D.      Zero objects are eligible for garbage collection
E.      One object is eligible for garbage collection
F.      Two objects are eligible for garbage collection
G.      Three objects are eligible for garbage collection


My Diagram:
HEAP
100: Dozens referenceBy da[]
200: Dozens referenceBy da[0]
300: IntegerArrayObject referenceBy 200
400: Dozens referenceBy d                   (GC Eligible)
500: IntegerArrayObject referenceBy 400      (GC Eligible)
600: Dozens referenceBy da[1]                  (GC Eligible)
700: IntegerArrayObject referenceBy 600      (GC Eligible)

STACK
da[] = 100
da[0] = 200
d = 400 (NULL)
da[1] = 600 (NULL)

Correct Answer: C & F from options
My Answer (Wrong): 7 Objects are created, and 4 Objects are eligible for Garbage Collection
0
techbro
Asked:
techbro
  • 3
  • 3
  • 2
2 Solutions
 
MogalManicCommented:
Is this homework?

The answer is c and f becaus there are only objects created when there is a new.  All you need to do is count the NEW's.

As for the object available for gc you count the NULL's, then for every object set to NULL count the 'new'ed objects inside the object. Since these objects don't allocate anything it is the same as the number of NULL's.
0
 
techbroAuthor Commented:
Hi MogalManic,

I really appreciate your response.
Since you mentioned that the "Objects" are only created when there is a "new".I just want to know how "new Dozens[3]" &  "new Dozens()" are different?

I know that:
new Dozens[3] is an Array Object which stores references of 3 Dozen Objects
new Dozens() is only one Object

When new Dozen() Object is created, another "Array Object", int [] dz, is created as well. So when  new Dozens[3] Object is constructed, why the the "Array Object", int [] dz, is not created?

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 a big setback for me to score my exam, so I want to eliminate all my doubts.

Thank you again,
Morshed
0
 
jaiminpsoniCommented:
When you create new Dozens[3], you just create an array object which contains 3 references.

It doesnt create the 3 Dozens object.

If you want to create those objects, you have to create those using new....

da[0] = new Dozens()
da[1] = new Dozens()


So, when you create array object, it just creates the array of references. Those references are null till you create new objects using new.

Hope this answers your question.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
MogalManicCommented:
jaiminpsoni is right!  I got caught in the "trick" question.  An array is a pointer to an "object", that contains multiple indexes.   Each index needs to be initialized either using static initializers ({}) or manually for each index ([]).

The two NEW's allocate on the heap, but also all of the array allocations are heap allocations (with or without the new).  Since there are three arrays (one in main and 2 in each instance of Dozens), the total of allocations is 5

As for the GC, when the items are set to NULL, it is the SAME object (d and da[1] refer to the same object).  So one Dozens object is "freed" and it's allocated array is "freed".
0
 
MogalManicCommented:
Here is a before and after picture of the heap and stack:
Clipboard01.png
Clipboard01.png
0
 
techbroAuthor Commented:
Thanks MogalManic and jaiminpsoni for your prompt response!
The concept of array allocated in heap makes sense to me.

I have one more question regarding garbage collecting an array.
Since MogalManic mentioned that da[] is pointing to a location, "100". Is it possible to make the entire Dozen Array be eligible for GC? How can I point the da[] to null instead of 100?


I know this is a syntax error and never compile if I try da[] = null.
0
 
jaiminpsoniCommented:
you need to do
da = null;
0
 
techbroAuthor Commented:
Thanks jaiminpsoni!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now