• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection

 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone help me out on what is happening in the code, Especially on line 10 specifically. The question was about how many objects get garbage collected by the program is complied and runned
Also does the reference variable on line 8 which is c1 point to null before it even reaches line 11.
I need an explanation to understand guys.

I took the code from Karthy Sierra and Bert Bates OCA/OCP Java study guide.
Any contribution will be highly welcomed and appreciated.
Thank you!!
JavaRanchGarbageCollection.PNG
[Thumbnail for JavaRanchGarbageCollection.PNG]
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:The question was about how many objects get garbage collected by the program is complied and runned
You don't know that. You can know how many objects are eligible to be garbage collected, but not the number how many were or will be collected.

Kelvin Okornoe wrote:Also does the reference variable on line 8 which is c1 point to null before it even reaches line 11.
I need an explanation to understand guys.
Start by telling us your thoughts, so we could either confirm or negate it.
Actually explain the quote above once again, just a bit clearer, as it isn't clear at the moment. Then please answer crossed off sentence (it is still readable).

 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Kelvin Okornoe wrote:The question was about how many objects get garbage collected by the program is complied and runned
You don't know that. You can know how many objects are eligible to be garbage collected, but not the number how many were or will be collected.

Kelvin Okornoe wrote:Also does the reference variable on line 8 which is c1 point to null before it even reaches line 11.
I need an explanation to understand guys.
Start by telling us your thoughts, so we could either confirm or negate it.
Actually explain the quote above once again, just a bit clearer, as it isn't clear at the moment. Then please answer crossed off sentence (it is still readable).



Hello Liutauras Vilda you are right, the question has to be framed as how many objects will be eligible for garbage collection, because you cannot actually determine which object exactly will be collected as garbage by JVM;
These are my thoughts on the question
1.On line two the variable story of type Short will be eligible for garbage collection at the end of the class because it is an instance object variable.
2.An object of reference type c1 and c2 are both placed on the heap on line 8 and 9 respectively.
3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard.
So c3 will be eligible for garbage collection because it will be pointing to null, as well as c1 and story of type Short making three objects eligible for garbage collection.
These are my thoughts please.
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell the question exactly as it appears, as well as given possible answers, so we'd know which lines are the part of interest. Such questions in OCAJP exam as far as I'm aware is being introduced in variuous ways, so quite difficut to pull the right/or any answer without knowing exact question is being asked.

Kelvin Okornoe wrote:2.An object of reference type c1 and c2 are both placed on the heap on line 8 and 9 respectively.
I don't think such answer adds clarity to the concept such question is meant to teach. In different words - this knowledge is insignificant in this question in my opinion.

Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard
Well, it isn't so precise as it should be. That should be more like: "method go() gets invoked on object reference c1 by passing an argument c2, and the result being assigned to reference variable c3".

However, what do you think, what happends with initially created object (c2) after the c2 is passed as an argument to method go() ?
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Please tell the question exactly as it appears, as well as given possible answers, so we'd know which lines are the part of interest. Such questions in OCAJP exam as far as I'm aware is being introduced in variuous ways, so quite difficut to pull the right/or any answer without knowing exact question is being asked.

Kelvin Okornoe wrote:2.An object of reference type c1 and c2 are both placed on the heap on line 8 and 9 respectively.
I don't think such answer adds clarity to the concept such question is meant to teach. In different words - this knowledge is insignificant in this question in my opinion.

Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard
Well, it isn't so precise as it should be. That should be more like: "method go() gets invoked on object reference c1 by passing an argument c2, and the result being assigned to reference variable c3".

However, what do you think, what happends with initially created object (c2) after the c2 is passed as an argument to method go() ?
JavaRanchGarbageCollection2.PNG
[Thumbnail for JavaRanchGarbageCollection2.PNG]
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:
Liutauras Vilda wrote:Please tell the question exactly as it appears, as well as given possible answers, so we'd know which lines are the part of interest. Such questions in OCAJP exam as far as I'm aware is being introduced in variuous ways, so quite difficut to pull the right/or any answer without knowing exact question is being asked.

