• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java's Garbage Collection

 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a static object (type HashMap) in a class. This HashMap contains other HashMaps. In these HashMaps are objects with a large data structure (it's a class of my own with primitive datatypes and other own classes).

When a certain action is performed, a certain HashMap (mapToRemove) must be removed from the static object and all the objects in mapToRemove are of no use anymore, so i want to give the memory taken by this objects free, so it can be used by other objects.

I know i can do something like this:
Object o = HashMap.remove(keyOfMapToRemove);
o = null;

but will this clear also al the other objects in this hashmap? and how can i verify an object is collected for garbage?

any hints, comments, tips, tricks are more then welcome

Thanks!
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As long as an object isn't referenced by anything it will be eligible for garbage collection. In java, the runtime environment takes care of garbage collection destroying objects it determines are no longer used. This is performed periodically and the programmer should not concern themselves with the nuts and bolts of this operation.
However it may be deemed necessary to force garbage collection before a computationally intense operation. This can be suggested by using
It must be noted that using one of the above calls does not guarantee garbage collection, but merely suggests that the garbage collector makes a best effort to clean discarded objects from memory.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just calling HashMap.remove(Object) is enough.
If all you want to do is discard the value don't want to do something with it first you can savely discard the returned value from the method.

And indeed this will make everything that's referenced by the object you remove eligible for garbage collection (unless they're referenced by something else of course).
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sound to me like the individual objects will be garbage collected in the second GC pass. In the firsct GC pass, the HashMap is garbage collected. If it contained the only references to the objects in question, that leaves them available for garbage collection on the next pass.

So you'd call System.gc() twice.


This leads to the question of how much attention the jvm pays to System.gc()

The following links suggest that HotSpot consistently responds to System.gc(), though Java purists probably think that Sun shouldn't have said anything.

http://java.sun.com/docs/hotspot/PerformanceFAQ.html#15

http://java.sun.com/docs/hotspot/PerformanceFAQ.html#23


The following quote makes it clear that the even some Sun code relies System.gc(), so these calls are honored.

Another way applications can interact with garbage collection is by invoking full garbage collections explicitly, such as through the System.gc() call. These calls force major collection, and inhibit scalability on large systems. The performance impact of explicit garbage collections can be measured by disabling explicit garbage collections using the flag -XX:+DisableExplicitGC.

One of the most commonly encountered uses of explicit garbage collection occurs with RMI's distributed garbage collection (DGC). Applications using RMI refer to objects in other virtual machines. Garbage can't be collected in these distributed applications without occasional local collection, so RMI forces periodic full collection.



[ October 07, 2004: Message edited by: Mike Gershman ]
[ October 07, 2004: Message edited by: Mike Gershman ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:
It sound to me like the individual objects will be garbage collected in the second GC pass. In the firsct GC pass, the HashMap is garbage collected. If it contained the only references to the objects in question, that leaves them available for garbage collection on the next pass.

So you'd call System.gc() twice.



No, it doesn't work like that. There is a load of documentation about GC on Sun's Web site, but most of the time you don't care.

Basically, if no other object has a (strong) reference to an object, it will be GC'd ... sometime. There are no guarantees about when this happens, except that all unreferenced objects will have been GC'd before OutOfMemoryError gets thrown.

Calling System.gc() a few times does usually seem to cause all unreferenced objects to get GC'd, but that is not guaranteed, however many times you call it.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the GS is called, is not my main issue. i just have to know how i can collect all this objects for garbage collection, because if they aren't cleaned up, the app will go out of memory and that's not meant to be.

So if i do a hashmap.remove all the objects (keys + values) will be collected for garbage if they aren't reference by another object, variable,...
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't actually run out of memory while there are objects eligible for garbage collection. The only risk is that if the HashMap is large enough, garbage clection can occur at an inconvenient time and your user can get a time-out or just think he's hung. However, I've never heard a first-hand account of this happening on a modern client or server - the things are just so darned fast.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how can i verify an object is collected for garbage?



Just add a weak reference to the object and later check if it's null
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:


Just add a weak reference to the object and later check if it's null



how do i create a weak reference to the object? and an object is collected for garbage if there are no references anymore to this object, so how could it be collected if i add a weak reference to it?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Weak references do not prevent garbage collection. They return a strong reference to the object if it's still in memoery, otherwise they return null.

As to how to use weak references, I found this example in Sun's "The Java Programming Language, Third Edition", section 12.5. The problem is that it compiles with an obscure warning, "unchecked call to WeakReference(T) as a member of the raw type java.lang.ref.WeakReference", pointing to the new operator. So I think this is correct, but I'm not sure.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Gershman:
"unchecked call to WeakReference(T) as a member of the raw type java.lang.ref.WeakReference



That's a warning of JDK 1.5, telling you that you should use the new generics feature. Try

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic