How to check how many resources a Java program uses?

How to check how many resources the java program uses?

In java, itโ€™s quite simple to create a large nice program that does almost everything you want, but a small side effect is also very easy to indirectly highlight many errors and processor cycles by mistake (ie just use the โ€œnewโ€ couple of times in the wrong location).

And if the program should run in an environment with a very limited amount of resources, say, something built-in or, possibly, a mobile phone (J2ME, Android, etc., etc.), it is important to know how hungry your program is.

Note. Since my main focus has been made in recent years, I could have missed something very simple here, so please do not take much;)

Thanks Johan

+4
source share
4 answers
maxMemory(); totalMemory(); freeMemory(); 
+5
source

You seem a little confused about what you really need. In your question, I feel some concern about โ€œhow many resources will Java depend on behind me to bite me later?โ€.

The answer to this is to rephrase Douglas Adams: โ€œDon't panic!โ€ :-).

Seriously, Java doesn't necessarily use much more resources than a native application, so you probably don't need to worry.

Quick summary:

  • Java programs will have fixed memory overhead due to the JVM, i.e. even a trivial program will consume several MB of memory. This, however, will not grow for more complex software, so this is usually not a problem in practice. In addition, several Java virtual machines running in parallel will share most of the RAM.
  • CPU: In general, a Java program will use the same processor time as an equivalent program. There is no fundamental reason Java is needed more. Garbage collection has some overhead, but it is usually not significant (and manual memory management also has overhead).
  • However, things like accidentally holding objects or unnecessary large objects can cause memory problems. But you solve this problem when it arises.

Short version: donโ€™t worry, just do the same thing as always: run it, make it work correctly, run it quickly (by profiling).

+4
source

if you are trying to capture these attributes outside of the program for performance tests; You can also use Java Profiler. There are good profilers there. Java 6 also provides the ability to perform such activities. eg J Profiler If you want more such tools; let me know.

+1
source

Since Java 1.6 has a set of tools to control the JVM. One of them is jmap . You can take a look at it.

I'm not quite sure how this relates to J2ME, but I would suggest that you can apply the same technique to the J2ME emulator on a computer (using the full JVM, not KVM).

0
source

All Articles