Search code examples
javaprofilingheap-memoryheap-dump

Runtime heap analysis; when is a String initialized that matches a pattern


I have a Java program that at some point eventually creates a string that is matched by "schema.sql". I want to find where in the source code this happens. Searching "schema.sql" on the source code does not yield the location. Instead the program must create the string at runtime. I just don't know where. It happens in some library on my project. How can I find where my program creates this string?

Can I watch the heap at runtime somehow for a string that is matched by "schema.sql"? I looked at things like VisualVM but it looks like it only analyzes heap dumps, not the running state of the heap. There must be a way to do this; any ideas?


Solution

  • You can do that with JProfiler with the following steps:

    1) Start allocation recording at startup by configuring an appropriate allocation recording profile in the session startup dialog.

    enter image description here

    2) Take a heap snapshot after you think that the string has been created

    3) Double-click on the java.lang.String row in the classes view of the heap walker to create a new object set with all strings

    4) Switch to the "Outgoing references view"

    5) Search for your string by choosing "Apply filter->By restricting the selected value" and entering the string value

    enter image description here

    6) Switch to the allocations view and check out the stack trace

    enter image description here

    Disclaimer: My company develops JProfiler.