Android's garbage collection mechanism (Android refbase class (sp wp ))

Source: Internet
Author: User

From: http://blog.chinaunix.net/u3/90973/showart_2163813.html

 

SP and WP implementation process ~~
I personally think SP and WP are actuallyAndroidAutomatically implemented for C ++JunkReclaimMechanismTo be specific to the internal implementation, SP and WP are actually just an interface for implementing the garbage collection function, for example, the overload of *,-> is to make it look like a real pointer, the real implementation of garbage collection is the refbase base class. The Directory of this part of code is:/frameworks/base/include/utils/refbase. h
First, all classes will inherit the refbase class, because it achieves all the functions required for garbage collection, so in fact, all objects are declared and can be automatically released, that is to say, in fact, the smart pointer is our object, and it will maintain a count of strong references and weak references. Once the strong reference count is 0, it will release itself.
First, let's look at sp. SP is actually not the abbreviation of smart pointer, but strong pointer. It actually contains a pointer to the object. Let's take a look at a constructor of SP:
Template <typename T>
Sp <t>: SP (T * Other)
: M_ptr (other)
{
If (other) Other-> incstrong (this );
}
For example, we declare an object:
Sp <camerahardwareinterface> hardware (New camerahal ());
In fact, the SP pointer does not perform any operations on itself, that is, a pointer value, contains a pointer to the object, but the object will add a strong reference count to the object itself, the implementation of this incstrong is in the refbase class. When a new camerahal object is created and its value is given to sp <camerahardwareinterface>, its strong reference count changes from 0 to 1. Therefore, every time an object is assigned a sp pointer, the strong reference count of the object will be increased by 1. Next let's look at the SP destructor:
Template <typename T>
Sp <t> ::~ SP ()
{
If (m_ptr) m_ptr-> decstrong (this );
}
In fact, each time you delete an SP object, the strong reference count of the object pointed by the SP pointer will be reduced by one. When the object is strongly referencedTechnologyWhen the value is 0, this object will be automatically released.
WP is short for weak pointer. WP is actually a pointer class of the weak reference type.JavaIt is often used. A weak reference is the owner of an object reference. A weak reference can be used to maintain the reference to the object, but it will not prevent it from being recycled. If an object only has weak references, it becomes a candidate object for garbage collection, just like there are no remaining references, and once the object is deleted, all weak references will also be clear. Weak references are suitableDataA class with many Members and relatively easy re-creation, also known as the fat class, can reference objects by creating weak references, but does not prevent it from being recycled by garbage collection, achieve a certain balance in memory usage.

In Android, The promote function in the WP class actually upgrades a weak reference to a strong reference. Whether SP or WP, Android uses the existing C ++ features to solve memory usage and recovery.

To reduce the memory usage of fat objects, Let's first look at a WP constructor:
WP <t>: WP (T * Other)
: M_ptr (other)
{
If (other) m_refs = Other-> createweak (this );
}
Like sp, it actually only assigns values to pointers. the object itself adds a weak reference count for itself, and WP also contains a m_ref pointer, this pointer is mainly used to upgrade WP to SP:
Template <typename T>
Sp <t> WP <t>: Promote () const
{
Return sp <t> (m_ptr, m_refs );
}

Template <typename T>
Sp <t>: SP (T * P, weakref_type * refs)
: M_ptr (P & refs-> attemptincstrong (this ))? P: 0)
{
}
In fact, the only thing we can do with the WP pointer is to upgrade the WP pointer to an SP pointer, and then determine whether the upgrade is successful. If the upgrade succeeds, the object still exists, if a failure occurs, the object is released. The WP pointer is used in a single example. Make sure that there is only one mhardware object, for example:
WP <camerahardwareinterface> camerahardwarestub: Singleton;
Sp <camerahardwareinterface> camerahal: createinstance ()
{
Log_function_name

If (Singleton! = 0 ){
Sp <camerahardwareinterface> hardware = singleton. Promote ();
If (hardware! = 0 ){
Return hardware;
}
}

Sp <camerahardwareinterface> hardware (New camerahal (); // Add 1

Singleton = hardware; // weak reference plus 1
Return hardware; // value assignment constructor, with a strong reference plus 1
}
// The hardware is deleted and the reference is strongly reduced by 1

 

Keywords:

Android SP WP

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.