JVM - Are WeakReferences Collected in Small GCs?

I was interested, as this will make them less useful. If so, is there a way to make memory weakly referencing only garbage in the main GC?

+7
source share
6 answers

I do not know what is actually happening with WeakReference . The javadoc does not specifically state what the โ€œtime frameโ€ means for cleaning / breaking them. And this is the answer to your question (at least theoretically) "implementation dependent." Indeed, the JLS specification and javadocs do not even mention major or minor collections. The whole topic comes in the "implementation details" category.

If you need links sensitive to GC, then perhaps you should use SoftReference . This is described as follows:

"All soft links to objects with soft achievements are guaranteed to be cleared before the virtual machine throws an OutOfMemoryError. Otherwise, no restrictions will be set at the time the soft link is fixed, or the order in which the set of such links to different objects will be cleared. However, virtual machine implementations are encouraged to bias against clearing newly created or recently used soft links. "

Elsewhere, a soft link is described as stronger than a weak link. It is understood that it is less likely to be violated; for example, an overly hot garbage collector.

But note that even this does not mean large and small garbage collections.

+7
source

Why do you need this? Your program does not have to care about serious or small GC cycles, and in fact this kind of differentiation does not even exist in all JVM / GC configurations.

WeakReference collected as soon as there are no strong references to the object. This may include minor GC.

If you want the object to remain in place for a while until the actual pressure in memory, try SoftReference .

+5
source

You might be thinking of SoftReferences, which may be closer to what you want.

Small collections collect any object in a young space. Weak attitude towards the object in the young space will be collected on the younger GC. A WeakReference to an object in the space on which data is stored will be collected based on the collection, for example. Full GC. BTW You can only have parallel collections of the space on which you are stored.

+3
source

It will depend if the WeakReference object is in Eden or not - a small collection will only consider objects in Eden.

+1
source

WeakReference does not prevent the collection of the object, therefore, if the object belongs to a young space and is accessible only by weak links, it will be assembled.

SoftReferences (in JSM HotSpot) act either as weak or strong links, depending on the latest timestamps of access (timestamp of the last call to get() on the link). The "expiration time" of the sotf link is calculated as the free memory size multiplied by the coefficient configured via -XX:SoftRefLRUPolicyMSPerMB=T (for example, less free memory, short expiration of soft links).

Usually, using JVM links for caching is a bad idea: - it moves temporary objects into the old space, violating the weak hypothesis of generations - links pose a special threat through GC, which increases the insignificant time of GC

If you need a cache with an eviction strategy, it is better to implement it explicitly and take into account the effect of the occupied memory on the stored data in the eviction policy.

0
source

Some ideas from Brian Goetz (2006 article) that still appear on viguor:

Weak links

Soft Links โ†’ interesting details in [Poor Manโ€™s Cache] and [How the Garbage Collector Handles Links].

0
source

All Articles