Android development experience Section 2: Recycling your small objects

Source: Internet
Author: User

The principle proposed in the previous article is to avoid creating unnecessary objects. For objects that occupy a large amount of resources, you can create them during program initialization and keep using them. Static constants can be used for some unchanged objects, such as strings and constants. However, during the development process, we will also encounter another situation where some objects occupy less resources but are frequently used (for example, the objects that record the coordinates of the screen points ), how should we deal with this situation?

This problem is actually complicated. We need to analyze the specific situation, but there are still some rules to follow.

Principle 1: Create an object pool to reuse small objects cyclically

For example:

 

1 publicclass testpoint
2 {
3 publicint MX;
4 publicint my;
5 Public testpoint (int x, int y)
6 {
7 set (x, y );
8}
9
10 publicvoid set (int x, int y)
11 {
12 MX = X;
13 my = y;
14}
15}

 

Use Time:

 

 

Publicvoid test ()
{
For (INT I = 0; I <10000; I ++)
{
Testpoint P = new testpoint (I, I );
...
}
}

 

Through analysis, the above Code will create 10000 new testpoint objects, which is of course unreasonable and can be optimized using the following methods:

 

 

1 publicclass testpoint
2 {
3 publicint MX;
4 publicint my;
5 Public testpoint (int x, int y)
6 {
7 set (x, y );
8}
9
10 publicvoid set (int x, int y)
11 {
12 MX = X;
13 my = y;
14}
15
16
17 /**
18 * Object pool
19 */
20 privatestatic arraylist <testpoint> spoints = NULL;
21
22 /**
23 * Get an object from the object pool. If the pool is empty, create a new object and initialize it.
24 * @ Param x coordinate
25 * @ Param y ordinate
26 * @ return object
27 */
28 publicstatic testpoint poppoint (int x, int y)
29 {
30 testpoint P = NULL;
31 if (spoints! = NULL & spoints. Size ()> 0)
32 {
33 P = spoints. Remove (0 );
34 p. Set (x, y );
35}
36 else
37 {
38 P = new testpoint (x, y );
39}
40 return P;
41}
42
43 /**
44 * return an object to the object pool. If the pool is empty, create a pool first.
45 * @ Param p the object to be returned
46 */
47 publicstaticvoid pushpoint (testpoint P)
48 {
49 If (spoints = NULL)
50 {
51 spoints = new arraylist <testpoint> ();
52}
53
54 spoints. Add (P );
55}
56
57}

 

In use:

 

 

Publicvoid Test2 ()
{
For (INT I = 0; I <10000; I ++)
{
Testpoint P = testpoint. poppoint (I, I); // get the object
...
Testpoint. pushpoint (p); // return the object
}
}

 

In this way, because the used objects are returned to the object pool, the actual number of object creation times decreases significantly when needed.

 

Principle 2: set an upper limit for your object pool

The previous Code also has limitations. For example, if the program logic results in a high object acquisition peak, there will be more or more objects in the pool. When the program object acquisition requirement is reduced, the objects in the pool become a burden of memory usage. One feasible method is to set an upper limit for the number of objects in the object pool. When returning the objects, Danggui finds that the objects in the pool have reached this upper limit and no longer puts them in the pool. As long as the upper limit is set properly, the pool memory usage and GC memory release pressure can find a better balance.

Principle 3: Protect your object pool

It should be emphasized that for the object pool shared by multiple threads, strict thread protection should be implemented for all static methods accessing the object pool, because arraylist operations are time-consuming and multi-thread access to them is a thread conflict.

 

Summary:

The use of the Object pool can effectively reuse small objects that are frequently used cyclically. If appropriate, it can greatly improve the program running efficiency and reduce the resource occupation of the program. However, please believe that there is no universally applicable optimal solution, and the specific problem must be analyzed.

 

Author: Wang Feng www.otlive.cn

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.