title: determine size of object: best way to use instrumentation in scala/sbt
body: <p>According to <a href="https://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object">this question</a>, the standard way to determine the memory size of an object in Java is by using java.lang.instrumentation. After to some research, it looks like there is no Scala specific way to achieve this, so the Java approach should also applies here.</p>
  
  <p>Unfortunately, for a Scala programmer without Java background it is not fully straightforward to adapt this technique in Scala. My questions are:</p>
  
  <p><strong>Question 1</strong></p>
  
  <p>What exactly is happening here? I guess the reason why we have to put a class like <code>ObjectSizeFetcher</code> in a separate JAR is to ensure that it is somehow loaded before the actual program where we want to use it. I assume it is not possible to use instrumentation without the <code>Premain-Class</code> entry and the parameter <code>-javaagent:TheJarContainingObjectFetcher.jar</code>?</p>
  
  <p><strong>Question 2</strong></p>
  
  <p>Is there a simple way to implement the complete work flow in SBT? Currently I only see a somewhat cumbersome solution: I first have to set up a secondary SBT project where I define <code>ObjectSizeFetcher</code> and package it into a JAR. So far I did not figure out how to automatically add the <code>Premain-Class</code> entry to the JAR during packaging, so I would have to solve that manually. Than I can add the resulting JAR to the local libraries of the project where I want to use <code>getObjectSize</code>. For this project I now have to enable <code>fork in run</code> and use <code>javaOptions in run += "-javaagent:TheJarContainingObjectFetcher.jar"</code>. Is there a more simple (and less intrusive) work flow to quickly use instrumentation within an existing SBT project? Maybe I can tell SBT directly about a <code>Premain-Class</code> to make this secondary JAR unnecessary?</p>
  
  <p><strong>Question 3</strong></p>
  
  <p>Would you recommend a completely different way to evaluate the memory usage of an object in Scala?</p>