Breakpoint on array construction
Asked Answered
P

3

6

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?

Preparative answered 14/9, 2012 at 10:23 Comment(2)
I don't believe you can. Is your application running in a container, is it a rich client, or just standalone app? Can you tell us more about your app... Remember primitive types (int, char, byte, etc) are the basic building blocks of Java so even if you could break point creation of arrays you'll be stepping over alot code!Desilva
It is a standalone app, not sure what else I can tell about the app, but I accepted the answer from @Santosh.Preparative
T
3

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.

Thee answered 14/9, 2012 at 10:43 Comment(0)
L
2

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

Lyublin answered 26/9, 2012 at 11:31 Comment(1)
That is exactly what I did, along with setting the size limit to 0 as well. I don't think the "objects without allocation information" problem is related to this. In any case, this setting should impact how much data is collected, not how many of those have the allocation information collected.Preparative
V
0

World of Primitive arrays is really mysterious and I don't think anyone is allowed in there ;). The Only way to browse through Java code in debug is F5 but F5 i.e.Step in Only works for functions not declaration so I guess it is not possible.

To make sure you can print these arrays using Arrays.toString() which will print all the elements in array

Vickivickie answered 14/9, 2012 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.