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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the citations in java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn what are the citations in java.
I. strong quotation
Concept: when an object is unreachable, it can be recycled.
/ * strong reference. When the strong reference pointer does not exist, the object will be recycled * or it can be understood that when the ROOT reference disappears, the object will be recycled * / public class StrongReference {/ * jvm will call back the method * @ throws Throwable * / @ Override protected void finalize () throws Throwable {System.out.println ("gc doing now!") when gc is executed. } public static void main (String [] args) {StrongReference strongReference = new StrongReference (); / / remove the reference pointer strongReference = null; / / manually call gc System.gc ();}}
II. Soft citation
Concept: when there is insufficient memory space, it will be reclaimed.
/ * Xmx2oM sets the maximum heap memory to 20m * soft references will gc when the memory space is insufficient. Thus, the soft reference object * / public class MySoftReference {public static void main (String [] args) {SoftReference softReference = new SoftReference (new byte [1024 / 1024 / 10]); System.out.println ("before the first gc: byte []:" + softReference.get ()); System.gc (); System.out.println ("after the first gc execution: byte []:" + softReference.get ()) Byte [] bytes = new byte [1024 / 1024 / 12]; System.out.println ("after insufficient memory: byte []:" + softReference.get ());}}
Supplement to the above code: in the source code, I set the virtual machine parameter-Xmx to 20m. The two byte arrays that I have new have both exceeded 10m. The target object will be stored in the old age. (according to the Cenozoic era: old age = 1:2.) probably the memory distribution is. In the old age, 20 * 2 Compact 3 is about 13.3m. The younger generation is about 20 * 1 Compact and 3 is about 6.7m. Among them, the young generation eden:s0:s1 = 8:1:1, so Eden is about 5.3m. The So/S1 is about 0.67m. As shown in the following figure, it is basically consistent with the calculated results. As for the extra part, it is my understanding that jvm self-adjusts the calculated heap size.
Based on the analysis of heap memory. You can see. The object of 10m was loaded in the old age for the first time. The second time I want to load the 12m object. It won't fit. Because it is a soft reference. This part of the content will be recycled first when jvm gc. So there is no OOM problem here.
Third, weak quotation (more important, one of the common questions in the interview)
Note: weak citation is used in ThreadLocal. Can cause memory leaks. See (https://my.oschina.net/u/4141142/blog/4523523) for details.
Concept: jvm ignores the reference. The object can be obtained an instance of the object by a weakly referenced object. When jvm executes gc, it will be recycled directly. (advance condition: no strong reference exists).
Example 1:
/ * weak reference virtual machine ignores the reference. As long as the gc must be recycled (provided that the object is not strongly referenced) * / public class Test {public static void main (String [] args) {Teacher teacher = new Teacher (); WeakReference weakReference = new WeakReference (teacher); System.out.println (weakReference.get ()); / / if teacher is not set to null, there is also a strong reference. Will not be recycled by gc / / teacher = null; System.gc (); System.out.println (weakReference.get ());}
Example 2:
/ * the User object holds a strong reference to the Teacher object. * when teacher is set to null. The strong application of teacher disappears. * User's strong reference to Teacher is still there. So user.getTeacher () is not null * so teacher is not recycled by gc. * * when user is also set to null. There is no strong reference from user to teacher. * teacher will be recycled by gc at this time * / public class Test1 {public static void main (String [] args) {User user = new User (); Teacher teacher = new Teacher (); teacher.setName ("zs"); user.setTeacher (teacher); WeakReference weakReference = new WeakReference (teacher); System.out.println (System.identityHashCode (weakReference.get (); System.out.println (System.identityHashCode (teacher)) System.out.println (System.identityHashCode (user.getTeacher (); teacher = null; System.out.println (user.getTeacher (). GetName ()); / / when user is set to null, the strong reference to teacher disappears. At this point the teacher will be recycled. User = null; System.gc (); System.out.println (weakReference.get ());}}
Example 3:
Public class People extends WeakReference {public People (Teacher referent) {super (referent);}} / * People inherits from WeakReference People is also a virtual reference object. * so when teacher is set to null, the strong reference pointer is cleared. * teacher will be recycled by gc. * / public class Test2 {public static void main (String [] args) {Teacher teacher = new Teacher (); People people = new People (teacher); teacher = null; System.gc (); System.out.println (people.get ());}}
IV. False quotation
Concept: this quotation is very creepy. When an object is referenced by a virtual reference. You cannot get the object of the instance. Only when the object is recycled will it be received and stored in the queue. Basically used to operate direct memory to use. Can be used to monitor the gc of some important objects. Or the frequency of monitoring gc (not as simple and intuitive as printing gc logs).
The following jvm parameter-Xmx20M. Of course, it can also be smaller, mainly to see the effect of gc.
/ * * Virtual reference * compare chicken ribs, virtual reference objects can not get () out. But directly manipulate the operating system memory. * the target object of the virtual reference, after being collected, will be stored in the queue * A new thread needs to be started to listen to the queue for data. If there is data, the direct memory will be reclaimed. * / public class MyPhantomReference {private static final List LIST = new LinkedList (); private static final ReferenceQueue QUEUE = new ReferenceQueue (); public static void main (String [] args) {PhantomReference phantomReference = new PhantomReference (new MyPhantomReference (), QUEUE); new Thread (()-> {while (true) {LIST.add (new byte [1024x1024]) Try {Thread.sleep (1000);} catch (Exception e) {e.printStackTrace (); Thread.currentThread () .interrupt ();} System.out.println (phantomReference.get ());}}) .start () New Thread (()-> {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: 206
*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.