title: How to monitor Java memory usage?
body: <p>We have a j2ee application running on Jboss and we want to monitor its memory usage. Currently we use the following code </p>
  
  <pre><code>    System.gc();
      Runtime rt = Runtime.getRuntime();
      long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024;
      logger.information(this, "memory usage" + usedMB);
  </code></pre>
  
  <p>This code works fine. That means it shows memory curve which corresponds to reality. When we create a big xml file from a DB a curve goes up, after the extraction is finished it goes down.  </p>
  
  <p><img src="https://i6.photobucket.com/albums/y231/oleg_pavliv/hc.jpg" alt="alt text"></p>
  
  <p>A consultant told us that calling gc() explicitly is wrong, "let jvm decide when to run gc". Basically his arguments were the same as <a href="https://stackoverflow.com/questions/66540/system-gc-in-java">disscussed here</a>. 
  But I still don't understand:</p>
  
  <ul>
  <li>how can I have my memory usage curve?</li>
  <li>what is wrong with the explicit gc()? I don't care about small performance issues which can happen with explicit gc() and which I would estimate in 1-3%. What I need is memory and thread monitor which helps me in analysis of our system on customer site.</li>
  </ul>