javaandroidmemory-leakshprof

Parsing hprof memory dump


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!


Solution

  • 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.