Analysis of actual content of Android garbage collection

Source: Internet
Author: User

The coding method in the Android mobile phone operating system is easier for Basic programmers. Because it is based on the Linux operating system. Here we will introduce the Android garbage collection mechanism to deepen your understanding of this system.

  • Android Virtual Devices apply to your deployment goals
  • Run commands in Android adb
  • Android menu construction skills
  • Android Image Browsing source code
  • Android Quick Start Summary

I personally think sp and wp are actually the automatic garbage collection mechanism implemented by Android for c ++. Specifically, sp and wp are actually just an interface for implementing the garbage collection function, for example, the overload of *,-> is to make it look the same as the real pointer, and the real 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 Android garbage collection, therefore, after all objects are declared, they have the ability to automatically release themselves. That is to say, smart pointers are actually our objects, 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:

 
 
  1. template< typename T> 
  2. sp< T>::sp(T* other)  
  3. : m_ptr(other)  
  4. {  
  5. if (other) other->incStrong(this);  

For example, we declare an object:

 
 
  1. 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:

 
 
  1. template< typename T> 
  2. sp< T>::~sp()  
  3. {  
  4. 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 strong reference Technology of an object is 0, the object will be automatically released.
Let's look at wp again. wp is the abbreviation of weak pointer. The principle of weak reference pointer is to apply Android garbage collection to reduce the memory usage of fat objects, let's first look at a constructor of wp:

 
 
  1. wp< T>::wp(T* other)  
  2. : m_ptr(other)  
  3. {  
  4. 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:

 
 
  1. template< typename T> 
  2. sp< T> wp< T>::promote() const  
  3. {  
  4. return sp< T>(m_ptr, m_refs);  
  5. }  
  6. template< typename T> 
  7. sp< T>::sp(T* p, weakref_type* refs)  
  8. : m_ptr((p && refs->attemptIncStrong(this)) ? p : 0)  
  9. {  

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:

 
 
  1. Wp <CameraHardwareInterface> CameraHardwareStub: singleton;
  2. Sp <CameraHardwareInterface> CameraHal: createInstance ()
  3. {
  4. LOG_FUNCTION_NAME
  5. If (singleton! = 0 ){
  6. Sp <CameraHardwareInterface> hardware = singleton. promote ();
  7. If (hardware! = 0 ){
  8. Return hardware;
  9. }
  10. }
  11. Sp <CameraHardwareInterface> hardware (new CameraHal (); // Add 1
  12. Singleton = hardware; // weak reference plus 1
  13. Return hardware; // value assignment constructor, with a strong reference plus 1
  14. }
  15. // The hardware is deleted and the reference is strongly reduced by 1

Android garbage collection will be introduced here.

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.