Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Garbage Collection Challenge

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking my time to make sure I understand this. I've attempted question 19 (Jeanne Boyarsky and Scott Selikoff, OCA Java SE 8 Programmer I Study Guide, Chapter 1 review questions, page 48).
Here's the code:

The correct choices in the answer key are:

B. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.

D. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.


D makes sense, because we're assigning it null on line 9, but B not so much, because one was first assigned to null on line 6.
Also, "following line 8" means "all of the lines below line 8", right?

I drew this to help me out, but I have no idea what to draw for line 10:

[If place inline fails, Rabbit_GC.png is the attached image that's supposed to be here]

The two reference variable has already been disconnected from object R2 on line 9, so would line 10 have to point it to a a new R3 object or something?
Rabbit_GC.PNG
[Thumbnail for Rabbit_GC.PNG]
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:. . .  I have no idea what to draw for line 10: . . .

What about two→→→R3? It is a new object, so it isn't R2.
 
Sam Peterson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Sam Peterson wrote:. . .  I have no idea what to draw for line 10: . . .

What about two→→→R3? It is a new object, so it isn't R2.


What about that one object created on line 3? Do you think it became eligible for garbage collection on line 6 or line 8?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually think about variables in java as pointers.

So :
Rabbit one = memory point 1008 = new instance object of Rabbit1Data.
When you then then get on line 5,
Rabbit three = one; That actually says, Rabbit three is the same memory addres of pointer Rabbit one. (It actually copys the pointing value of one, and it is not really referring to one at all after it has copied the adres).

So now both variable one and three reference to memory point 1008. (And java will check from time to time if there are any variables pointing to this same memory adress).

Now as long as there is a pointer(variable) to point 1008, Rabbit1Data should not be garbage collected.
 
Sam Peterson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Koen Ursem wrote:
So now both variable one and three reference to memory point 1008. (And java will check from time to time if there are any variables pointing to this same memory adress).

Now as long as there is a pointer(variable) to point 1008, Rabbit1Data should not be garbage collected.


Yes, but after the last pointer activity on line 10, the R1 object still has four still pointing to it, two is now pointing to an R3 object, and object R2 has nothing pointing to it. So in reality, isn't R2 the only 'object' eligible for garbage collection? And will it automatically be collected in this case, simply because it is 'eligible' for garbage collection?
 
Koen Ursem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 8, one is being pointed to an empty adress not refering(pointing) to any Rabbit object in memmory.
At line 9, four copies this empty refer(null addres) from one also not refering(pointing) to any Rabbit objects in memmory.

This means only three after line 6 is still pointing to the Rabbit you called R1 created from the new constructor on line 3.


Explanation:
The new constructor statement in java is the moment where data is to be created in memmory.
The variables are just referals to this data, the variable one is of type Rabbit, this is only saying that it expects on the address it refer to, that there is data on this address that equals the data-structure of a Rabbit.


 
Marshal
Posts: 7178
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:

Campbell Ritchie wrote:

Sam Peterson wrote:. . .  I have no idea what to draw for line 10: . . .

What about two→→→R3? It is a new object, so it isn't R2.


What about that one object created on line 3? Do you think it became eligible for garbage collection on line 6 or line 8?


Sure latter.

Do you think differently?
 
Liutauras Vilda
Marshal
Posts: 7178
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP

Please explain what do you know as for the rule(s), when object become eligible for garbage collection?
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:... after the last pointer activity on line 10, the R1 object still has four still pointing to it


No, it doesn't. See line 6 before making your conclusion about line 7. The only way you'd think that line 7 makes four reference R1 (the Rabbit created on line 3) is if you misunderstood what line 6 and line 7 do. Edit: Actually, looking at your notes about what you think happens on line 7, you have misunderstood it. On line 6, what is one referencing? Then how does four magically reference R1 on line 7 if you're assigning what one references to it?
 
Sam Peterson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Sam Peterson wrote:On line 6, what is one referencing? Then how does four magically reference R1 on line 7 if you're assigning what one references to it?


One of the answers from the answer key says, "The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8."

What does the context 'immediately following line 8' mean? Does that mean all of the lines after or before line 8?

 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"immediately following" means after. That is, from line 9 onwards.
 
Sam Peterson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Koen Ursem wrote:I usually think about variables in java as pointers.

So :
Rabbit one = memory point 1008 = new instance object of Rabbit1Data.
When you then then get on line 5,
Rabbit three = one; That actually says, Rabbit three is the same memory addres of pointer Rabbit one. (It actually copys the pointing value of one, and it is not really referring to one at all after it has copied the adres).

So now both variable one and three reference to memory point 1008. (And java will check from time to time if there are any variables pointing to this same memory adress).

Now as long as there is a pointer(variable) to point 1008, Rabbit1Data should not be garbage collected.



So does the garbage collector remove memory addresses, objects or both? If it removes objects, then object R1 should have already been eligible for garbage collection on line 6 (contrary to the answer key saying that it's eligible after line 8):
Line6eligible.jpg
[Thumbnail for Line6eligible.jpg]
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your drawing for line 5 and line 6 is wrong with respect to what happens to the three reference variable. You are misunderstanding what this does:

It's not what you're depicting in your most recent drawing. Your first drawing was correct.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A reference variable always refers to an object or null. It will never refer to another reference variable.

This line

makes the three reference variable refer to the same thing that the one reference variable refers to. At that point, the one variable refers to R1. On line 7, the reference variable four is made to refer to the same thing that one refers to, which at that point is null so four will also be null.

Is that clearer now?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:So does the garbage collector remove memory addresses, objects or both?


The garbage collector frees up memory that was used by an object, so the memory can be used by something else. I'm not sure what you mean by "remove memory addresses" but taken at face value, memory addresses cannot be removed.

A fitting analogy is literally a garbage collector like your mom, for example. When (or if) you (as a teenager or messy adult) have a lot of clutter/garbage in your room, your mom (the GC) will come in and see a bunch of dirty articles of clothing that you're no longer using strewn about on the floor. Those dirty clothes are like your GC-eligible objects. When your mom sees these objects, she picks them up, freeing up whatever space they were taking up for other things, like newer dirty articles of clothing. The floor is the memory space that objects can occupy. Your mom never removes sections of the floor, does she? Likewise, the GC will never "remove memory addresses".
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another analogy that might help you.

Imagine you and three of your closest friends were at the zoo and dropped by the small animal yard.  There, you find several pens you can go to pet the small animals. You and your friends go into the Rabbit pen to spend some time with the little bunnies. Each person who enters the Rabbit pen is given one (and only one) leash and instructed to hold your leash at all times, even when there is nothing attached to the leash. The zookeeper takes rabbits from their cages and loops a leash around its neck so that the person holding the leash can interact with it safely. If you no longer wish to play with that particular rabbit, you simply slip the leash off. The zookeeper will go around the pen and put any unleashed rabbits back into their holding cage.

Are you following me so far?

Let's say you are ONE and the zookeeper puts the first rabbit, R1, at the end of your leash. That's what this does:

Your second friend, TWO, is up next. The zookeeper takes out another rabbit, R2, and loops TWO's leash around its neck. This is what this does:

The rabbit pen is a popular one and now it seems that there are no rabbits in the holding cage at the moment so the zookeeper tells you third friend, THREE, "Why don't you just play with the rabbit your first friend has right now" and loops THREE's leash around the rabbit that you're playing with (R1). That's what this does:

So now both you (ONE) and your friend THREE are holding leashes attached to the first rabbit, R1.

But you don't want to share a rabbit with THREE so you slip your leash off R1. This is what this does:

When you do this, your friend THREE still has her leash on R1. She doesn't care about what you do to your leash. She wants to play with rabbit R1.

Now your fourth friend, FOUR, comes into the pen and is given a leash but since there are still no rabbits available, the zookeeper tells FOUR to just stand beside you, without a rabbit to play with. This is basically what line 7 does:


So here's the situation at this point:
- THREE has a leash on R1
- TWO has a leash on R2
- you (ONE) have a leash that is just hanging there with no rabbit (null) at the end of your leash
- and FOUR is in the same situation as you, standing around with no rabbit (null) at the end of her leash

The rest should be easy to follow:

THREE is done with R1 and slips her leash off and goes over to stand beside you and FOUR

Then TWO follows suit:

Before TWO can say anything, the zookeeper takes a different rabbit, R3, and slips TWO's leash around its neck:

If you've been following the story carefully up to this point you should be able to figure out which of the R1, R2, and R3 rabbits is roaming freely around the pen unleashed and are eligible to be picked up by the zookeeper to be put back in the holding cage.

NOTE:

1. Never at any point is a leash put around a person's neck. That's just wrong. That's what your second diagram basically shows, where three is pointing to one, which means you're saying that your friend THREE put her leash around your neck. That is, a reference variable will always refer to a Rabbit, never another reference variable.

2. Never at any point does the zookeeper touch you or your friends and puts any of you lot in the holding cages. That's just not how zookeepers work. They only pick up Rabbits. That is, the GC will only clean up objects that are no longer in use. The GC never bothers with reference variables except to see if they are currently referring to a rabbit object or not. That is to say, the GC never cleans up reference variables.

Your next question might logically be: Who cleans up the reference variables then? That is, how does the zookeeper get all those people just standing around with no rabbits on the end of their leashes to leave the Rabbit pen to free up some space?

Since this is quite long now, I will answer that question if you actually ask it.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing about that last analogy. There's an aspect to the story that might be somewhat misleading. I said that "the zookeeper puts the leash around the rabbit's neck..." Note that this part of the story is NOT meant to imply that the Garbage Collector also assigns objects to reference variables because it most certainly doesn't.

To make things a little clearer, let's just say the Zookeeper who puts leashes around rabbits necks is a different person from the one who picks up free-roaming rabbits and puts them back in their cages. Only that zookeeper, the one who picks up free rabbits and puts them away, is the equivalent of the Java Garbage Collector.
 
Sam Peterson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, your rabbit zoo analogy finally cleared the confusion about the solutions from the answer key:

Sam Peterson wrote:

The correct choices in the answer key are:

B. The Rabbit object from line 3 is first eligible for garbage collection immediately following line 8.

D. The Rabbit object from line 4 is first eligible for garbage collection immediately following line 9.


Following your analogy, I think I finally got the drawing correct:

[if place inline fails, the image that's supposed to be here is RabbitGCsolved.jpeg]

However, due to what you said here:

Junilu Lacar wrote:

Sam Peterson wrote:So does the garbage collector remove memory addresses, objects or both?


The garbage collector frees up memory that was used by an object, so the memory can be used by something else. I'm not sure what you mean by "remove memory addresses" but taken at face value, memory addresses cannot be removed.


And here:

Junilu Lacar wrote:
The floor is the memory space that objects can occupy. Your mom never removes sections of the floor, does she? Likewise, the GC will never "remove memory addresses".


I'm a little confused to why you brought this up:

Junilu Lacar wrote:
Your next question might logically be: Who cleans up the reference variables then?


Are reference variables literally temporary physical placeholders in memory space on the computer hard disk?

Also, since we still have the R3 object that's not eligible for garbage collection at the end of this program, what is the lifespan of that object and the two reference variable after the program finishes?

Also, whether or not we write code that stores that object and it's attributes to a database, would doing or not doing that also affect the lifespan of the object R3 and the reference variable two?
RabbitGCsolved.jpg
[Thumbnail for RabbitGCsolved.jpg]
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sticking with the zoo analogy, I anticipated you asking something along the lines of "Ok, so the GC is the zookeeper who puts the rabbits away, but who's in charge of putting all those leashes away when the visitors leave?"

The answer is "Don't worry yourself about that. Once the people leave the pen, the leashes just magically disappear. There's a nondepletable supply of leashes to give to new people coming in."
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your most recent drawing is a correct depiction.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All objects are GC eligible when a program terminates although at that juncture it's kind of a moot point since all the memory used by the program gets freed up by the OS anyway, theoretically at least.

How does the JVM manage memory used by objects, variables, etc?
Where does the JVM keep reference variables

If you're just studying for a Java programming certification exam, I'm pretty sure this information is out of scope.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are reference variables literally temporary physical placeholders in memory space on the computer hard disk?


You should hope it doesn't come to that because your system would slow down to a crawl. No, under normal conditions, variables are stored somewhere in virtual memory (RAM). If you're low on RAM, them some things might get "paged out to disk" but when too much of that starts to happen, you get what's called disk thrashing.
 
Koen Ursem
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:
Also, since we still have the R3 object that's not eligible for garbage collection at the end of this program, what is the lifespan of that object and the two reference variable after the program finishes?



Ooh i like this question,

I wonder if R3 would be up for GC at line 10 when there is no further use of the variable two in the current scope, or on line 12 where the scope of the current codeblock ends?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Koen Ursem wrote:

Sam Peterson wrote:
Also, since we still have the R3 object that's not eligible for garbage collection at the end of this program, what is the lifespan of that object and the two reference variable after the program finishes?

I wonder if R3 would be up for GC at line 10 when there is no further use of the variable two in the current scope, or on line 12 where the scope of the current codeblock ends?


There's the concept of weak references vs strong references in Java that you might want to research some more if you're really interested in those nitty-gritty details. In this case, whether R3 is GC-eligible on line 10 or when the main() method terminates is rather like splitting hairs and, as I said previously, a moot point. Since the program will terminate when you exit from main(), the GC probably won't even bother to sweep up any of those Rabbits since the whole zoo is getting vaporized anyway!
 
Maybe he went home and went to bed. And took this tiny ad with him:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!