Finding running instances in a running JVM

I am wondering if it is possible to get a handle to run instances of this class. A particular problem that caused this was the application, which does not exit due to the large number of running threads.

Yes, I know that you can demonize theads, and they wonโ€™t hold the output of the application. But I wondered if this was possible. The closest I can do is findLoadedClass class loaders (protected!), Although you will have to run your own class loader to do this,

In this regard, pay attention to how profiling tools allow you to track object descriptors? by running their own custom classloaders? or is there some nice tricky way that i don't see?

+4
source share
5 answers

In fact, you can get a stack trace of all running threads that are dumped to stdout using kill -QUIT <pid> on * NIX OS, for example, when you launch the application on the Windows console and press Ctrl-Pause (as another note to the poster.)

However, it looks like you are asking for software ways to do this. So, assuming what you really want is a collection of all topics that the current call stacks include one or more methods from this class ...

The best thing I can find that does not include calls in the JVMTI is to check the stacks of all running threads. I have not tried this, but should work in Java 1.5 and later. Keep in mind that this is by definition NOT ALL WITHOUT thread safe (the list of running threads - and their current stack traces - will constantly change under you ... a lot of paranoid material would be needed to actually use this list.)

 public Set<Thread> findThreadsRunningClass(Class classToFindRunning) { Set<Thread> runningThreads = new HashSet<Thread>(); String className = classToFindRunning.getName(); Map<Thread,StackTraceElement[]> stackTraces = Thread.getAllStackTraces(); for(Thread t : stackTraces.keySey()) { StackTraceElement[] steArray = stackTraces.get(t); for(int i = 0;i<steArray.size();i++) { StackTraceElement ste = steArray[i]; if(ste.getClassName().equals(className)) { runningThreads.add(t); continue; } } } return runningThreads; } 

Let me know if this approach suits you!

+5
source

From this page ,

The Java profiler uses the native interface for the JVM (JVMPI for Java <= 1.4.2 or JVMTI for Java> = 1.5.0 ) to obtain profiling information from a running Java application.

This means that Sun provided native code that gives the profiler hooks in the JVM.

+3
source

If you only need a quick stack trace of your running application to find out which threads block the JVM output, you can send it a QUIT signal.

 $ kill -QUIT <pid of java process> 

On Windows, you need to run the application in the console window (using java.exe , not javaw.exe ), then you can press Ctrl-Pause to create a stack dump. In both cases, it is written to standard output.

+2
source

I am using Visual VM to monitor the JVM. Has many features, give them a try.

+1
source

I think you need Java dump . This should display all the threads with what they are doing at the moment. I had a similar problem that helped solve the thread dump (Quartz scheduler threads, which did not exit when Tomcat finished).

0
source

Source: https://habr.com/ru/post/897475/


All Articles