title: How to get PID's Memory, CPU usage using java program?
body: <p>I am trying to get the PID's Memory and CPU usage by a Java program.</p>
  
  <p>I can be able to get PID of current program by the following code:</p>
  
  <pre><code>private static int getProcessId()
  {
  
  String name = ManagementFactory.getRuntimeMXBean().getName();
  
  StringBuffer pid = new StringBuffer();
      for (int i = 0, l = name.length(); i &lt; l; i++) {
      if (Character.isDigit(name.charAt(i))) {
          pid.append(name.charAt(i));
      } else if (pid.length() &gt; 0) {
          break;
      }
      }
      try {
      return Integer.parseInt(pid.toString());
      } catch (NumberFormatException e) {
      return 0;
      }
  
  }
  </code></pre>
  
  <p>By using the PID I want to calculate the Memory and CPU usage. Is there any methods to get those things?</p>
  
  <p>For example suppose if I get PID as 2256 from the method <code>getProcessId()</code>. I want to calculate memory and CPU for the PID: 2256.</p>