Domanda

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?

È stato utile?

Soluzione

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.

Altri suggerimenti

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

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top