5

无论如何要在 Eclipse 或其他调试器中设置断点,以便在构建数组时停止执行?我对原始数组 (int[]) 的构造特别感兴趣,但这个问题应该同样适用于任何数组。

我需要找到造成大量垃圾的罪魁祸首,其中包括 int[]、char[] 和 byte[] 等,所以如果我可以在某些条件下设置断点,我将能够缩小代码范围.

我尝试使用 yourkit 内存分析,但它只显示这些对象中一小部分的分配,其余显示为<objects without allocation information>,我不知道为什么。当我进入Objects unreachable from GC roots视图时,我只能看到大约 7% 的垃圾的分配信息。由于分配了这么少比例的对象,我什至不确定我是否遗漏了一些位置。有没有办法让 YK 保留所有分配?

4

3 回答 3

3

当你构造一个数组时,VM 只是保留那么多内存空间填充引用。这是一个单步本机操作,不可能在内存分配过程中出现断点。例如采取以下代码

public class Test{

 public void createArray(){

        int[] iarray = new int[10];

    }

}

现在,如果你反汇编它,你会得到以下说明

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

}

注意 method 的定义createArray()newarray int是一条将内存分配给指定数量元素的指令。

于 2012-09-14T10:43:47.697 回答
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

于 2012-09-26T11:31:44.670 回答
0

World of Primitive arrays is really mysterious and I don't think anyone is allowed in there ;). 在调试中浏览 Java 代码的唯一方法是,F5F5 i.e.Step in仅适用于未声明的函数,所以我想这是不可能的。

为了确保您可以打印这些数组,使用Arrays.toString()它将打印数组中的所有元素

于 2012-09-14T10:34:03.867 回答