Simplified explanation:
Periodically, the garbage collector looks at all the objects in the system and sees that they are no longer available from live links. It frees up any objects that are no longer available.
Note that your method does not create a new Integer
object at all. A reference to the same Integer
object can be transmitted, for example, again and again.
The reality of garbage collection is much more complicated than this:
- Modern GCs tend to be generational, suggesting that most objects are short-lived, so you don't need to frequently check the entire (possibly large) heap; he can just often check "recent" objects for life
- Objects can have a finalizer - code that must be run before garbage collection. This delays the collection of garbage from such objects in a loop, and the object can even βresurrectβ itself, making itself available
- Modern GCs can be built in parallel and have many settings
source share