How to track cpu memory usage and disk utilization in Java

In this article, we will discuss some initial commands, tools, and techniques to help track CPU memory and disk utilization in Java.





Java tools oversee the constructs and processes of Java bytecode. Java profilers keep track of all system commands and processor usage. This allows you to call funds at any time.





The standard profiler provides a wealth of knowledge, the value of which depends on the purpose of the debug. Some tools are better for aggregating logs for all applications and servers, such as Stackify Retrace , setting up and tracking an automatic request log. Tools: Stackify's Application Performance Management, Retrace - offer Java users a better understanding of the application design through embedded logging and code profiling. Together with the implemented centralized and structured logging, it is possible to access all application logs from a single point for all applications and servers.





Here we have listed ways to demonstrate memory and CPU tracking in Java to optimize your code.  





5 Ways to Monitor CPU Memory and Disk Usage in Java!





  1. Basic Linux Commands for Tracking CPU Memory Usage and Disk Usage





    a) free –m





    The command transfers the available and occupied memory to your virtual machine.





    b) top





    This command is to view CPU and memory usage.





    When you enter 1 on your keyboard, then top will fire and display all possible CPUs and usage of each.





    There are 4 CPUs in total - CPU0, CPU1, CPU2, CPU3 - and all their usage statistics.





    c) meminfo and cpuinfo





    Use the following commands for detailed information regarding memory and virtual machine processors.





    $ cat / proc / cpuinfo





    $ cat / proc / meminfo





    d) CPU memory and disk usage of a particular process.





    $ ps -p [pid] -o% cpu,% mem, cmd





    CPU Java. .





    e) Java .





    $ps uH –p [pid]





    .





    CPU .





    , , . CPU.





    df –k  –>





    lsof -i :<port> –> ,





    lsof –i: tcp –> TCP





    netstat -plunt  –>









  2. ManagementFactory . MemoryMXBean. MemoryMXBean:





    MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
    
    System.out.println(String.format(“Initial memory: %.2f GB”, 
    
      (double)memoryMXBean.getHeapMemoryUsage().getInit() /1073741824));
    
    System.out.println(String.format(“Used heap memory: %.2f GB”, 
    
      (double)memoryMXBean.getHeapMemoryUsage().getUsed() /1073741824));
    
    System.out.println(String.format(“Max heap memory: %.2f GB”, 
    
      (double)memoryMXBean.getHeapMemoryUsage().getMax() /1073741824));
    
    System.out.println(String.format(“Committed memory: %.2f GB”, 
    
      (double)memoryMXBean.getHeapMemoryUsage().getCommitted() /1073741824));
          
          



    , , . :





    : Java





    : , Java





    : JVM. OutOfMemoryException ,





    : JVM





  3. (-Agentpath)





    java Windows, Linux masOS. -Agentpath - .





    :





    java –agentpath:/usr/profiler/lib/[agent].so  -jar application.jar





    , .





    JProfiler Yourkit .





  4. Java (-Javaagent)





    Java -javaagent , . Java :





    a) premain()  





    b) Pre-Main MANIFEST.MF JAR,





    c) JVM –javaagen





    java -javaagent:agent.jar -jar application.jar





    premain() main() , -javaagent -jar.





    , main() , agentmain() . Agent-Main MANIFEST.MF .





  5. (-Agentlib)





    -Agentlib - , JVM . :





    1) HPROF 





    2) JVM





    1) HPROF: 





    HPROF CPU, Java. . –agentlib:hprof –Xrunhprof HPROF.





    JVM , , java.hprof.txt. 





    java agentlib:hprof=heap=sites Hello.java





    HPROF , .





    javac -J-agentlib:hprof=heap=sites Hello.java





    2)





    –agentlib:jdwp . JVM





     java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:9001, server=y,suspend=y –jar application.jar 





    , IDE.





    Use commands limited / remote for debugging method in Java. An application that monitors records on the specified port will not move on until step (ii) completes.





    Screenshot of the entry from the application: Monitoring for transport dt_socket at address: 9001





    Initialize the project in the IDE in debug mode and attach encryption to the host and port.





    Debug your code on a single server with an IDE.








All Articles