The "Java" Itoo project's often neglected performance optimization

Source: Internet
Author: User

Itoo V3.0It's going to be over soon.,functionally, basically, the development is complete.,but put it inJBosswhen deploying in the,feel particularly slow when used,if it's a lot of data,,The word "slow" to the fullest..Most of this slow problem is due to the use of the underlying systemJPACascade caused by,every time the query,as long as there is an associated table,will all be queried.,to issue a large version of theHQLStatement,It 's kinda scary to watch, too..out to optimizeJPAcascading issues,you can also start with code.,consider from the following aspects.


(1) reduce the object life cycle

the life cycle of an object has such a calculation formula: Object life Cycle = Destruction Time - creation Time

actually reducing the object life cycle has 2 a way : destroy objects as early as possible and create objects as late as possible. Most people only pay attention to destroying objects as early as possible, but few pay attention to creating objects as late as possible. In particular, objects that consume large memory should be created as late as possible. You only start creating it when you really need it . In addition to objects, such as connecting to a database. Only connect to the database when all is ready , and shut down immediately with the completion of the database connection. Minimum time to use the connection, including data flow


(2) The sooner the thread starts , the better.

When the thread is ready to work, the thread is started, and the sooner the thread starts, the faster it can get the data.


(3) Replace a single string with a character

Stringbuilderstr=new StringBuilder ();

so str.append ("M") Better str.append (' m ') Fast

(4) StringBuilder can be continuously increased

StringBuilder of the Append method returns itself can continue to increase without having to re-obtain StringBuilder Object

Stringbuilderstr=new StringBuilder (); Str.append ("abc"); Str.append ("ddd");

can be written

Stringbuilderstr=new StringBuilder (); Str.append ("abc"). Append ("ddd");


(5) no sequential traversal of an array

In fact, there are times when you iterate through the array-independent order, simply iterate over the array object.

The usual wording is

Int[] A=newint[10000];for (inti=0,len=a.length;i<len;i++) {    int b=a[i];          and then process}

In fact, I wrote this when I was in an unrelated sequence of iterating through an array. :

Int[] A=newint[10000];for (inti=-1,len=a.length;++i<len;) {    int b=a[i];           and then process}

Sometimes it's written like this.

Int[] A=newint[10000];for (inti=a.length;i>0;) {    int b=a[--i];           and then process}

make full use of ++i or --I. The last one doesn't count . Len, directly - I. values can be evaluated, compared with constants 0 Compare

(6) don't have any extra action.

Stringa=geta (), B=GETB (); if (a!=null&&b!=null) {}

Better for

String A=geta (); if (a!=null) {     String b=getb ();//So that A is null, you do not need to get B.     if (b!=null) {     }}

There's something like

if (GetValue ()!=2&&getvalue ()!=0) {...}

Write directly as

Intvalue=getvalue (); if (value!=2&&value!=0) {...}

to invoke the method wasted performance, the second is to access the object properties, the fastest is the local variables, using subscript access to the array system also to determine the cross-boundary problem also wasted performance .


(7) There are also some arrays for copying ' System.arraycopy () , and using local variables is faster than using global variables. You can see the non-denaturing String class source code will know


(8) about Locks Java There are read and write locks and the ability to try to acquire a lock, you can see Readwritelock class


(9) A memory leak occurs, and the analysis discovers the global GC (gabage Collection) frequently, you may consider checking your code for large List , Map , arrays, and so on, using the corresponding Clean method to mitigate GC pressure, improve GC efficiency.


(Ten) io when the operation is frequent, finally The code must have Outputstream.flush off, in Web This is especially true in applications


Wholeheartedly for the name of the software services, you can not let customers wait too long.

The "Java" Itoo project's often neglected performance optimization

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.