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.