• Post Reply Bookmark Topic Watch Topic
  • New Topic

When will garbage collection do the clean up?  RSS feed

 
Frankie Law
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question about non reference objects.  Let say I have the follow code below.
The question is, how long will Integer( 20 ) stays in memory?



Question 2:
Given the same piece of code, but I inserted the numbers into the ArrayList.
How long will num3 stay in memory?  Will garbage collection come and clean up the memory that isn't being use?
Or am I getting the wrong idea?
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't know when GC clean memory.

GC have specific algorithms.

 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There can be no definite duration based answer to "How long does it stay in memory"
The best answer would probably be:  "Until it is cleaned up by the Garbage Collector"
And a garbage collection runs "when it needs to"

It very much depends on the settings you choose for the garbage collector.
A quick google brought up an article: Understanding Java Garbage Collection which explains it better than I ever could.


In general:
Those "new Integers" you create will be allocated memory in the Eden space.
When a GC runs in the eden space, it uses a "Mark and Sweep" algorithm to copy only those values that are currently "alive" (able to be accessed) to the "Survivor" space

In Question 1 it would retain only those items still in the list.  The one that got removed wouldn't be accessible, so it would not be copied to the "Survivor" space.
In Question 2 it would depend on if the variable num2 was still "alive" or not when the garbage collector ran. 

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you can say for sure is that if the JVM is about to run out of memory, it will first do garbage collection on all unreferenced objects before giving up and throwing the out-of-memory error.
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:There can be no definite duration based answer to "How long does it stay in memory"
The best answer would probably be:  "Until it is cleaned up by the Garbage Collector"
And a garbage collection runs "when it needs to"

It very much depends on the settings you choose for the garbage collector.
A quick google brought up an article: Understanding Java Garbage Collection which explains it better than I ever could.


In general:
Those "new Integers" you create will be allocated memory in the Eden space.

What is Eden space?

When a GC runs in the eden space, it uses a "Mark and Sweep" algorithm to copy only those values that are currently "alive" (able to be accessed) to the "Survivor" space

In Question 1 it would retain only those items still in the list.  The one that got removed wouldn't be accessible, so it would not be copied to the "Survivor" space.
In Question 2 it would depend on if the variable num2 was still "alive" or not when the garbage collector ran. 

 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



In general:
Those "new Integers" you create will be allocated memory in the Eden space.

What is Eden space?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anda Cristea wrote:
In general:
Those "new Integers" you create will be allocated memory in the Eden space.

What is Eden space?


It's part of the heap, where objects are created. And of course, this is completely in the realm of implementation detail. There is no mention of it in the specifications, and may vary between Java versions, and platforms.... and of course, completely outside of the realm that someone learning Java should be trying to figure out...

Henry
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . may vary between Java versions, and platforms.... . . .
I don't think it was there a few versions ago, and as Henry says, it may disappear from the next version.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!