Location>code7788 >text

Use java code to get JVM information

Popularity:257 ℃/2025-04-13 10:27:37

Please indicate the source when reprinting:

Recently, there is a problem with positioning services in the environment. Due to the docker deployed by the service and the docker image used, after startup, there are no jdk-related tools [jstat, jmap, etc.] in the container; so we use the Java class in the project to obtain jvm-related information. The following is a test example:

import ;  
import ;  
import ;  
import ;  
import ;  
import ;  

import ;  

public class GCInfoDemo {  
    public static void main(String[] args) {  
        //Get memory management bean
        MemoryMXBean memoryMXBean = ();  
        MemoryUsage heapMemoryUsage = ();  
        MemoryUsage nonHeapMemoryUsage = ();  

        //Print memory information
        ("Heap Memory Usage:");  
        ("  Init: " + () / (1024 * 1024) + " MB");  
        ("  Used: " + () / (1024 * 1024) + " MB");  
        ("  Committed: " + () / (1024 * 1024) + " MB");  
        ("  Max: " + () / (1024 * 1024) + " MB");  
        
        ("\nNon-Heap Memory Usage:");  
        ("  Init: " + () / (1024 * 1024) + " MB");  
        ("  Used: " + () / (1024 * 1024) + " MB");  
        ("  Committed: " + () / (1024 * 1024) + " MB");  
        ("  Max: " + () / (1024 * 1024) + " MB");  

        //Get a list of garbage collector beans
        List<GarbageCollectorMXBean> gcBeans = ();  
        ("\nGarbage Collectors:");  
        for (GarbageCollectorMXBean gcBean : gcBeans) {  
            ("  Name: " + ());  
            ("  Number of collections: " + ());  
            ("  Total time spent in collections: " + () + " ms");  
        }  

        //Print memory pool information
        ("\nMemory Pools:");  
        for (MemoryPoolMXBean memoryPool : ()) {  
            ("  Name: " + ());  
            ("  Usage: " + ());  
        }  

        //Get thread management bean
        ThreadMXBean threadMXBean = ();  
        
        //Get all thread IDs
        long[] threadIds = ();  
        ("\nThreads Information:");  
        for (long threadId : threadIds) {  
            ("  Thread ID: " + threadId);  
            ("  Thread Name: " + (threadId).getThreadName());  
            ("  Thread State: " + (threadId).getThreadState());  
        }  

        //Check deadlock threads
        long[] deadlockedThreads = ();  
        if (deadlockedThreads != null) {  
            ("\nDeadlocked Threads:");  
            for (long deadlockedThreadId : deadlockedThreads) {  
                ("  Deadlocked Thread ID: " + deadlockedThreadId);  
                ("  Thread Name: " + (deadlockedThreadId).getThreadName());  
            }  
        } else {  
            ("\nNo deadlocked threads found.");  
        }  
    }  
}  
  1. Get GC information

    • pass()Get monitoring beans for all garbage collectors.

    • EachGarbageCollectorMXBeansupply:

      • getName(): GC algorithm name (such asG1 Young Generation

      • getCollectionCount(): Number of recycling times

      • getCollectionTime(): Cumulative time (milliseconds)

 2. Print the current thread information:

    • useThreadMXBeanGet all IDs of the current thread and passgetThreadInfoMethods obtain information of each thread, including thread name and status.

3. Check deadlock threads:

    • usefindDeadlockedThreadsMethods to check deadlocks in the JVM. If there are deadlock threads, the IDs and names of those threads are output. If not, the corresponding message is output.
Heap Memory Usage:
  Init: 508 MB
  Used: 10 MB
  Committed: 487 MB
  Max: 7205 MB

Non-Heap Memory Usage:
  Init: 2 MB
  Used: 4 MB
  Committed: 7 MB
  Max: 0 MB

Garbage Collectors:
  Name: PS Scavenge
  Number of collections: 0
  Total time spent in collections: 0 ms
  Name: PS MarkSweep
  Number of collections: 0
  Total time spent in collections: 0 ms

Memory Pools:
  Name: Code Cache
  Usage: init = 2555904(2496K) used = 1235968(1207K) committed = 2555904(2496K) max = 251658240(245760K)
  Name: Metaspace
  Usage: init = 0(0K) used = 3597432(3513K) committed = 4980736(4864K) max = -1(-1K)
  Name: Compressed Class Space
  Usage: init = 0(0K) used = 392584(383K) committed = 524288(512K) max = 1073741824(1048576K)
  Name: PS Eden Space
  Usage: init = 133169152(130048K) used = 10914704(10658K) committed = 133169152(130048K) max = 2789212160(2723840K)
  Name: PS Survivor Space
  Usage: init = 22020096(21504K) used = 0(0K) committed = 22020096(21504K) max = 22020096(21504K)
  Name: PS Old Gen
  Usage: init = 355467264(347136K) used = 0(0K) committed = 355467264(347136K) max = 5666504704(5533696K)

Threads Information:
  Thread ID: 6
  Thread Name: Monitor Ctrl-Break
  Thread State: RUNNABLE
  Thread ID: 5
  Thread Name: Attach Listener
  Thread State: RUNNABLE
  Thread ID: 4
  Thread Name: Signal Dispatcher
  Thread State: RUNNABLE
  Thread ID: 3
  Thread Name: Finalizer
  Thread State: WAITING
  Thread ID: 2
  Thread Name: Reference Handler
  Thread State: WAITING
  Thread ID: 1
  Thread Name: main
  Thread State: RUNNABLE

No deadlocked threads found.

Process finished with exit code 0