java java对象的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4115239/
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
sizeof java object
提问by daydreamer
How can we find out the size of a java object??
我们如何找出一个java对象的大小??
Example:
例子:
class Person{
String name;
int age;
public Person(String n, int a){
name = n;
age = a;
}
}
Person person = new Person("Andy", 30);
now how can I know the size of person object??
现在我怎么知道人对象的大小?
Thank you
谢谢
回答by sleske
The question is not meaningful, at least not without further context.
这个问题没有意义,至少在没有进一步背景的情况下没有意义。
The notion of "size" in Java is only reasonably well defined for primitives: A byte is 8 bit (unsurprisingly) an int
is 32 bit, a long
64bit, etc. (see e.g. http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.htmlfor a full list).
Java 中“大小”的概念只是为原语定义得相当好:一个字节是 8 位(不出所料)一个int
是 32 位,一个long
64 位等等(参见例如http://download.oracle.com/javase/tutorial /java/nutsandbolts/datatypes.html获取完整列表)。
For object instances, it's more complicated, because:
对于对象实例,它更复杂,因为:
- Object instances can (and usually will) contain references to other instances internally, so you must decide whether to count these dependent instances, and how. What if several instances share a dependency?
- Sometimes, object instances may be reused (e.g. interning of
java.lang.String
, see http://en.wikipedia.org/wiki/String_interning). So if you use x objects of size y, the total size may be smaller than x*y - The JVM has a lot of leeway about how to implement objects and instances internally. It may use different techniques for different instances (e.g. sharing internal data structures), so there may not even be a meaningful "size" to assign to a single object.
- 对象实例可以(并且通常会)在内部包含对其他实例的引用,因此您必须决定是否计算这些依赖实例,以及如何计算。如果多个实例共享一个依赖项怎么办?
- 有时,对象实例可能会被重用(例如 的实习
java.lang.String
,请参阅http://en.wikipedia.org/wiki/String_interning)。因此,如果您使用大小为 y 的 x 个对象,则总大小可能小于 x*y - JVM 有很多关于如何在内部实现对象和实例的余地。它可能对不同的实例使用不同的技术(例如共享内部数据结构),因此甚至可能没有有意义的“大小”来分配给单个对象。
Maybe you could explain why you are interested in object sizes.
也许您可以解释为什么您对对象大小感兴趣。
There are some rules of thumb for estimating the heap memory used by instances (e.g. in the Sun JVM, a java.lang.Object
instance uses 8 byte), but these will depend on the JVM you use.
估计实例使用的堆内存有一些经验法则(例如,在 Sun JVM 中,一个java.lang.Object
实例使用 8 字节),但这些将取决于您使用的 JVM。
Generally, if you want to know about your heap usage, use a memory / heap profiler.
通常,如果您想了解堆使用情况,请使用内存/堆分析器。
Edit:
编辑:
Well, there is (as of JDK 6) a way to get an approximation of the amount of memory used by an object: http://download.oracle.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize%28java.lang.Object%29
好吧,(从 JDK 6 开始)有一种方法可以获取对象使用的内存量的近似值:http: //download.oracle.com/javase/6/docs/api/java/lang/instrument/ Instrumentation.html#getObjectSize%28java.lang.Object%29
It's still only an approximation...
这仍然只是一个近似值......
回答by Knubo
I think you can get something that might help you if you do something like this:
如果你做这样的事情,我认为你可以获得可能对你有帮助的东西:
/* First trigger classloading */
MyObject delegate = new MyObject();
Runtime.getRuntime().gc();
long before = Runtime.getRuntime().freeMemory();
MyObject delegate2 = new MyObject();
long after = Runtime.getRuntime().freeMemory();
System.out.println("Memory used:"+(before-after));
This will hopefully give you the answer you want, or at least an approximation.
这有望给你你想要的答案,或者至少是一个近似值。
回答by sourcedelica
EhCache provides a SizeOfclass that will try to use the Instrumentationagent and will fall back to a different approach if the agent is not loaded or cannot be loaded (details here).
EhCache 提供了一个SizeOf类,该类将尝试使用Instrumentation代理,如果未加载或无法加载代理,它将回退到不同的方法(详情请点击此处)。
Also see the agent from Heinz Kabutz.
另请参阅Heinz Kabutz 的经纪人。
回答by Artur Mkrtchyan
回答by daydreamer
Objects. To determine the memory usage of an object, we add the amount of memory used by each instance variable to the overhead associated with each object, typically 16 bytes. The overhead includes a reference to the object's class, garbage collection information, and synchronization information. Moreover, the memory usage is typically padded to be a multiple of 8 bytes (machine words, on a 64-bit machine). For example, an Integer object uses 24 bytes (16 bytes of overhead, 4 bytes for its int instance variable, and 4 bytes of padding)
对象。为了确定对象的内存使用量,我们将每个实例变量使用的内存量添加到与每个对象相关的开销中,通常为 16 字节。开销包括对对象类的引用、垃圾收集信息和同步信息。此外,内存使用量通常被填充为 8 字节的倍数(机器字,在 64 位机器上)。例如,Integer 对象使用 24 个字节(16 个字节的开销,4 个字节用于其 int 实例变量,以及 4 个字节的填充)
Reference : http://www.amazon.com/Algorithms-4th-Edition-Robert-Sedgewick/dp/032157351X
参考:http: //www.amazon.com/Algorithms-4th-Edition-Robert-Sedgewick/dp/032157351X
回答by Richard J. Ross III
Java has no built in sizeof
operator.
Java 没有内置sizeof
运算符。
You could try and use a serialization method, by serializing an object to memory and getting the size of that data, but that wouldn't necessarily be the size you want.
您可以尝试使用序列化方法,通过将对象序列化到内存并获取该数据的大小,但这不一定是您想要的大小。
You could also have a method called sizeOf()
, like this:
您还可以有一个名为 的方法sizeOf()
,如下所示:
// returns the size of this object in bytes
int sizeOf()
{
int size = 0;
size += name.Length * 2; // each character in name is 2 bytes, no?
size += 4; // for n, which is 32 bits = 4 bytes
}
Note that this implementation doesn't include the metadata inside name, only the number of bytes necessary to make the char[]
for it.
请注意,此实现不包含名称中的元数据,仅包含为其创建所需的字节数char[]
。