Search code examples
javaeclipsebreakpointsyourkit

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?


Solution

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