We know that there are no pointers in the Java language, instead of referencing reference. There are four types of references in Java: Strong references, weak references (weakreference), soft references (softreference), virtual references (phantomreference). One of the strongest references is the most common reference we use, and we don't have the opportunity to use the virtual reference. So we mainly understand the next weakreference and SoftReference.
1. first a paragraph of code:
Public class referencetest { publicstaticvoid main (string[] args) { linkedlist<bytenew linkedlist<>(); for (int i=0; i<1024; i++) { list.add (newbyte[1024*1024]); } }}
The above code throws:Exception in thread "main" Java.lang.OutOfMemoryError:Java Heap space
Heap memory overflow. Because we are constantly allocating a 1M size byte[] object on the heap, and adding that reference to the list, looping 1024 times, consumes 1G of heap memory, causing heap space to OutOfMemory.
2. We use weekreference to modify the code:
Public classReferencetest { Public Static voidMain (string[] args) {LongBeginTime =System.nanotime (); linkedlist<weakreference<byte[]>> list =NewLinkedlist<>(); for(inti = 0; I < 1024; i++) {List.add (NewWeakreference<> (New byte[1024 * 1024])); } LongEndTime =System.nanotime (); System.out.println (EndTime-beginTime); }}
Results for output: 195947704 (0.19 seconds)
We found that the heap memory overflow error is gone. What is the reason for this? Because we used the weak reference weekreference to refer to the 1M byte[] object on the heap, the weak reference to the object referenced by Weekreference, if it is only a weak reference, but not strongly referenced, the next GC will reclaim the memory that the object occupies , so there is no memory overflow.
3. we use softreference to modify the code:
Public classReferencetest { Public Static voidMain (string[] args) {LongBeginTime =System.nanotime (); linkedlist<softreference<byte[]>> list =NewLinkedlist<>(); for(inti = 0; I < 1024; i++) {List.add (NewSoftreference<> (New byte[1024 * 1024])); } LongEndTime =System.nanotime (); System.out.println (EndTime-beginTime); }}
Output: 1499904286 (1.5 seconds)
We found that the heap memory overflow error is also gone. Because we used the soft reference softreference to refer to the 1M byte[] object on the heap, and the soft reference softreference referenced the object, if only the soft reference, but not the strong reference, when the memory space is insufficient, the GC The memory consumed by the object is reclaimed , so no memory overflow.
However, we note that the time taken to use weekreference and the use of SoftReference is close to 10 of the gap. The reason for this is that the GC reclaims the space occupied by the object only when there is insufficient memory space, and the full gc,full GC causes the STW program to pause, so it takes too much time to softreference.
4. Summary
Strong reference: As long as the object on the heap is pointed at at least one strong reference, the GC will not reclaim the object's memory space.
Weak references: As long as the objects on the heap are only pointed to by weak references, the next GC reclaims the object's memory space, regardless of whether the current memory space is sufficient.
Soft references: As long as the objects on the heap are only pointed to by soft references, and when there is not enough memory space, the GC reclaims the object's memory space.
WeakReference and SoftReference are generally used to construct a caching system, such as using a map. Because the cache system is a "full-life" object, the system stops, the cached object is destroyed, so when we constantly want to add objects to the cache object, then it will cause the cache object map to refer to more and more objects, and because it is a strong reference, these are placed in the map cache objects can not be collected by GC lock, This causes the system heap to become more memory intensive, which eventually results in memory overflow.
So at this point we can use WeakReference or softreference, after the strong references are wrapped in weakreference and softreference, into weak and soft references, then when the object in the cache is only cached by the map lock reference , the memory consumed by these objects is reclaimed in the next GC and in the case of out-of-memory GC. In fact, the JDK provides us with a specialized class: Weakhashmap, weak reference HashMap, so the structure of the caching system is that we can consider using it.
In fact, there are a lot of maps in the JDK, how we should choose the problem:
HashMap
Concurrenthashmap
TreeMap
Weakhashmap
Linkedhashmap
Collections.synchronizedmap
Hashtable
Wait a minute. When we choose a map, we should think about it, which is more suitable for our needs.
WeakReference and SoftReference in Java