• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

When will garbage collection do the clean up?

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 460
6
Netbeans IDE Oracle Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't know when GC clean memory.

GC have specific algorithms.

 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.  

 
Sheriff
Posts: 28321
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 460
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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

What is Eden space?

 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, probably the answer would be until memory is cleaned by garbage collector.

In question 1 :

objects would remain in the list.

In question 2:

Here variable num2 will still be alive that is, numbers.remove(1).

Meanwhile you can refer resource below for more information on garbage collection,

http://www.flowerbrackets.com/java-garbage-collection/
 
Campbell Ritchie
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The likeliest answer to the original question is, “never”. You cannot expect garbage collection to occur at any particular time; it is only likely to occur at all when the heap space is nearly all occupied. I don't think that blog is particularly helpful because it is too much of an oversimplification.
 
Trust God, but always tether your camel... to this tiny ad.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic