Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the four references of JVM virtual machine and the practical method of GC

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "what are the four references to the JVM virtual machine and what are the practical methods of GC". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. background

Java memory collection does not need to be responsible for the programmer, JVM will launch Java GC to complete garbage collection if necessary. Java allows us to control the life cycle of objects, providing us with four ways of citing. The strength of references from strong to weak are: strong reference, soft reference, weak reference and virtual reference.

2. Introduction 1. Strong reference to StrongReference

StrongReference is the default reference form of Java and does not need to be displayed when used. No matter how tight the system resources are, Java GC will not actively recycle objects with strong references that are used by strong references.

Public class StrongReferenceTest {public static int M = 1024024; public static void printlnMemory (String tag) {Runtime runtime = Runtime.getRuntime (); int M = StrongReferenceTest.M; System.out.println ("\ n" + tag+ ":"); System.out.println (runtime.freeMemory () / M + "M (free) /" + runtime.totalMemory () / M + "M (total)") } public static void main (String [] args) {StrongReferenceTest.printlnMemory ("1. Original available memory and total memory "); / / instantiate an array of 10m and establish a strong reference with strongReference byte [] strongReference = new byte [10*StrongReferenceTest.M]; StrongReferenceTest.printlnMemory (" 2. After instantiating an array of 10m and establishing a strong reference "); System.out.println (" strongReference: "+ strongReference); System.gc (); StrongReferenceTest.printlnMemory (" after 3.GC "); System.out.println (" strongReference: "+ strongReference); / / strongReference = null;, the strong reference breaks strongReference = null; StrongReferenceTest.printlnMemory (" 4. After strong reference is broken "); System.out.println (" strongReference: "+ strongReference); System.gc (); StrongReferenceTest.printlnMemory (" after 5.GC "); System.out.println (" strongReference: "+ strongReference);}}

Running result:

Cdn.xitu.io/2018/1/7/160cd0dc536b2384?imageView2/0/w/1280/h/960/format/webp/ignore-error/1 ">

two。 Weak reference WeakReference

If an object has only weak references, no matter whether there is sufficient memory or not, the object after Java GC will be automatically reclaimed if it has only weak references.

