The principle and implementation of Java object Pooling technology

Source: Internet
Author: User
Tags constructor

Summary

On the basis of analyzing the basic principle of object pooling technology, this paper gives two ways to realize object pooling technology. The problems that should be paid attention to when using object pooling technology are also pointed out.

Keywords object pool; object pooling technology; Java objects; performance

Life-cycle analysis of Java objects

The lifecycle of a Java object consists of approximately three phases: object creation, object usage, and object cleanup. Therefore, the life cycle length of an object can be represented by an expression such as: T = T1 + T2 +t3. Where T1 represents the time when the object was created, T2 represents the time when the object was used, and T3 indicates its scavenging time. From this, we can see that only T2 is really effective time, and T1, T3 is the cost of the object itself. Let's look at the proportions of T1, T3 in the entire lifecycle of the object.

As we know, Java objects are created by constructors, and in this process all constructors in the chain of the constructor are invoked automatically. In addition, by default, when the constructor of a class is invoked, Java initializes the variable to the determined value: All objects are set to NULL, and integer variables (byte, short, int, long) are set to 0, The float and double variables are set to 0.0, and the logical values are set to false. So the time overhead of creating a new object with the New keyword is significant, as shown in table 1.

Table 1 Comparison of the time spent in some operations

example standardized time
local assignment i = n 1.0
instance assignment this.i = n 1.2
method call funct () 5.9
new object new Object () 980
new Build array 3100

As you can see from table 1, it takes 980 units to create a new object, 980 times times the local assignment time, 166 times times the method call time, and more time to create an array.

Then look at the process of clearing the object. One of the advantages of the Java language, we know, is that Java programmers do not have to explicitly release objects, as the C + + programmers do, and the automatic memory management system called the Garbage Collector (garbage Collector), timed or when memory is not highlighted, Automatically reclaims the memory occupied by garbage objects. The pros and cons of everything, while providing a great convenience for Java programmers, also bring a large performance overhead. This cost includes two aspects, the first is the object management overhead, the GC in order to be able to properly release the object, it must monitor the running state of each object, including the object's request, reference, referenced, assignment, and so on. Second, when the GC begins to recycle the "junk" object, the system suspends execution of the application and consumes the CPU alone.

Therefore, if you want to improve the performance of your application, you should minimize the number of times you create new objects, while minimizing T1, T3, which can be achieved through object pooling technology.

The basic principle of object pooling technology

The core of object pooling technology is two points: caching and sharing, for those objects that are frequently used, are not released immediately after use, but are cached for subsequent applications to reuse, reducing the number of objects created and disposed of, thereby improving the performance of the application. In fact, because object pooling technology limits objects to a certain number, it effectively reduces the overhead on application memory.

Implementing an object pool typically involves the following classes:

1 Object Pool Factory (Objectpoolfactory) class

This class is primarily used to manage object pools of the same type and settings (Objectpool), which typically contains two methods:

CreatePool: An object pool for creating specific types and settings;

Destroypool: For releasing the specified object pool;

At the same time to ensure a single instance of Objectpoolfactory, you can use the singleton design pattern, see the following getinstance method implementation:

public static ObjectPoolFactory getInstance() {
 if (poolFactory == null) {
  poolFactory = new ObjectPoolFactory();
 }
 return poolFactory;
}

2) Parameter object (Parameterobject) class

This class is used primarily to encapsulate some of the property parameters of the created object pool, such as the maximum number of objects that can be stored in the pool (maxcount), the minimum (Mincount), and so on.

3) Object Pool (Objectpool) class

Used to manage the loan and return of the objects to be pooled, and notify the poolableobjectfactory to complete the appropriate work. It generally contains the following two methods:

GetObject: Used to borrow objects from a pool;

Returnobject: Returns the pooled object to the pool and notifies all threads in the waiting state;

4 Pool Object Factory (Poolableobjectfactory) class

This class is primarily responsible for managing the lifecycle of the pooled objects, in simple terms, generally including the creation and destruction of objects. Like Objectpoolfactory, it can also be implemented as a single instance.

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.