为什么不能在托管环境中测量对象的大小? [英] Why can't object size be measured in a managed environment?

查看:101
本文介绍了为什么不能在托管环境中测量对象的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此stackoverflow上存在许多问题的变体,询问如何测量对象的大小(例如

So a number of variations of a question exist here on stackoverflow that ask how to measure the size of an object (for example this one). And the answers point to the fact, without much elaboration, that it is not possible. Can somebody explain in length why is it not possible or why does it not make sense to measure object sizes?

推荐答案

我从标签中猜测您正在询问Java和C#中对象大小的度量.对C#不太了解,因此以下内容仅适用于Java.

I guess from the tags that you are asking about measurements of object sizes in Java and C#. Don't know much about C# therefore the following only pertains to Java.

单个对象的浅大小和保留大小之间也存在差异,我想您正在询问浅大小(这将是得出保留大小的基础).

Also there is a difference between the shallow and detained size of a single object and I suppose you are asking about the shallow size (which would be the base to derive the detained size).

我还解释了您的术语托管环境,您只想在运行时在特定的JVM中知道对象的大小(例如,不计算仅查找对象的大小)源代码).

I also interpret your term managed environment that you only want to know the size of an object at runtime in a specific JVM (not for instance calculating the size looking only at source code).

我的简短回答如下:

  • 测量对象大小是否有意义?是的,它确实.任何在内存限制下运行的应用程序开发人员都乐于知道类布局和对象分配对内存的影响.
  • 在托管环境中进行测量是否不可能?不它不是. JVM必须知道其对象的大小,因此必须能够报告对象的大小.如果我们只有一种要求的方式.
  • Does it make sense to measure object sizes? Yes it does. Any developer of an application which runs under memory constraints is happy to know the memory implications of class layouts and object allocations.
  • Is it impossible to measure in managed environments? No it is not. The JVM must know about the size of its objects and therefore must be able to report the size of an object. If we only had a way to ask for it.

长答案:

有很多原因不能仅从类定义中得出对象大小,例如:

There are plenty of reasons why the object size cannot be derived from the class definition alone, for example:

  • Java语言规范仅给出了基本类型的下限内存要求. int至少消耗4个字节,但实际大小取决于VM.

  • The Java language spec only gives lowerbound memory requirements for primitive types. A int consumes at least 4 bytes, but the real size is up to the VM.

不确定语言规范如何说明引用的大小. JVM中可能的对象数量是否受到任何限制(这将影响对象引用的内部存储大小)?当今的JVM使用4个字节作为参考指针.

Not sure what the language spec tells about the size of references. Is there any constraint on the number of possible objects in a JVM (which would have implications for the size of internal storage for object references)? Today's JVMs use 4 bytes for a reference pointer.

JVM可以(并且确实)填充对象字节以在某个边界处对齐,这可能会扩展对象的大小.当今的JVM通常将对象内存对齐到8个字节的边界.

JVMs may (and do) pad the object bytes to align at some boundary which may extend the object size. Todays JVMs usually align object memory at a 8 byte boundary.

但是所有这些原因并不适用于使用实际内存布局的JVM运行时,最终允许其世代垃圾收集器将对象推入周围,因此必须能够报告对象大小.

But all these reasons do not apply to a JVM runtime which uses actual memory layouts, eventually allows its generational garbage collector to push objects around, and must therefore be able to report object sizes.

那么我们如何知道运行时的对象大小? 在Java 1.5中,我们得到了java.lang.instrument.Instrumentation#getObjectSize(Object). Javadoc说:

So how do we know about object sizes at runtime? In Java 1.5 we got java.lang.instrument.Instrumentation#getObjectSize(Object). The Javadoc says:

返回特定于实现的近似值 指定对象消耗的存储空间.结果可能包括一些 或对象的所有开销,因此对于进行比较很有用 在实施中,但不在实施之间.估计 一次调用JVM可能会改变.

Returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.

阅读一粒盐可以告诉我,有一种合理的方法可以在运行时的一点上获得对象的确切浅层大小.

Reading with a grain of salt this tells me that there is a reasonable way to get the exact shallow size of an object during one point at runtime.

这篇关于为什么不能在托管环境中测量对象的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