In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the citation methods in Java". In the daily operation, I believe that many people have doubts about the citation methods in Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the citation methods in Java?" Next, please follow the editor to study!
Four citation methods in java are suitable for different scenarios. It is important to understand virtual references and combine text and code.
Strong citation
Strongly referenced objects will not be reclaimed by the garbage collector. JVM would rather throw OOM than recycle strongly referenced objects.
Mm = new M (); soft reference
When there is enough heap space, GC will not reclaim soft-referenced objects. When there is insufficient heap space to allocate new space, triggering GC will reclaim these objects, which is usually used in cache and other areas. Use soft references to cache objects, release this space when you run out of space, and reload it from DB when you need to use it again.
In addition, the soft reference can be used with the queue (ReferenceQueue). If the object referenced by the soft reference is garbage collected, JVM will add the soft reference to the reference queue associated with it.
/ * soft reference: generally used in cache. If there is insufficient space, GC will reclaim it * running parameter-Xmx200m-XX:+PrintGC * Created by etfox on 17:06 on 2021-03-01 * * / public class TestSoftReference {public static void main (String [] ags) throws InterruptedException {/ / 100m cache data byte [] cacheData = new byte [100 * 1024 * 1024] / hold cached data with soft references SoftReference cacheRef = new SoftReference (cacheData); / / remove strong references from cached data cacheData = null; System.out.println ("before the first GC" + cacheData); System.out.println ("before the first GC" + cacheRef.get ()); / / check the collection of objects after a GC () / / wait for GC Thread.sleep (500); System.out.println ("after the first GC" + cacheData); System.out.println ("after the first GC" + cacheRef.get ()); / / before assigning a 120m object, check the recovery of cache objects byte [] newData = new byte [120 * 1024 * 1024]; System.out.println ("after allocation" + cacheData) System.out.println ("after assignment" + cacheRef.get ()) } console== > [GC (Allocation Failure) 4120K-> 1055K (15872K), 0.0016237 secs] [Full GC (Allocation Failure) 1055K-> 1054K (15872K), 0.0015426 secs] before the first GC before the first GC [B@1973e9b [Full GC (System.gc ()) 103583K-> 103455K (118340K), 0.0015559 secs] after the first GC [B@1973e9b [GC (Allocation Failure) 105575K-> 103455K (198016K), 0.0001733 secs] [Full GC (Allocation Failure) 103455K-> 103455K (198016K) 0.0011860 secs] [Full GC (Allocation Failure) 103455K-> 819K (198016K), 0.0012080 secs] Post-allocated null Post-assigned null weak reference
A weakly referenced reference object is cleared every time it is GC, regardless of the current heap memory size. If this object is used occasionally and you want to get it when you need it, but do not want to affect the collection of the object, you can use weak references to describe the object.
Of course, weak references can also be used in conjunction with event queues.
/ * weak reference: if the object is used occasionally and you want to get (get) when you use it, but do not want to affect the garbage collection of this object * you can introduce the queue * Created by etfox on 17:59 on 2021-03-01 * * / public class TestWeakReference {public static void main (String [] args) throws InterruptedException {/ / 100m cache data byte [] cacheData = new byte [100 * 1024 * 1024] / / hold cached data with soft references WeakReference cacheRef = new WeakReference (cacheData); System.out.println ("before the first GC" + cacheData); System.out.println ("before the first GC" + cacheRef.get ()); / / check the collection of objects after a GC; / / wait for GC Thread.sleep (500) System.out.println ("after the first GC" + cacheData); System.out.println ("after the first GC" + cacheRef.get ()); / / remove the strong reference from the cached data cacheData = null; System.gc (); / / wait for GC Thread.sleep (500); System.out.println ("after the second GC" + cacheData) System.out.println (after the second GC + cacheRef.get ()) } console== > [GC (Allocation Failure) 3912K-> 1025K (15872K), 0.0016372 secs] [Full GC (Allocation Failure) 1025K-> 1024K (15872K), 0.0014157 secs] before the first GC [before the B@1973e9b first GC [B@1973e9b [Full GC (System.gc ()) 103723K-> 103456K (118340K), 0.0016463 secs] after the first GC [B@1973e9b [Full GC (System.gc ()) 105601K-> 1056K (198016K)] 0.0012771 secs] null after the second GC null virtual reference after the second GC
A false reference, as its name implies, is illusory, and the falsely referenced object cannot be obtained at the time of get. It also has no applicable scenario in our daily development, and its main function is to track the life cycle of an object (usually objects in direct memory (objects in JDK1.5 Java can be allocated directly in memory in addition to the space managed by JVM). Generally used in JVM development, it is mainly used to manage direct memory Because direct memory usually cannot be managed by GC (C++ delete is finished), special handling is required.
For example, ByteBuffer.allocateDirect (1024) of NIO; allocates memory to direct memory space, generally speaking, the data read from the network card is read into direct memory by the operating system, and when needed, it needs to be copied to the JVM heap space. If you do not use allocateDirect, you need a copy process, which is very time-consuming.
/ / |-|
/ / | Network card | = > | Direct memory | = = copy = = > | JVM heap space |
/ / |-|
The use of direct memory omits the process of copying, commonly known as nio's zero copy, but objects in direct memory cannot manage this space through the normal GC process when they are not needed, so virtual references are used.
Explanation:
Virtual references need to be used with an event queue. When JVM GC, it does not mean to clean up the virtual references, but to put the virtual references into the event queue, and the garbage collection thread will check the event queue from time to time to see if the reference collection process needs to do some follow-up processing (such as cleaning up objects in direct memory). This is the function and meaning of false quotation.
/ * Virtual reference: you can track the life cycle of an object through the queue. Generally, virtual reference is only used when writing about JVM. It is mainly used to manage direct memory (C++ delete is finished at once) *-Xmx20m-XX:+PrintGC * Created by etfox on 12:14 on 2021-03-03 * * / public class TestPhantomReference {private static final List LIST = new LinkedList (); private static final ReferenceQueue QUEUE = new ReferenceQueue () Public static void main (String [] args) throws InterruptedException {final PhantomReference phantomReference = new PhantomReference (new M (), QUEUE); / / always returns null System.out.println (phantomReference.get ()); / / ByteBuffer.allocateDirect (1024) Allocate memory to the operating system space, direct memory space, JDK 1.5 / data usually read from the network card, usually read by the system into direct memory, if you want to use, you need to copy to the JVM heap space / / if you do not use allocateDirect (direct memory), you need a copy process It is very time-consuming / / |-| / | Network card | = > | Direct memory | = = copy = = > | JVM heap space | / / |-| |-|-| -| / / the copy process is omitted by using direct memory Commonly known as nio zero copy / /, objects in direct memory cannot be cleaned by JVM GC when they are no longer needed, so virtual references / / virtual references need to be used with a queue. JVM GC does not mean that virtual references will be cleaned up. Instead, the virtual reference will be put into the event queue / / the garbage collection thread can check the event queue from time to time to see if the reference collection process needs to do some aftertreatment (such as cleaning up the object that is directly in memory) / / this is the function and meaning of virtual reference ByteBuffer b = ByteBuffer.allocateDirect (1024) New Thread (new Runnable () {@ Override public void run () {while (true) {LIST.add (new byte [1024 * 1024]); try {Thread.sleep (1000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println (phantomReference.get ());}) .start () / / simulate garbage collection thread new Thread (new Runnable () {@ Override public void run () {while (true) {Reference)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.