Can you really use this visualization tool for Java performance tuning?
Declare the cache object

Private?static?final?Map?map?=?new?HashMap ()
Public?static?void?main (String?args []) {
? try? {
? Thread.sleep (10000); / / time to open visualvm
Catch? (InterruptedException?e)? {
? e.printStackTrace ()
?}
? / / add objects to the cache in a loop
? for (int?i=0;?i < 1000000 bot iTunes +) {
? TestMemory?t?=?new?TestMemory ()
? map.put ("key" + iMagne t)
?}
? System.out.println ("first")
? / / provide time for dump to go out of heap
? try? {
? Thread.sleep (10000)
Catch? (InterruptedException?e)? {
? e.printStackTrace ()
?}
? for (int?i=0;?i < 1000000 bot iTunes +) {
? TestMemory?t?=?new?TestMemory ()
? map.put ("key" + iMagne t)
?}
? System.out.println ("second")
? try? {
? Thread.sleep (10000)
Catch? (InterruptedException?e)? {
? e.printStackTrace ()
?}
? for (int?i=0;?i < 3000000 / iTunes +) {
? TestMemory?t?=?new?TestMemory ()
? map.put ("key" + iMagne t)
?}
? System.out.println ("third")
? try? {
? Thread.sleep (10000)
Catch? (InterruptedException?e)? {
? e.printStackTrace ()
?}
? for (int?i=0;?i < 4000000 / iTunes +) {
? TestMemory?t?=?new?TestMemory ()
? map.put ("key" + iMagne t)
?}
? System.out.println ("forth")
? try? {
? Thread.sleep (Integer.MAX_VALUE)
Catch? (InterruptedException?e)? {
? e.printStackTrace ()
?}
? System.out.println ("qqqq")
?}
}
3. Configure jvm parameters as follows:
-Xms512m
-Xmx512m
-XX:-UseGCOverheadLimit
-XX:MaxPermSize=50m
4. Run the program and sign in for visualvm monitoring
Using JVisualVM to analyze memory leaks
1. Check the Visual GC tag, which contains the following. This is a screenshot of the output first.
This is a screenshot of the output forth:
Through the comparison of two pictures, it is found that:
The old generation has been on gc, and when the program continues to run, you can find that the old generation gc continues:
Increased to 7 times, but the memory of the older generation has not been reduced. Indicates that there are objects that cannot be recycled, which may be a memory leak.
How to analyze which object is leaked? Open the sampler tab: click as shown below:
Heap dump according to the program output, dump once when second is output, and dump once when forth is output.
Enter the heap tab that comes out of the last dump and click on the class:
Click on the upper right corner: "compare with another heap storage". As shown in the figure, select the dump content exported for the first time to compare:
The comparison results are as follows:
You can see that TestMemory object instances have been growing and increasing during the interval, indicating that there may be a memory leak in the methods referenced by the object.
How do I view object reference relationships?
Right-select the class TestMemory and select "Show in instance View", as shown below:
On the left is the total number of instances created, and on the upper right is the structure of the instance. The following is a reference description. You can see from the figure that it is referenced in class CyclicDependencies and referenced by HashMap.
In this way, the location of the leakage can be determined, and then it can be analyzed and solved according to the actual situation.
JVisualVM remote monitoring Tomcat
1. Modify the catalina.sh configuration file of the remote tomcat by adding:
JAVA_OPTS= "$JAVA_OPTS
?
-Djava.rmi.server.hostname=192.168.122.128
-Dcom.sun.management.jmxremote.port=18999
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false "
This time, the configuration does not go through the permission check. Just open the jmx port.
2. Open jvisualvm, right-click remote, and select add remote Host:
3. Enter the name of the host and write ip directly, as follows:
Right-click the newly created host, select add JMX connection, and enter the port configured in tomcat.
4. Double-click to open. Over!
Source: cnblogs.com/xifengxiaoma/p/9402497.html
-End-
I have some friends recently. Can I help you find some? Interview questions? Information, so I rummaged through the collection of 5T data, collected and sorted out, it can be said that programmer interview is necessary! All the materials have been sorted out on the net disk, welcome to download!



Comments
There are no comments for this story
Be the first to respond and start the conversation.