A recent study of the Java cache implementation found the use of soft references (SoftReference), which was not quite understandable, and looked at JDK documentation, code, and several articles. Make a summary, if there is a mistake, please correct me.
There are two reasons why you want to learn about several types of references in Java:
Understand the relationship between Java cache implementations, learning Java references, and Java garbage collection mechanisms
Memory resources are limited and need to be used wisely. The cache is not simply HashMap, and Java references are very closely related to the Java garbage collection mechanism.
Avoid incorrect use of Java references
A colleague puts 5000+ transaction data into a hashmap, pointing to the HashMap with a spring Singleton Bean's global properties. Using this technique in large quantities, it was soon reported out of memory. The larger memory also jiabuzhu the wrong use of memory. Understanding the principles helps us to minimize or not make low-level mistakes.
The relationship between Java reference and Java garbage collection mechanism
When the Java Virtual Machine (JVM) feels insufficient memory, it triggers a garbage collection operation (GC), cleans up useless objects, and frees up memory. But how to tell if an object is rubbish? One method is to calculate the number of references to the object, and if the number of references is 0, the object is garbage (the thread object is the exception), otherwise it is useful and cannot be reclaimed. But what if the object with the reference number 0 is recycled, or does not meet the memory requirements? Java divides references into 4 types, and the garbage collector attempts to reclaim objects that have only weak references.
The reference to an object can be up to (Reachable) strength, from strong to weak divided into 5 categories, as follows:
Strong to reach (strong Reachable)
Within a thread, there is no need to refer directly to the newly created object, which is strongly accessible.
Soft to reach (Soft Reachable)
is not strong, but it can be reached by a soft reference (SoftReference).
Weak to reach (Soft Reachable)
Neither the strong nor the soft can reach, but through a weak reference (WeakReference) can be reached.
Virtual up to (Phantom Reachable)
It is neither strong nor soft, nor weakly accessible, but can be reached by a virtual reference (phantomreference).
Not reached (unreachable)
There is no reference to the object.
It is better and easier to understand that the Java garbage collector prioritizes objects that are low in strength. There are also two important points:
The strong can never be cleaned up.
The JVM guarantees to clean up all soft reference objects before throwing out of memory
Brother Lian Java Training
share the following 4 Types of Java references
When implementing a caching system, if you use a strong reference, you need to manually clear some references (reference set NULL), otherwise the out of memory errors will be thrown sooner or later. The only reason the cache system introduces a weak or soft reference is to leave the reference clear to the Java garbage collector and the cache program to stay out of it.
Several weak references are used in very similar ways. The following are 4 reference types, respectively.
Strong references (strongreference)
We usually declare that variables use strong references, more than 99% of ordinary systems are strong references. Like what
String s = "Hello World"
Weak references (WeakReference)
When the garbage collector decides to reclaim a soft object at some point, it cleans up the soft reference and optionally holds the reference to a reference queue (Referencequeue).
Soft references (SoftReference)
Similar to weak references, except that Java virtual opportunities try to keep the soft references alive for a longer period of time before they are cleaned up.
Virtual Reference (Phantomreference)
This is more flexible than the finalize mechanism in object, which is used only to deal with resource cleanup issues. The Get method always returns the Null,java virtual machine is not responsible for cleaning up virtual references, but it puts virtual references inside the reference queue.
Two examples of tests
With HashMap, an out of memory error is reported.
[Java] View
Plain Copy
public static void Main (string[] args) {
map<string, string> list = new hashmap<string, string> ();
Long i = 1;
while (I < 100000000L) {
List.put (
String.valueof (i),
"JDJJDJJJJJJJJJJ%%%%%%%%JJJJJJJJJJJJJJJKKKKKKKKKKKKKKKKKJJJJJJ"
+ "JJJKKKKKHDDDJDJDJDJDJDJDJDJJDJDJDJDJDJDJJDJDJDJDJJDJDJJJJJJJJJ"
+ "JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ"
+ "JJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
Test whether the first one is still alive
if (i% 100000 = = 0) {
System.out.println (List.get (string.valueof (1)));
}
i++;
}
}
With Weakhashmap, out of memory errors are not reported.
[Java] View
Plain Copy
public static void Main (string[] args) {
map<string, string> list = new weakhashmap<string, string> ();
Long i = 1;
while (I < 100000000L) {
List.put (
String.valueof (i),
"JDJJDJJJJJJJJJJ%%%%%%%%JJJJJJJJJJJJJJJKKKKKKKKKKKKKKKKKJJJJJJ"
+ "JJJKKKKKHDDDJDJDJDJDJDJDJDJJDJDJDJDJDJDJJDJDJDJDJJDJDJJJJJJJJJ"
+ "JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ"
+ "JJJJJJJJJJJJJJJJJJJJJJJJJJJJ");
Test whether the first one is still alive
if (i% 100000 = = 0) {
System.out.println (List.get (string.valueof (1)));
}
i++;
}
}
Summary
The Java language inside the array, list, map and other containers, for each object inside has a reference, big data in the case of the memory leak. Weak references are only suitable for special scenarios such as cache, and strong references are used for objects that must not be recycled by the garbage collector in Java.
Shallow solution of four Java references