In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to learn the four reference types of objects in Android". In the daily operation, I believe that many people have doubts about how to learn the four reference types of objects in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how to learn the four reference types of objects in Android". Next, please follow the editor to study!
Catalogue
I. detailed explanation of quotation
1. Strongly quote StrongReference
2. Weak citation
3. Soft reference
4. Virtual reference
Foreword:
In Java, everything is regarded as an object, and references are used to manipulate objects; in JDK1.2, the object reference is divided into four levels, so that the program can be more flexible to control its life cycle, the level from high to bottom is: strong > soft > weak > virtual reference; and GC garbage collector (Garbage Collection) has different processing methods for different types, understanding these treatments will help us to write higher quality code.
I. detailed explanation of quotation
1. Strongly quote StrongReference
Strong references are the most commonly used references. If an object has a strong reference, the garbage collector will never reclaim it. When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error to make the program terminate abnormally, rather than solve the problem of insufficient memory by randomly recycling objects with strong references. For example, the variable s in the code String s = "abc" is a strong reference to the string object "abc". As long as you assign a null value null to the strong reference object s, the object can be reclaimed by the garbage collector; because the object no longer contains other strong references
/ / str indicates a strong reference, pointing to the object new String () String str = new String (); 2. Weak reference
Weak references (WeakReference) are reference types that are weaker than soft references, similar to soft references, except that weak references cannot prevent garbage collection. When the garbage collection mechanism is running, if an object's reference is a weak reference, the object will be reclaimed regardless of whether there is enough memory space. Weak references are often used to prevent memory leaks, most commonly caused by singletons and Handler
/ / weak reference instance WeakReference weakReference = new WeakReference (context); / / get the reference saved by weak reference Context ctx = weakReference.get (); 3. Soft reference
SoftReference: soft reference-> when the virtual machine runs out of memory, the object it points to will be reclaimed; when you need to get the object, you can call the get method
Soft reference objects will not be quickly reclaimed by jvm. Jvm will judge when to recycle according to the current heap usage, and will be recycled only when the frequency of heap usage approaches the threshold.
Basic usage:
MySoftReference msf = new MySoftReference (); SoftReference sf = new SoftReference (msf); MySoftReference mySoftReference = (MySoftReference) sf.get ()
Basic features:
If there is enough memory, soft references will not be reclaimed by jvm
If there is not enough memory, the reference will be recycled according to the usage of the stack
Unrecycled soft references can always be occupied by the program.
Soft references can be used in conjunction with reference queues (ReferenceQueue) to implement memory-strapped caching
If the soft reference object is recycled, the Java virtual machine adds the soft reference object to the reference queue associated with it
ReferenceQueue rq = new ReferenceQueue (); SoftReference sf = new SoftReference (msf,rf)
When the soft reference object is reclaimed, the strongly referenced Reference is stored in the ReferenceQueue queue, and then you can use poll () to determine whether the current reference queue has an object that has lost the soft reference. If the queue is empty, it will return a null, otherwise the method will return a Reference object in front of the queue. You can detect which soft reference object is recycled and then clear it
Reference reference= null; while ((reference== (EmployeeRef) rq.poll () {/ / clear operation reference= null; System.gc ();} 4, virtual reference
Virtual references (PhantomReference) are the weakest references, and an object with a virtual reference is almost the same as no reference and can be reclaimed by the garbage collector at any time. All references obtained through the get () method of the virtual reference will fail (null), and the virtual reference must be used with the reference queue ReferenceQueue
The purpose of the ReferenceQueue reference queue is to track the garbage collection process. When the garbage collector collects an object, if it finds that it has a virtual reference, it destroys the object after the collection and adds the object to which the virtual reference points to the reference queue. We can only judge whether the virtual reference is recycled by GC only by whether the virtual reference is added to the ReferenceQueue, which is the only way to determine whether the object is recycled.
There is a finalize () method in the Object class of Java. The principle is that once the garbage collector is ready to release the memory occupied by the object, the finalize () method will be called first, and the memory occupied by the object will not be really reclaimed until the next garbage collection action occurs. But the problem is that the virtual machine cannot guarantee when finalize () will be called, because the GC runtime is not fixed.
This problem can be solved by using virtual references, which are mainly used to track garbage collection activities and to achieve fine memory usage control, which is of great significance to Android.
/ / reference queue ReferenceQueue queue = new ReferenceQueue (); / / virtual reference PhantomReference phantomReference = new PhantomReference (new Object (), queue); Log.e (TAG, "virtual reference: PhantomReference = =" + phantomReference.get ()); / / system garbage collection System.gc (); System.runFinalization ()
The reference obtained by phantomReference.get () is always null, calling the system to collect garbage, queue.poll () gets the saved reference object and removes it from the queue
The virtual reference cannot get the reference of the target through the get () method. It always returns null, the source code:
Public T get () {return null;}
Summary:
Strong reference (StrongReference): does not automatically recycle, the most difficult to be recycled by GC, would rather throw an exception than recycle the object pointed to by the strong reference; any scenario
Soft references (SoftReference): when memory is low, GC will recycle objects that soft references point to are less used and have been replaced by LruCache
Weak reference (WeakReference): regardless of insufficient memory, objects pointed to by weak references can be recycled as long as they are GC; often used to avoid memory leaks
Virtual reference (PhantomReference): can be recycled at any time, also known as ghost reference, which is equivalent to not pointing to any instance reference; tracking whether an object is recycled is rarely used
At this point, the study on "how to learn deeply about the four reference types of objects in Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.