title: Java: Inspecting non-heap memory usage
body: <p>I'm inspecting running times, heap memory usage and non-heap memory usage of different algorithms to a specific sorting problem.</p>
  
  <p>I've successfully been able to measure running time and heap memory usage, but I have problems measuring non-heap memory usage.</p>
  
  <p>What I do when I try to measure it is I start a new thread which constantly checks if the non-heap memory has changed while I'm running my sorting algorithm. This thread object records the highest value of the memory usage during the running of that algorithm.</p>
  
  <p>This works for ordinary heap memory (or at least, I think it works as it provides logical results), but when I try to do the same for the non-heap memory, I seam to get a lot of 0 memory usage results.</p>
  
  <p>I use ManagementFactory.memoryMXBean.getNonHeapMemoryUsage().getUsed() to read the non heap memory and I figured that this memory is not freed when running the garbage collector (I run System.gc() 16 times to hopefully make the garbage collector collect the garbage before I record the initial memory (I know this is only a suggestion, however this shouldn't matter as it doesn't do anything for the non-heap memory anyways (i think))). I'm also guessing that this memory is not freed when things are popped from the stack because my results are largest in the first algorithms tried and therefore I suspect I get only the difference of the non-heap memory usage between each run and 0 if the latter run uses less memory than the former.</p>
  
  <p>This is my memory inspector class:</p>
  
  <pre><code>class MemoryInspectorThread extends Thread
  {
  private int samplingInterval;
  
  private long initialMemoryHeap;
  private long initialMemoryNonHeap;
  private long maxMemoryHeap;
  private long maxMemoryNonHeap;
  
  private boolean kill=false;
  
  void kill()
  {
      kill=true;
  }
  
  long getMaxNewMemoryHeap()
  {
      return (maxMemoryHeap-initialMemoryHeap)/1024;
  }
  
  long getMaxNewMemoryNonHeap()
  {
      return (maxMemoryNonHeap-initialMemoryNonHeap)/1024;
  }
  
  MemoryInspectorThread(int samplingInterval)
  {
      this.samplingInterval=samplingInterval;
  
      try
      {
          sleep(1000);
      } catch (InterruptedException e)
      {
          e.printStackTrace();
      }
  
      MemoryMXBean memoryMXBean=ManagementFactory.getMemoryMXBean();
  
      System.gc(); System.gc(); System.gc(); System.gc();
      System.gc(); System.gc(); System.gc(); System.gc();
      System.gc(); System.gc(); System.gc(); System.gc();
      System.gc(); System.gc(); System.gc(); System.gc();
  
      initialMemoryHeap=memoryMXBean.getHeapMemoryUsage().getUsed();
      initialMemoryNonHeap=memoryMXBean.getNonHeapMemoryUsage().getUsed();
  }
  @Override
  public void run()
  {
      MemoryMXBean memoryMXBean=ManagementFactory.getMemoryMXBean();
  
      while (!kill)
      {   
          long freeMemoryHeap=memoryMXBean.getHeapMemoryUsage().getUsed();
          long freeMemoryNonHeap=memoryMXBean.getNonHeapMemoryUsage().getUsed();
  
          if (freeMemoryHeap &gt; maxMemoryHeap)
          {
              maxMemoryHeap=freeMemoryHeap;
          }
  
          if (freeMemoryNonHeap &gt; maxMemoryNonHeap)
          {
              maxMemoryNonHeap=freeMemoryNonHeap;
          }
  
          try
          {
              sleep(samplingInterval);
          } catch (InterruptedException e)
          {
              e.printStackTrace();
          }
      }
  }
  }
  </code></pre>
  
  <p>How can I get the actual non-heap memory? Are my assumptions about how the thing works correct?</p>