Java 如何找到堆中的对象数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2262620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the number of objects in the heap
提问by java_geek
How can I find the number of live objects on the heap in Java program?
如何在 Java 程序中找到堆上活动对象的数量?
采纳答案by user85421
There is a hack you can try:
有一个黑客可以尝试:
- create your own java.lang.Object (copy the original source)
- count the created objects in the constructor (not called for arrays)
- add the path to your classfile to the boot classpath
- 创建您自己的 java.lang.Object(复制原始源代码)
- 计算构造函数中创建的对象(不为数组调用)
- 将类文件的路径添加到引导类路径
see this (old) articlefor a sample.
有关示例,请参阅此(旧)文章。
Probably there are better ways to do it using JPDAor JMX, but I've not found how...
回答by Scharrels
As far as I know, you cannot. You can, however, get the amount of memory used for the program:
据我所知,你不能。但是,您可以获得用于程序的内存量:
Runtime rt = Runtime.getRuntime();
System.out.println("Used: " + (rt.totalMemory() - rt.freeMemory());
System.out.println("Free: " + rt.freeMemory());
System.out.println("Total: " + rt.totalMemory());
回答by fastcodejava
If all your objects are created using some kind of Factory
class you can find number of objects in the heap. Even then you have to have something in the finalize()
method. Of course, this cannot be done for all objects, e.g. the jdk library classes cannot be modified. But if you want to find number of instances of a particular class you have created you can potentially find that.
如果您的所有对象都是使用某种类创建的,Factory
您可以在堆中找到许多对象。即便如此,您也必须在finalize()
方法中有所作为。当然,这不能对所有对象都这样做,例如无法修改jdk 库类。但是,如果您想查找您创建的特定类的实例数,您可能会找到它。
回答by Melv
For debugging, you can use a profiler (like YourKit, a commercial java profiler). You'll find both open source and commercial variants of java profilers.
对于调试,您可以使用分析器(例如 YourKit,一个商业 Java 分析器)。您将找到 Java 分析器的开源和商业变体。
For integration with your code, you might look at using "Aspect Oriented Programming" technique. AOP frameworks (e.g. AspectWerkz) let you change the class files at class load time. This will let you modify constructors to register objects to your "all-my-runtime-objects-framework".
为了与您的代码集成,您可能会考虑使用“面向方面的编程”技术。AOP 框架(例如 AspectWerkz)允许您在类加载时更改类文件。这将允许您修改构造函数以将对象注册到“我的所有运行时对象框架”。
回答by Will Hartung
jmap is the standard java utility that you can use to capture heap dumps and statistics. I can't say what protocol is used by jmap to connect to the JVM to get this info, and it's not clear if this information is available to a program running in the JVM directly (though I'm sure the program can query it's JVM through some socket to get this information).
jmap 是标准的 Java 实用程序,可用于捕获堆转储和统计信息。我不能说 jmap 使用什么协议连接到 JVM 以获取此信息,并且不清楚此信息是否可用于直接在 JVM 中运行的程序(尽管我确定该程序可以查询它的 JVM通过某个套接字获取此信息)。
JVM TI is a tool interface used by C code, and it has pretty much full access to the goings on of the JVM, but it is C code and not directly available by the JVM. You could probably write a C lib and then interface with it, but there's nothing out of the box.
JVM TI 是 C 代码使用的工具接口,它几乎可以完全访问 JVM 的运行情况,但它是 C 代码,JVM 不能直接使用。您可能会编写一个 C 库,然后与它进行交互,但没有任何开箱即用的功能。
There are several JMX MBeans, but I don't think any of them provide an actual object count. You can get memory statistics from these though (these are what JConsole uses). Check out the java.lang.management classes.
有几个 JMX MBean,但我认为它们中的任何一个都没有提供实际的对象计数。您可以从这些中获取内存统计信息(这些是 JConsole 使用的)。查看 java.lang.management 类。
If you want some fast (easy to implement, not necessarily a quick result as a jmap takes some time), I'd fork off a run of jmap, and simply read the resulting file.
如果你想要一些快速的(易于实现,不一定是一个快速的结果,因为 jmap 需要一些时间),我会分叉运行 jmap,然后简单地读取结果文件。
回答by MALAY GHOSH
public class ObjectCount
{
static int i;
ObjectCount()
{
System.out.println(++i);
}
public static void main(String args[])
{
ObjectCount oc = new ObjectCount();
ObjectCount od = new ObjectCount();
ObjectCount oe = new ObjectCount();
ObjectCount of = new ObjectCount();
ObjectCount og = new ObjectCount();
}
}
回答by santhosh
public class NumOfObjects {
static int count=0;
{
count++;
}
public static void main(String[] args)
{
NumOfObjects no1=new NumOfObjects();
System.out.println("no1:" + count);//1
NumOfObjects no2=new NumOfObjects();
System.out.println("no2:"+ count); //2
for (int i=0; i<10;i++)
{
NumOfObjects noi=new NumOfObjects();
}
System.out.println("Total objects:"+count);// 12
}
}
回答by Bin Wang
回答by Anji Pavuluri
class Test1
{
static int count=0;
public Test1()
{
count++;
System.out.println("Total Objects"+" "+count);
}
}
public class CountTotalNumberOfObjects
{
public static void main(String[] args)
{
Test1 t = new Test1();
Test1 t1 = new Test1();
Test1 t3 = new Test1();
Test1 t11 = new Test1();
Test1 t111 = new Test1();
Test1 t13 = new Test1();
}
}
回答by Jakub Kubrynski
The simplest way is to use jmap
tool. If you will print objects histogram at the end you'll see total number of instances and also accumulated size of all objects:
最简单的方法是使用jmap
工具。如果您将在最后打印对象直方图,您将看到实例总数以及所有对象的累计大小:
jmap -histo <PID>
will print whole objects with number of instances and size. The last line will contain total number
jmap -histo <PID>
将打印具有实例数和大小的整个对象。最后一行将包含总数
Total 2802946 174459656
Total 2802946 174459656
Second column is total instances count, and last is total bytes.
第二列是总实例数,最后一列是总字节数。