Recycle principles of Android system events

Source: Internet
Author: User

Recently, some functional jar packages have been encapsulated. Because some actions need to be generated and some callbacks need to be provided to the caller, events and listeners are used.

For example, in draglistener and dragevent, each drag action triggers a dragevent event at the beginning, and a new dragevent object is required. Later, I felt like this was a waste of memory. Then I studied the motionevent class of the system and found the correct one.
Solution.


The motionevent construction method is anonymous and cannot be directly created. The interface for obtaining objects externally is a static obtain method, which can be obtained from a motionevent object or some variables. Why is it a good solution, because it provides a recycle Method
You can recycle the current object. You don't need to create a new object next time, just take it from its recycle pool.

The following describes several important variables in motionevent:

Java code

  1. // Variable
  2. Private motionevent mnext;
    // Point to the next object of the recycle Stack
  3. Private Boolean mrecycled;
    // Indicates whether the object is recycled.
  4. // Static variables
  5. Static private
    Final int max_recycled =
    10; // The maximum number of recyclables
  6. Static private object gresponerlock =
    New object (); // lock
  7. Static private
    Int gresponerused = 0; // Number of recycled objects in the recycle Stack
  8. Static private motionevent grecyclertop =
    NULL; // reclaim the stack top object of the stack
// Variable private motionevent mnext; // point to the next object of the recycle stack private Boolean mrecycled; // indicates whether the object is recycled/static variable static private final int max_recycled = 10; // maximum number of recyclables static private object gresponerlock = new object (); // The static private int gresponerused = 0 used to lock the entire class; // reclaim the number of objects in the stack. Static private motionevent gresponertop = NULL; // reclaim the top object of the stack.

Then there is a static obtain method:

Java code

  1. Static private motionevent obtain (){
  2. Synchronized (gresponerlock ){
    // Lock the entire class
  3. If (gresponertop =
    Null) {// If the stack top does not exist, a new
  4. Return
    New motionevent ();
  5. }
  6. Motionevent EV = gresponertop; // The stack top exists, and a reference eV is used to point to it.
  7. Gresponertop = eV. mnext; // then, the next object at the top of the stack is raised to the top of the stack.
  8. Gresponerused --; // reduce the number of objects in the recycle stack by one
  9. Ev. mrecycledlocation = NULL; // an exception with unknown function
  10. Ev. mrecycled = false; // The current object flag is not recycled.
  11. Return EV;
  12. }
  13. }
Static private motionevent obtain () {synchronized (gresponerlock) {// lock the entire class if (gresponertop = NULL) {// The stack top does not exist, A new return New motionevent ();} motionevent EV = gresponertop; // The stack top exists, and a reference eV is used to point to it gresponertop = eV. mnext; // then the next object at the top of the stack is mentioned to the gresponerused -- on the top of the stack; // The number of objects in the recycle stack is reduced by one eV. mrecycledlocation = NULL; // It is an exception that affects unknown eV. mrecycled = false; // the flag of the current object is return EV ;}}

The other several obtain Methods call the obtain () method to retrieve objects from the recycle stack and assign values.

Its recycle method is as follows:

Java code

  1. Public void recycle (){
  2. // Ensure that the recycle method is called only once
  3. If (track_recycled_location ){
  4. If (mrecycledlocation! =
    Null ){
  5. Throw
    New runtimeexception (tostring () + "recycled twice! ", Mrecycledlocation );
  6. }
  7. Mrecycledlocation = new runtimeexception ("Last recycled here ");
  8. } Else if (mrecycled ){
  9. Throw new runtimeexception (tostring () +
    "Recycled twice! ");
  10. }
  11. Synchronized (gresponerlock) {// lock class
  12. If (gresponerused <max_recycled ){
    // If the objects in the recycle stack have not reached the maximum value
  13. Gresponerused ++; // The number of elements in the recycle stack increases by 1.
  14. Mnumhistory = 0;
  15. // These two statements point the next of the current object to the top of the previous stack, and then place the current object to the top of the stack.
  16. Mnext = gresponertop;
  17. Gresponertop = this;
  18. }
  19. }
  20. }
Public void recycle () {// make sure that the recycle method only calls if (track_recycled_location) {If (mrecycledlocation! = NULL) {Throw new runtimeexception (tostring () + "recycled twice! ", Mrecycledlocation);} mrecycledlocation = new runtimeexception (" Last recycled here ");} else if (mrecycled) {Throw new runtimeexception (tostring () +" recycled twice! ");} Synchronized (grecyclerlock) {// lock class if (grecyclerused <max_recycled) {// if the objects in the recycle stack have not reached the maximum value grecyclerused ++; // increase the number of elements in the recycle stack by 1 mnumhistory = 0; // the two statements point the next of the current object to the top of the previous stack, then place the current object to the top of the stack. mnext = gresponertop; gresponertop = This ;}}}

Based on this idea, I also made an event. The same recovery principle greatly saves the memory usage when the event is triggered frequently.

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.