4

I have encountered some strange text formatting in our application and I need to find the cause. The problem is I have no idea where to start looking in our Legacy code.

Is it possible to make the IntelliJ IDEA debugger search for a value throughout the entire instance of an application in the JVM? As opposed to the usual way of setting a breakpoint with a condition.

user2818782
  • 736
  • 8
  • 18
  • I don't understand what you mean by "search for a value". Do you mean in the heap or stack? Initially, since you were talking about formatting, I thought you were talking about code. – ProgrammersBlock Sep 07 '17 at 12:59
  • You can set breakpoint (CTRL+SHIFT+F8) for "Any exception", it will set breakpoint where exception is thrown, also you can add condition to iterate over scope ("this") variables and check type (I assume String in this case) and value – whoopdedoo Sep 07 '17 at 14:37

1 Answers1

5

Judging from the comment on this question, there is no such feature in IntelliJ.

You could however try performing a heap dump and then searching it using OQL. It can be done in the VisualVM tool which is bundled with the JDK:

  1. Run the jvisualvm command (assuming you have the JDK's bin folder in your PATH).
  2. Start your application, find it in the left column under Local, and select Heap Dump from the right-click menu.
  3. Head to the OQL Console view. In Query Editor, type your query, for example:

    select s from java.lang.String s where s.toString().contains("hello")
    

    and execute it.

  4. If your object is on the heap, you should see it. Click it and check the referencing objects in References.

This should get you closer to the classes/objects your object is used by.

There are some tricky parts:

  • you have to perform the heap dump before the object in question is garbage-collected - that is, as soon as possible after you are sure the object was created (other ways to acquire a heap dump are described here),
  • the object can be unreachable. In such cases, try to look through the object graph it is part of and find other objects which are still referenced,
  • the object may not live in the heap at all (escape analysis).

Another tool for heap dump analysis is Eclipse Memory Analyzer (MAT). (For your use case, enable the Keep unreachable objects option before analyzing the heap dump.)

You mentioned that you need to search for the value at runtime. However, if you suspect what you are looking for is a string literal, try just searching the JARs of your application as described in this answer.

Piotr Góralczyk
  • 557
  • 5
  • 14