jconsole, jstat, jmap, jstack, gc

阅读更多

 

 

通过工具jconsole查看:

直接执行命令:jconsole即可,通过jconsole可以手动执行gc,检测死锁,查看内存信息

 

一:分析死锁:

 

参考资料(http://www.cnblogs.com/ilahsa/archive/2013/06/03/3115410.html)

 

模拟死锁代码:DemoServiceImpl.java

 

package com.tch.test.dubbo_service_provider.serviceImpl;

import com.tch.test.dubbo_service_model.service.DemoService;

public class DemoServiceImpl implements DemoService {

	private Object resource1 = new Object();
	private Object resource2 = new Object();
	
	public String sayHello(String name) {
		deadLock();
		return "Hello " + name;

	}
	
	public void deadLock(){
		lockResource1();
		lockResource2();
	}

	private void lockResource1() {
		new Thread(new Runnable() {
			public void run() {
				synchronized(resource1){
					System.out.println(Thread.currentThread().getId() + ": i have get the lock of resource1");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getId() + ": i begin to get the lock of resource2");
					synchronized(resource2){
						System.out.println(Thread.currentThread().getId() + ": i have get the lock of resource2");
					}
				}				
			}
		}).start();
	}
	
	private void lockResource2() {
		new Thread(new Runnable() {
			public void run() {
				synchronized(resource2){
					System.out.println(Thread.currentThread().getId() + ": i have get the lock of resource2");
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(Thread.currentThread().getId() + ": i begin to get the lock of resource1");
					synchronized(resource1){
						System.out.println(Thread.currentThread().getId() + ": i have get the lock of resource1");
					}
				}				
			}
		}).start();
	}

}

 

 

执行命令:

jps

显示出Java进程,其中5900 Jps是我们执行jps的进程,6464 Provider则是我们应用程序的进程,6464也就是我们需要的进程号。

6464 Provider

2712

5900 Jps

 

然后执行:jstack -l 6464

 

就会显示出来很多信息,其中信息的最后是:

Found one Java-level deadlock:

=============================

"Thread-2":

  waiting to lock monitor 0x0000000057437088 (object 0x00000000d5db1d70, a java.lang.Object),

  which is held by "Thread-1"

"Thread-1":

  waiting to lock monitor 0x0000000057436b08 (object 0x00000000d5db1d80, a java.lang.Object),

  which is held by "Thread-2"

 

Java stack information for the threads listed above:

===================================================

"Thread-2":

        at com.tch.test.dubbo_service_provider.serviceImpl.DemoServiceImpl$2.run(DemoServiceImpl.java:52)

        - waiting to lock <0x00000000d5db1d70> (a java.lang.Object)

        - locked <0x00000000d5db1d80> (a java.lang.Object)

        at java.lang.Thread.run(Thread.java:745)

"Thread-1":

        at com.tch.test.dubbo_service_provider.serviceImpl.DemoServiceImpl$1.run(DemoServiceImpl.java:33)

        - waiting to lock <0x00000000d5db1d80> (a java.lang.Object)

        - locked <0x00000000d5db1d70> (a java.lang.Object)

        at java.lang.Thread.run(Thread.java:745)

 

Found 1 deadlock.

 

明确显示了死锁的位置。。。

 

 

二:查看应用程序的内存信息:

 

参考资料(http://liudaoru.iteye.com/blog/540772)

 

执行:jmap -dump:format=b,file=dumpFile.bin 6464

会在当前目录生成文件:dumpFile.bin,就可以查看应用程序(jps显示的进程号:6464)内存信息,包含有哪些对象等等

但是该文件是二进制的文件,需要通过:

jhat dumpFile.bin,然后在浏览器地址:http://localhost:7000/即可查看应用程序的内存信息

 

 

三:查看应用程序GC信息:

 

参考资料(http://www.cnblogs.com/alipayhutu/archive/2012/08/20/2647353.html)

 

执行命令:

jstat -gc 6464 3000

可以每隔三秒显示一次应用程序的GC信息

 

你可能感兴趣的:(jconsole,jstat,jmap,jstack,gc)