Profiling number of garbage-collected object insta

2019-02-18 14:33发布

问题:

I am looking for a tool that can provide VisuaVM-like profiling about live objects, but in non-GUI mode.

The Visual VM functionality I am referring, is accessed by going to the "Profiler" tab and clicking "Memory".

By setting a profile preset of "Profile object allocations and GC" for ever 1 objects (all objects). This gives me exactly what I need in an auto-refreshing view, which I can filter for the class that interests me.

However, I want to be able to export the table of "live objects" to a text file, for every snapshot that is taken (Visual VM refreshes every one seconds). Obviously, pointing and clicking cannot possibly be a solution...

Anyone know of such a "command-line" profiler?

I have been looking at jmap which provides heap dumps, but it is too costly (the dump takes too long, I am just interested in the number of objects).

There is a commercial tool called YourKit but I don't know whether it can do what I need (and also seems rather expensive for the type of "one-off" usage I need it for).

If I could use VisualVM as-is, but have it append the output to a file (instead of refreshing its GUI) it'd be perfect...

回答1:

I think Class Histograms are what you look for. You could collect the histograms in regular intervals and this will show you number of objects of each class and occupied space. You can then parse the text output yourself in order to:

  • compare two histograms to see instance allocation/deallocation
  • filter by a class name
  • monitor space occupation of class instances over time

Collect class histogram with jmap -histo $pid.