Article From:

Looking at the JDK dynamic proxy source, I found that the cache was being cached, but the cache was wrapped in WeakReference
Do I think I can’t put it directly in it? Why do I introduce such a weak quote?

Answer 0:

No research has been done on its source code, only from the problem analysis, the role of WeakReference.

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings.
Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable. At that time it will atomically clear all weak references to that object and all weak references to any other weakly-reachable objects from which that object is reachable through a chain of strong and soft references. At the same time it will declare all of the formerly weakly-reachable objects to be finalizable. At the same time or at some later time it will enqueue those newly-cleared weak references that are registered with reference queues.

The above is quoted from the WeakReference document

It mentions that weak references are often used for canonical mapping, and that objects with weak references are more easily marked as recoverable when performing memory accessibility analysis. Looking at the source code of the dynamic proxy class, it turns out that static classes and methods are used, so should we consider the Cache of the dynamic proxy class?It is referenced by static classes and static methods (statics are often referred to as GC Roots in reachability analysis)

It can serve as the object of root:

  1.A static variable in a class, when it holds a reference to an object, it acts as a root.

The first class of elements listed as GC Root is static member variables.

So if caches are no longer needed, using strong references makes GC tag analysis think they are reachable from GC Root, and it is not likely to tag this memory, instead, it can effectively tag these caches, thus improving memory recovery efficiency.

The above is my personal understanding, perhaps rather superficial.

Leave a Reply

Your email address will not be published. Required fields are marked *