简体   繁体   English

如何确定我的课程使用多少内存?

[英]How can I determine how much memory my classes use?

I had a discussion with my Computer Science teacher about how much memory a class in JAVA takes up. 我与我的计算机科学老师讨论了JAVA课程的内存占用量。 Neither of us knew a specific answer, but I had a few questions that he couldn't answer. 我们俩都不知道具体的答案,但我有一些他无法回答的问题。

1) Is it as simple as each classes' amount of bytes that its primitive data types use is what determines its memory usage? 1)它的原始数据类型使用的每个类的字节数是否与确定其内存使用情况一样简单?

2) If you make an instance of a class and set it to null, would it still take up as many bytes as an instance that is not null? 2)如果你创建一个类的实例并将其设置为null,它是否仍然占用与非空的实例一样多的字节?

3) Does a String with 100 characters in it, have the same amount of bytes as a single character string? 3)包含100个字符的字符串是否与单个字符串具有相同的字节数?

4) If a class has no variables, no methods, nothing, and just looks like this: 4)如果一个类没有变量,没有方法,没有,只是看起来像这样:

public class Test{}

would it still take up memory? 还会占用记忆吗?

You're confusing between two different things: the amount of memory that a class takes (meta-data, or "the blueprint" of the instances) and the amount of memory that an instance takes (data/object). 你在两个不同的东西之间混淆:一个class占用的内存量(元数据,或实例的“蓝图”)和instance占用的内存量(数据/对象)。

For classes you can use java.lang.instrument.Instrument.getObjectSize(yourObject.getClass()); 对于类,您可以使用java.lang.instrument.Instrument.getObjectSize(yourObject.getClass()); and as for an instance - it doesn't necessarily has a fixed size. 而对于一个实例 - 它不一定具有固定的大小。 For example, if you have an instance of Person and it has a name field - then different names will capture different space according to the length of the String. 例如,如果您有一个Person实例并且它有一个name字段 - 那么不同的名称将根据String的长度捕获不同的空间。

You can use profilers in order to see how much memory instances take, YourKit is an excellent profiler but it's not free. 您可以使用分析器来查看内存实例占用的内容, YourKit是一个优秀的分析器,但它不是免费的。

Use this: java.lang.instrument package 使用此: java.lang.instrument包

Compile that library and include in your jar. 编译该库并包含在jar中。 Then: 然后:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}

In Manifest.MF add: 在Manifest.MF中添加:

Premain-Class: ObjectSizeFetcher

Now do: 现在做:

public class C {
    private int x;
    private int y;

    public static void main(String [] args) {
        System.out.println(ObjectSizeFetcher.getObjectSize(new Test()));
    }
}

You can invoke your code with: 您可以使用以下命令调用代码:

java -javaagent:ObjectSizeFetcherAgent.jar C

source: In Java, what is the best way to determine the size of an object? source: 在Java中,确定对象大小的最佳方法是什么?

You can measure the size using the technique here: 您可以使用此处的技术测量尺寸:

https://stackoverflow.com/a/383597/3049628 https://stackoverflow.com/a/383597/3049628

Beyond that you can usually guess the rough amount of memory (most things are either 4 bytes or 8 depending on in a 32 bit or 64 bit systems. Arrays are packed better.) but the actual implementation is JVM dependent so there is no fixed rule. 除此之外,您通常可以猜测大量的内存(大多数事情是4个字节或8个,具体取决于32位或64位系统。数组打包得更好。)但实际实现依赖于JVM,所以没有固定的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM