Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakpoint on array construction

Is there anyway to set a breakpoint in eclipse or another debugger such that the execution stops on the construction of an array? I am particularly interested in the construction of a primitive array (int[]) but this question should be equally applicable to any array.

I need to find the culprit(s) creating large amount of garbage consisting of int[], char[] and byte[] among others, so if I can put a breakpoint with some conditions, I will be able to narrow the code down.

I tried using yourkit memory profiling, but it only shows allocations for only a tiny portion of these objects and the rest are shown as <objects without allocation information>, I am not sure why. When I go into the Objects unreachable from GC roots view, I see allocation information for only about 7% of the garbage. With allocations for such a small percentage of objects, I am not even sure if I am missing some locations. Is there a way to get YK to preserve all allocations?

like image 606
haridsv Avatar asked Sep 14 '12 10:09

haridsv


2 Answers

When you construct an array, the VM simply reserves that much memory space for to be filled in references. This is a single step native operation and a break-point in the memory allocation process will not be possible . For example take the following code

public class Test{

 public void createArray(){

        int[] iarray = new int[10];

    }

}

Now if you disassemble this, you get following set of instructions

Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public void createArray();
  Code:
   0:   bipush  10
   2:   newarray int
   4:   astore_1
   5:   return

}

Notice the definition of method createArray(), newarray int is a single instruction to allocate the memory to specified number of elements.

like image 72
Santosh Avatar answered Sep 23 '22 13:09

Santosh


I work for YourKit, so I'll try to clarify "objects without allocation information" message.

By default YourKit profiler records allocation of each 10-th object. This is a configurable option, so changing "Record each" value to 1 should help. Here is the details from profiler documentation http://www.yourkit.com/docs/11/help/allocations.jsp

like image 26
Vladimir Kondratyev Avatar answered Sep 24 '22 13:09

Vladimir Kondratyev