One of the performance analysis is: When oom occurs, browse object assignments and references to discover and fix memory leaks;
Sample program Pointfactory
Public classPointfactory {protectedArrayList points =NewArrayList (); protected StaticPointfactory instance =Newpointfactory (); PublicPoint Createpoint (intXinty) { point point=NewPoint (x, y); This. Points.Add (point); returnPoint ; } Public voidRemovepoint (Point point) { This. Points.remove (point); } Public voidprinttestpoints () { for(inti = 0; I < 5; i++) { point point=Createpoint (i, I); System.out.println ("point =" +Point ); } } Public Staticpointfactory getinstance () {returninstance; } Public Static voidMain (string[] args)throwsException {JFrame frame=NewJFrame ("Points Test"); Frame.setdefaultcloseoperation (Windowconstants.exit_on_close); JButton Button=NewJButton ("Print Test Points"); Button.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {pointfactory.getinstance (). printtestpoints (); } }); Frame.getcontentpane (). Add (button); Frame.setsize (200, 100); Frame.setvisible (true); }}
View Code
Run pointfactory;
Run Jprofile, select Local JVM;
Select the program;
There was a terrible warning:
In some cases, a bug in attach mode can cause the JVM to crash;
The Sun JVM Attach API is a non-standard set of APIs in the Sun JVM that can be connected to the JVM;
Bug Details:
- Sun Java (HotSpot) client JVM can crash in attach mode due to a JVM bug
Due to a JVM bug 6776659 HotSpot client JVM can crash in attach mode.
There is no crash for the server JVM:JVM Option-server solves the problem.
Source Document
Let's just run it in server mode.
This time there was no hint;
Choose
Select Instrumentation Dashboard, we want to see all the analysis;
So many classes, I suspect that the point class has a memory leak, then just look at it, set the view Filters;
There is no point button, so there is no class created;
But the view inside is still empty ah;
I had to use java.awt.Point, but fortunately I was witty;
Why does the 5 class come out total is 333?
Once more, turn 418;
Click Garbage collection Run GC:
10 remaining instances, normal, 10 instances, why there are 418 so many;
And so on, the dot button, Run GC, and total 5 increments per point, proves that the point class is not recoverable;
Already integrated into Eclipse, can profile as Java application, very convenient;
So why didn't the point class release? Clearly it is useless;
The actual project, find source code and then find the reference will be exhausted people, we still through the runtime stack snapshot to see it;
Switch to the heap view;
In the cumulative reference list, you can see who referred to point;
Here is points this ArrayList quoted, but no removal resulted;
Call Removepoint when you are done with it.
Using Jprofile to discover and fix memory leaks