title: Programmatically calculate memory occupied by a Java Object including objects it references
body: <p>I need to programmatically find out exactly how much memory a given Java Object is occupying including the memory occupied by the objects that it is referencing.</p>
  
  <p>I can generate a memory heap dump and analyse the results using a tool.  However it takes a considerable amount of time to generate a heap dump and for such a tool to read the dump to generate reports.  Given the fact that I will probably need to do this several times, my work would be a lot more agile if I could throw in some code in my project that would give me this value "runtime".</p>
  
  <p>How could I best achieve this?</p>
  
  <p>ps: Specifically I have a collection of objects of type javax.xml.transform.Templates</p>