I have a project where I need to be able to measure the efficiency in CPU and in memory space of functions I receive as text and compile at run-time using the Java Compiler API.

I am using ThreadMXBean to measure running time. Is there a better/more exact way? 

```
ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();

```

What I need is advice on how to measure the memory used.
Ideally I would like to know about every assignment, and its size. But I would be alright with a precise count of the bytes used by the code.

Every piece of code need to be compiled, tested, run and evaluated **automatically**. So **using a Profiler and look yourself is not an option**.
Any suggestion, possible method, resource or tools you might point me to is welcome.

I need to make that point clear. This is not a one off process. **It is the main service of a web app that receives, compiles, analyzes and scores implemented functions on the fly**. At pick I would expect a few 100 concurrent jobs going on. The process of monitoring once or manually with external tools is not my goal.

**I repeat**: I want to do this `as a service`. It is not for testing my apps or profiling them.

I do not bite (or down-vote since we are on SO), so any serious suggestion will earn up-votes.

**status update**: This project is on hold since I do not have a solid reliable way of doing the memory use measurements. I am currently looking at other languages/frameworks out of the JVM world that could this too. **Even so I am still actively seeking advice and studying for this project.**