T
T
Tarasov Konstantin2014-10-08 13:46:15
Java
Tarasov Konstantin, 2014-10-08 13:46:15

How to analyze the memory usage of a java program?

Stuck on such a task - to analyze the memory usage of a java program. There are no methods that return the amount of memory consumed by an object in java. It is also difficult to analyze from the outside - after all, there we also see the memory occupied by the JVM. What can be used to analyze how much a particular program consumes memory in its pure form, without taking into account the JVM and so on? Surely something has already been invented for this purpose. (Do not offer self-calculation)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vlastachu, 2014-10-08
@vlastachu

You can use the garbage collector log.
On different JVM flags and output format are different.
Here's what you'll most likely see.

[GC 245118K->214677K(1571712K) eden 30464K->0K survivor 247K->270K tenured 214406K->214406K, 0.0212236 secs]
[GC 245141K->214708K(1571712K) eden 30463K->0K survivor 270K->301K tenured 214406K->214406K, 0.0229019 secs]
[GC 245172K->214692K(1571712K) eden 30463K->0K survivor 301K->285K tenured 214406K->214406K, 0.0241589 secs]
[GC 245156K->214747K(1571712K) eden 30463K->0K survivor 285K->340K tenured 214406K->214406K, 0.0245928 secs]

This uses a garbage collector with three heaps (three generations) eden, survivor and tenured.
Each of them has the characteristics of how much before assembly and how much is left after (eden 30463K-> 0K).
At the beginning there is a general characteristic that you are probably interested in. It also indicates the amount of memory occupied by objects before and after assembly, as well as the total amount of the heap in brackets. In addition to these generations, there is also a permanent generation, which stores information about classes and static fields. I may be wrong on many points, but I hope I helped with the direction of the search.
There is also GCViewer - it visualizes the state of the collector and collects general statistics.
If you find any documentation on the collector log format, please share the link. I would be grateful.

P
Power, 2014-10-09
@Power

Information about memory can also be viewed using jstat . This is such a console utility that allows you to connect to any running java process (only run it as the same user as which java is running).
The same information can show all sorts of graphic visualvm and jconsole.
But what you will see there is consumption, which includes objects of the JVM itself and uncollected garbage.
And to see the details and "clean" consumption, you need a profiler (visualvm and jconsole can do this too).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question