Kelvin Okornoe wrote:2.An object of reference type c1 and c2 are both placed on the heap on line 8 and 9 respectively.
I don't think such answer adds clarity to the concept such question is meant to teach. In different words - this knowledge is insignificant in this question in my opinion.

Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard
Well, it isn't so precise as it should be. That should be more like: "method go() gets invoked on object reference c1 by passing an argument c2, and the result being assigned to reference variable c3".

However, what do you think, what happends with initially created object (c2) after the c2 is passed as an argument to method go() ?



This what I think happens to c2 after it is passed as an argument to the method go(), the reference variable c2 points to null.
 
Dave Tolls
Ranch Hand
Posts: 2282
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java is pass by value, hence any changes to the value of a variable (that is, the value of a primitive or reference variable) will not be reflected in the calling code.

So c2 in main() cannot be changed to point to anything else when passed in as a parameter to a method.
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard
Liutauras Vilda wrote:Well, it isn't so precise as it should be. That should be more like: "method go() gets invoked on object reference c1 by passing an argument c2, and the result being assigned to reference variable c3".
Actually you said that precisely, I misread that yesterday (it was very late night here), so apologies for that.

Dave Tolls wrote:Java is pass by value, hence any changes to the value of a variable (that is, the value of a primitive or reference variable) will not be reflected in the calling code.
So c2 in main() cannot be changed to point to anything else when passed in as a parameter to a method.
Dave is of course is right about what he just explained.

Kelvin Okornoe wrote:1.On line two the variable story of type Short will be eligible for garbage collection at the end of the class because it is an instance object variable.
Well, that is a bit too early, you need to look first at the higher level. main() method. Look to main method and try identify, which objects at main methods level will likely be eligible to be garbage collected.

Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard.
So c3 will be eligible for garbage collection because it will be pointing to null, as well as c1 and story of type Short making three objects eligible for garbage collection.
These are my thoughts please.
Does c3 ever gets constructed as object? Or just variable of type CardBoard declared and initialized to null? If it never gets constructed, can it be eligible to be garbage collected?


 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:
Liutauras Vilda wrote:
Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard
Liutauras Vilda wrote:Well, it isn't so precise as it should be. That should be more like: "method go() gets invoked on object reference c1 by passing an argument c2, and the result being assigned to reference variable c3".
Actually you said that precisely, I misread that yesterday (it was very late night here), so apologies for that.

Dave Tolls wrote:Java is pass by value, hence any changes to the value of a variable (that is, the value of a primitive or reference variable) will not be reflected in the calling code.
So c2 in main() cannot be changed to point to anything else when passed in as a parameter to a method.
Dave is of course is right about what he just explained.

Kelvin Okornoe wrote:1.On line two the variable story of type Short will be eligible for garbage collection at the end of the class because it is an instance object variable.
Well, that is a bit too early, you need to look first at the higher level. main() method. Look to main method and try identify, which objects at main methods level will likely be eligible to be garbage collected.

Kelvin Okornoe wrote:3.The method go with a parameter of reference variable c2 is invoked on c1 and a result value of null will be returned and stored in c3 of type CardBoard.
So c3 will be eligible for garbage collection because it will be pointing to null, as well as c1 and story of type Short making three objects eligible for garbage collection.
These are my thoughts please.
Does c3 ever gets constructed as object? Or just variable of type CardBoard declared and initialized to null? If it never gets constructed, can it be eligible to be garbage collected?

c3 never gets to be constructed as an object, it is just a variable of type CardBoard declared and initialized to null. So it cannot be garbage collected because it is not created or available on the heap.




c3 never gets to be constructed as an object, it is just a variable of type CardBoard declared and initialized to null. So it cannot be garbage collected because it is not even created/available on the heap.
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:c3 never gets to be constructed as an object, it is just a variable of type CardBoard declared and initialized to null. So it cannot be garbage collected because it is not even created/available on the heap.
Sounds correct. Have a cow for getting closer