Public class WeakReferenceTest {public static int M = 1024024; public static void printlnMemory (String tag) {Runtime runtime = Runtime.getRuntime (); int M = WeakReferenceTest.M; System.out.println ("\ n" + tag+ ":"); System.out.println (runtime.freeMemory () / M + "M (free) /" + runtime.totalMemory () / M + "M (total)") } public static void main (String [] args) {WeakReferenceTest.printlnMemory ("1. Original available memory and total memory "); / / create weak reference WeakReferenceweakRerference = new WeakReference (new byte [10*WeakReferenceTest.M]); WeakReferenceTest.printlnMemory (" 2. Instantiate an array of 10m and create a weak reference "); System.out.println (" weakRerference.get (): "+ weakRerference.get ()); System.gc (); StrongReferenceTest.printlnMemory (" after 3.GC "); System.out.println (" weakRerference.get (): "+ weakRerference.get ());}} run result: 3. Soft references SoftReference soft references are basically the same as weak references, with the main difference being that soft references are not recycled until they run out of memory. If an object has only soft references, Java GC will not reclaim it when it has enough memory, but it will only be reclaimed when it is out of memory. Public class SoftReferenceTest {public static int M = 1024024; public static void printlnMemory (String tag) {Runtime runtime = Runtime.getRuntime (); int M = StrongReferenceTest.M; System.out.println ("\ n" + tag+ ":"); System.out.println (runtime.freeMemory () / M + "M (free) /" + runtime.totalMemory () / M + "M (total)") } public static void main (String [] args) {SoftReferenceTest.printlnMemory ("1. Original available memory and total memory "); / / establish soft reference SoftReferencesoftRerference = new SoftReference (new byte [10*SoftReferenceTest.M]); SoftReferenceTest.printlnMemory (" 2. Instantiate an array of 10m and create a soft reference "); System.out.println (" softRerference.get (): "+ softRerference.get ()); System.gc (); SoftReferenceTest.printlnMemory (" 3. System.out.println ("softRerference.get ():" + softRerference.get ()); System.out.println ("softRerference.get ():" + softRerference.get ()); / / instantiate an array of 4m so that memory is not enough, and establish a soft reference / / free=10M=4M+10M-4M to prove that when memory availability is insufficient, byte [10m] after GC is recycled SoftReferencesoftRerference2 = new SoftReference (new byte [4*SoftReferenceTest.M]) SoftReferenceTest.printlnMemory ("4. After instantiating a 4m array "); System.out.println (" softRerference.get (): "+ softRerference.get ()); System.out.println (" softRerference2.get (): "+ softRerference2.get ());}} run result: 4. As you can see from the source code of the PhantomReference class, the virtual reference PhantomReference will only return null whenever its get () method is returned. So when using virtual references alone, it doesn't make sense and needs to be used in conjunction with the reference queue ReferenceQueue class. If an object has only a virtual reference when executing a Java GC, the object is added to the ReferenceQueue associated with it. Public class PhantomReferenceTest {public static int M = 1024024; public static void printlnMemory (String tag) {Runtime runtime = Runtime.getRuntime (); int M = PhantomReferenceTest.M; System.out.println ("\ n" + tag+ ":"); System.out.println (runtime.freeMemory () / M + "M (free) /" + runtime.totalMemory () / M + "M (total)") } public static void main (String [] args) throws InterruptedException {PhantomReferenceTest.printlnMemory ("1. Original available memory and total memory "); byte [] object = new byte [10*PhantomReferenceTest.M]; PhantomReferenceTest.printlnMemory (" 2. After instantiating the array of 10m "); / / establishing a virtual reference ReferenceQueuereferenceQueue = new ReferenceQueue (); PhantomReferencephantomReference = new PhantomReference (object,referenceQueue); PhantomReferenceTest.printlnMemory (" 3. After establishing virtual reference "); System.out.println (" phantomReference: "+ phantomReference); System.out.println (" phantomReference.get (): "+ phantomReference.get ()); System.out.println (" referenceQueue.poll (): "+ referenceQueue.poll ()); / / break the strong reference object of byte [10*PhantomReferenceTest.M] = null; PhantomReferenceTest.printlnMemory (" 4. Execute object = null; strong reference after breaking "); System.gc (); PhantomReferenceTest.printlnMemory (" after 5.GC "); System.out.println (" phantomReference: "+ phantomReference); System.out.println (" phantomReference.get (): "+ phantomReference.get ()); System.out.println (" referenceQueue.poll (): "+ referenceQueue.poll ()) / / break the virtual reference phantomReference = null; System.gc (); PhantomReferenceTest.printlnMemory ("6. GC after breaking virtual reference "); System.out.println (" phantomReference: "+ phantomReference); System.out.println (" referenceQueue.poll (): "+ referenceQueue.poll ());}} run result: 3. Summary strong reference is the default reference form of Java, and there is no need to display the definition when using it, which is the most commonly used reference method. No matter how tight the system resources are, Java GC does not actively recycle objects with strong references. Weak references and soft references are generally used when the reference object is non-essential. The difference between them is that objects associated with weak references are always reclaimed during garbage collection, and objects associated with soft references are recycled only when they run out of memory. The vaguely referenced get () method always gets the null and cannot get the object instance. Java GC puts the vaguely referenced objects into the reference queue. It can be used to do some additional resource cleaning or thing rollback when the object is reclaimed. An instance of the reference object cannot be obtained from the virtual reference. Its usage is special, so virtual references are not put into the table for comparison here. Here to strong reference, weak reference, soft reference comparison: reference article https://segmentfault.com/a/1190000009707894https://www.cnblogs.com/hysum/p/7100874.htmlhttp://c.biancheng.net/view/939.htmlhttps://www.runoob.com/https://blog.csdn.net/android_hl/article/details/53228348"JVM virtual machine four references and what are GC practice methods, this is the end of the content, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report