内存溢出,CPU居高不下,死锁,我们怎么去分析呢,如果使用C或者C++,有gdb,java有吗,当然,那就是jstack 下面我写个死循环的例子,如下 JavaApplication1.java
public class JavaApplication1 { /** * @param args the command line arguments */ public static void main(String[] args) throws Exception { // TODO code application logic here System.out.println("Programe begin"); User u = new User(); u.GetUser(); System.out.println("Programe end"); }}
User.java
public class User { public void GetUser() throws InterruptedException{ boolean isGetUser =false; while(!isGetUser){ Thread.sleep(2000); System.out.println("loop get user info"); } }
很容易看到User中的GetUser是个死循环,当然我们开发实践中产生死循环的场景要复杂的多。那么我想定位到while代码块的位置怎么办呢?要是我们能看到jvm正在执行的堆栈是不是就迎刃而解了呢
jps #打印正在运行的java程序,第一列为pid,第二列程序名称 32064 Main 33681 Jps 33614 JavaApplication1jstack 33614 #打印当前进程的堆栈,很容易我们就发现了如下信息"main" #1 prio=5 os_prio=31 tid=0x00007fe0a7802800 nid=0x1c03 waiting on condition [0x000070000fc73000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at javaapplication1.User.GetUser(User.java:16) at javaapplication1.JavaApplication1.main(JavaApplication1.java:22)