So try to collect all information from this thread into one place and see if you can come up with a right answer to the test question.
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Kelvin Okornoe wrote:c3 never gets to be constructed as an object, it is just a variable of type CardBoard declared and initialized to null. So it cannot be garbage collected because it is not even created/available on the heap.
Sounds correct. Have a cow for getting closer

So try to collect all information from this thread into one place and see if you can come up with a right answer to the test question.


From my understanding I do understand that, c3 will not be eligible for garbage collection, because it is not created at all.
c2 will also not be eligible for garbage collection,because the returned value from the method does not affect the variable reference variable c2 in the main method.
But c1 will be eligible for garbage collection because it points to null, at this point only one object reference will be eligible for garbage collection.
But what happens to instance variable story of type Short?
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:But what happens to instance variable story of type Short?
Sorry for a late reply, been busy.

So, Short is an object too, right? And once c1 becomes eligible to be garbage collected, all other objects which lived in it become too.
Reminder, objects become eligible for garbage collection when they don't have any references which point to them or when they go out of scope.

Read one of certification books out there for the more precise formulation, but that is an idea.
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Kelvin Okornoe wrote:But what happens to instance variable story of type Short?
Sorry for a late reply, been busy.

So, Short is an object too, right? And once c1 becomes eligible to be garbage collected, all other objects which lived in it become too.
Reminder, objects become eligible for garbage collection when they don't have any references which point to them or when they go out of scope.

Read one of certification books out there for the more precise formulation, but that is an idea.


Short is an object(a reference type) and it has not been created yet,but it just has a variable story initialized with a value of 200.
So I was thinking since it was not even created with the new operator, it is not available to be eligible for garbage collection.
This is my thought please!

Am using Kathy and Berts certification book OCA and OCP, that was where I took the question from.
But I must appreciate your time and attention you have given me so far, I am very grateful for that.
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kelvin Okornoe wrote:Short is an object(a reference type) and it has not been created yet,but it just has a variable story initialized with a value of 200.
So I was thinking since it was not even created with the new operator
Short, Integer, Double... these are wrapper classes for primitives and they have a mechanism autoboxing and unboxing (<-- link), hence you are not required to use keyword 'new' in order to create an object.
 
Dave Tolls
Ranch Hand
Posts: 2282
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This line:

is a little more complex.

The compiler actually turns this into:

which results in a new Short being created if it's outside a certain range (which 200 is).

So in the current example that will create a new Short object.
 
Liutauras Vilda
Marshal
Posts: 3193
127
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:which results in a new Short being created if it's outside a certain range (which 200 is).
According to the documentation, that sounds like not always known.

Java API wrote:Returns a Short instance representing the specified short value. If a new Short instance is not required, this method should generally be used in preference to the constructor Short(short), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.


Nevertheless, I don't think it is a part of OCA exam to know that in such details.
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Dave Tolls wrote:which results in a new Short being created if it's outside a certain range (which 200 is).
According to the documentation, that sounds like not always known.

Java API wrote:Returns a Short instance representing the specified short value. If a new Short instance is not required, this method should generally be used in preference to the constructor Short(short), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.


Nevertheless, I don't think it is a part of OCA exam to know that in such details.


Okay!, understood, and a very big thanks to Liutauras and Dave for your time and contrubution....
 
Junilu Lacar
Sheriff
Posts: 10025
120
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't take whatever you see in the book or website you're using as an example of how to format your code properly.  That second image you posted shows a horrible style of formatting, particularly the last two closing braces.  That is totally misleading. It saves one extra line of code at the cost of clarity. Not a good trade-off at all, IMO.
 
Kelvin Okornoe
Greenhorn
Posts: 29
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Please don't take whatever you see in the book or website you're using as an example of how to format your code properly.  That second image you posted shows a horrible style of formatting, particularly the last two closing braces.  That is totally misleading. It saves one extra line of code at the cost of clarity. Not a good trade-off at all, IMO.


Thank you, I have taken your advice to my heart, head, and hand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!