Find highlighting large lines during java debugging session

After analyzing the heap dump, I found that about 30 MB of lines were allocated (one line of 30 MB).
Is there a way to set a breakpoint while debugging the application so that I can find the code that highlights these huge lines? Since thousands of lines are highlighted during application startup, it is not possible to simply set a breakpoint for each line distribution. I believe a conditional breakpoint will be too slow. If possible, I would like to check the contents of these lines to understand the origin of these distributions.
For example, they may be the result of a web service call, which is usually called with a small amount of data, but in rare cases with a huge input (which I would like to find).

+5
source share
3 answers

I would try a conditional breakpoint, even if it meant that the application was running for a day or two. At least then you can do something else while it works. And any other solution will probably take a day or two to implement anyway.

Otherwise, you could use AspectJ to add a pointcut to String constructors and use this to register a message, throw an exception, etc. This is basically the same as a conditional breakpoint, but should execute faster.

+1
source

Although I explicitly requested a debugging session, I assume that it was mainly an attempt to solve the real problem: large lines are highlighted, and you do not know where this happens.

Distribution can be traced using jVisualVM . You can run jVisualVM and your program and connect jVisualVM to your program.

If this does not meet your needs, I can remove this answer. Otherwise, how can you do this:

On the "Profiler" tab, you can select the "Settings" checkbox in the upper right corner and select the "Memory settings" tab. There you can check the box "Trace stack selection records":

StringAlloc01

Then, after clicking the "Memory" button at the top, the profiler will collect information about the distribution of lines. After the application exits (or after a snapshot), you can look at the recorded stack traces to find a method that selects a large line - in this example method4 :

StringAlloc02

+3
source
  • Copy java.lang.String source code from JDK sources to your project

  • Create a method in String.java as shown below (count is an existing java.lang.String instance variable)

    private void checkSize(){ if(count>1000000) //1MB System.out.println(count); }

  • Modify java.lang.String to call this checkSize() method at the end of each constructor

  • Add a breakpoint in the line System.out.println (count);

  • Add new java.lang.String class to bootstrap class

  • This breakpoint will only break when creating a string of size> 1 MB

+2
source

Source: https://habr.com/ru/post/1200567/


All Articles