开发者

Why Java process hang?

There are many threads in my Java server. It hangs when one thread recursive invoke 开发者_JS百科a method infinitely. After the method is invoked 54 times, the process hang, and there is no any log like "StackOverFlow" or "OutOfMemory".

However, as I know, only the thread with the problem will crash, other threads can work normally.

environment:

Linux version 2.6.31-20-server (buildd@crested) (gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8) ) java version "1.6.0_18" Java(TM) SE Runtime Environment (build 1.6.0_18-b07) Java HotSpot(TM) 64-Bit Server VM (build 16.0-b13, mixed mode)


All threads require stack space. Every time you invoke a method recursively the stack expands - eventually you're gonna run out.

The number of iterations you manage depends on the exact code being run.


Generate a thread dump which will show you a stack trace for each Java thread in the VM. This will tell you what the threads are doing when the JVM hangs.

To generate a thread dump, on UNIX platforms, you can use the command kill -QUIT process_id, where process_id is the process id of your Java program.

On Windows, you can enter the key sequence <ctrl><break> in the window where the Java program was started. Sending this signal instructs a signal handler in the JVM, to recursively print out all the information on the threads and monitors inside the JVM.


Ask for a thread dump to see what your threads do. Set name for each thread you fork to identify them correctly. I suppose there could be also a dead lock on object monitors as well. I suggest use JProfiler to find the object the threads are blocked on.

On the other hand infinite recursion is not soo good. Avoid this, or define limit for it.


You've answered your question:

It hangs when one thread recursive invoke a method infinitely.

My suggestion is to not invoke a method infinitely, you're expanding your stack very quickly as you're recursively calling a method.

What are you trying to do with threads? Can you optimise your code to invoke the method without an infinite loop?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