How to Reduce memory usage when a large number of Java threads are running

Source: Internet
Author: User
Tags openid

The project was released to the production server. After a period of tracking, we found that the memory usage of items on the server is growing slowly, but this problem was not found during local pressure, or even though a stress test ticket is made, I did not pay attention to it because the memory growth in the environment is not obvious. So I downloaded jprofilter and installed it on the local test server. When I run the test, I found that the number of classes exceeds the number of concurrent threads. It was strange that the original concurrent threads all use new
Class to implement some operations on the underlying data or objects. The Code is as follows:
Public void run (){
Try {
...
Openidaccount userdata = new openidservices (). getopenidaccount (this. openid );
...
} Catch (exception e ){
...
} Finally {
...
}
}
After the preceding Code ends, the newly created openidservices () object is not referenced by other objects, but it needs to wait for JVM to recycle, in this way, a large number of concurrent threads may not be released in time, resulting in a large amount of system memory.
After analyzing the source code, we can change this method to a static method,
Public class openidservices {
Public static openidaccount getopenidaccount (string openidurl) throws exception {
If (stringutils. isnotempty (openidurl )){
...
} Else {
Return NULL;
}
}
}
In this case, each thread does not need to call a new object. You only need to call the static method of this class.
Openidaccount userdata = openidservices. getopenidaccount (this. openid );
Therefore, I think that the number of new objects to be created is minimized in multi-threaded code. If you try to use single-sample objects, tool classes, or static methods as much as possible, this can reduce the memory usage.

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.