title: Best way to profile memory usage in a Java application?
body: <p>I realize that similar questions have been asked about this before here on SO, but let me describe exactly what I need to do:</p>
  
  <p>I have a set of tests which run a command line java application and I'd like to add memory profiling to them.  One option I see would be to add code (possibly using 3rd party tools/libraries) to my application that would provide a memory snapshot.  Another option would be to use a third party tool which manages/instruments my application and the JVM for me (and ideally does not require me to change my code).  I'm thinking of something like Valgrind but for Java.  Also open source if at all possible.</p>
  
  <p>What I'd really like to do is set up the memory tests so that my memory usage is being monitored at regular intervals, let's say every second, and dumped to a text file.  That way I'd be able to see if the memory usage oscillates/increases/decreases over time.  I'll also be able to calculate the max and min peaks.  </p>
  
  <p>Has anyone here done anything like this?  </p>
  
  <p>Thanks in advance.</p>