How to check how much resources a java program uses?

embedded, java, optimization

Solution

maxMemory();
totalMemory();
freeMemory();

Problem

How do I check how much resources a java program uses? In java it is quite easy to create a big nice program that does practically everything you want it to, but a little side effect is that is also very easy to indirectly allocate to much memory and cpu cycles by mistake (i.e. let's just use "new" a couple of times in the wrong place). And if the program are supposed to run in a environment with very limited amount of resources, let's say something embedded or maybe a mobile phone (J2ME, Android etc etc), it is crucial to know how resource hungry your program is. Note: Since my main focus has been c the last years I could be missing something very simple here, so please don't assume to much ;) Thanks Johan

Original source