Reputation: 3919
I have a java application "with history" and it uses WeakReferences for caching. I made several heapdumps and saw that all of them contains a lot of objects with weak references (10%-15% of heap size, ~1.2GB).
P.S. I know that WeakReference produces performance penalty for GC for CMS, because it makes algorithm harder. But did somebody see a paper or some official information about it? I found only this SO post.
Upvotes: 4
Views: 995
Reputation: 310957
Does it mean that weak references produce memory pressure on JVM?
No. Weak references don't affect GC at all. They just give you a way to track it.
And forces a FullGC with stop-the-world pauses?
No.
Upvotes: 1
Reputation: 21696
Object Computing, Inc. did a presentation on this that I've found useful in the past. Here's an excerpt:
Kinds of Object References
- Strong references
- SoftReference
- GC’ed any time after there are no strong references to the referent, but is typically retained until memory is low
- can be used to implement caches of objects that can be recreated if needed
WeakRefernence
- GC’ed any time after there are no strong or soft references to the referent
- often used for “canonical mappings” where each object has a unique identifier (one-to-one), and in collections of “listeners”
"For soft and weak references, the get returns null method when the referent object has been GC’ed."
SOURCE: http://java.ociweb.com/mark/other-presentations/JavaGC.pdf
That seems to suggest SoftReference is the go to choice for cached objects.
In practice I have used Guava caching APIs and let it manage the details: https://github.com/google/guava/wiki/CachesExplained
Upvotes: 1