0

I am working on an Android application and I have a memory dump (i.e. a .hprof file) that I captured from my application.

I want to investigate a certain container of the application.

Just like an ArrayList, the container has a growth policy that increases the capacity of the container when the size is reached to guarantee constant amortized time cost for adding elements into it.

My hypothesis is that every instance of the container possesses multiple unused allocated memory spaces.

I want to automate the process of investigating this case. Is there a way to write a script that can parse this .hprof file and return the ratio of these unused allocated spaces to total_entries?

Thank you!

T code
  • 447
  • 2
  • 7
  • 12

1 Answers1

0

You can take a look at jvm-hprof-rs (https://bitbucket.org/marshallpierce/jvm-hprof-rs/src/master/).

If you are comfortable writing Rust code, the library gives you a great deal of control over how to process the data. (Documentation)

For simpler cases, the analyze_hprof example can give you a CSV format containing the "Instance count", "Instance size (bytes)", "Total shallow instance size (bytes)", "Class name", and "Class obj id". You can then post-process to get the info you need.

cargo run --release --example analyze_hprof -- \
    -f path/to/your.hprof \
    instance-counts

If I am understanding your problem correctly, you can take the output CSV, grep for the class name you want to analyze, and compute Instance size (bytes) / Instance count.

Maurice Lam
  • 1,577
  • 9
  • 14
  • The readme for that project also mentions https://github.com/AdoptOpenJDK/jheappo, which looks like it has more flexibility for querying over command line. – Maurice Lam Mar 07 '23 at 00:19
  • Does this allow you to estime the amount of unused allocated memory in your container? When you connect a debugger to an ArrayList for example, even if the ArrayList doubled its capacity for further add(), if you check the size() method, it's always exactly the number of initialised elements in it, so you won't be able to see how much extra spaces the container has reserved in terms of memory. My ultimate goal is that, to try to see how much the container takes extra space in memory just to optimize for future add() calls by reserving slots that are still not used. Thanks for your help. – T code Mar 07 '23 at 18:00
  • For that the tool will need knowledge on the specific implementation of the container (e.g. the field names of the size / capacity). With that knowledge, you can try looking it up from the hprof via https://docs.rs/jvm-hprof/0.1.0/jvm_hprof/heap_dump/struct.Instance.html#method.fields – Maurice Lam Mar 07 '23 at 19:55