title: How to calculate memory usage of a Java program?
body: <p>If I use the <code>Runtime</code> class (<code>freeMemory()</code>, <code>totalMemory()</code>, and <code>gc()</code>), then it gives me memory above MB (i.e. 1,000,000 bytes).</p>
  
  <p>But if I run the same code on any online compiler, then they show memory used in KB (i.e. 1000 bytes). This is a huge difference.</p>
  
  <p>This means <code>Runtime</code> does not show the actual memory used by the program.</p>
  
  <p>I need to calculate actual memory used by the program.
  What is the way these online compilers use to calculate memory used by the program?</p>