Working: Creating objects for Java performance optimization

Source: Internet
Author: User
Creation of 1.Java performance optimization objects
For every Java programmer knows how to create objects, but do you know how to create them to improve the performance of your application?
Do you know the application rules for creating objects?
(1). To avoid creating objects in the loop body as much as possible .
(2). Try to make the object conform to the garbage collection standard in time.
(3). Do not take too deep an inheritance level.
(4). Accessing local variables is better than accessing variables in the class.
For example:
Vector v=new vector ();
for (int i=0; i<100;i++) {
Object Obj=new object ();

}
This kind of writing is not strange to everyone. But this will waste a lot of memory space. The correct method is as follows.
Vector v=new vector ();
Object Obj=null;
for (int i=0; i<100;i++) {
Obj=new Object ();

}
This saves a reference to an object in memory, thereby reducing the amount of memory space wasted.

And also:
Do not initialize an object more than once. This also leads to significant memory overhead, which offsets system performance.
public class Test () {
Private HashMap map=new HashMap ();
Public Test () {
Map=new HashMap ()
}
}
The above points are well understood. But there are a lot of novice will appear above the problem Oh!!!!

Working: Creating objects for Java performance optimization

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.