简体   繁体   English

Java中的引用变量是否具有任何大小?

[英]Does the reference variable in Java have any size?

In C++ we used sizeof() operator, which function can we use in Java to check the size of an object? 在C ++中,我们使用sizeof()运算符,我们可以在Java中使用哪个函数来检查对象的大小?

My basic doubt is that whether the reference variable in java has any size or not. 我的基本疑问是java中的引用变量是否具有任何大小。 Consider the following example: 考虑以下示例:

SampleClass obj = new SampleClass();

Here, will obj have any size? 在这里, obj有什么大小? If yes, How can we check it in Java? 如果是,我们如何用Java检查它?

obj is a variable, not an object. obj是变量,不是对象。 The value of obj is a reference - which is likely to be 4 or 8 bytes, depending on the JVM. obj是一个参考-可能是4或8个字节,具体取决于JVM。

The size of the object that the value refers to is also JVM-dependent. 该值引用的对象的大小取决于JVM。 As several objects can refer to each other, it's generally tricky to talk about the size of an object in Java in any particularly useful way... what usually matters is how much more memory would be potentially available if a particular object became eligible for garbage collection, and that depends on what other objects the first object refers to - and whether there are other objects that refer to those, too. 由于多个对象可以相互引用,因此以任何特别有用的方式谈论Java中对象的大小通常是棘手的……通常重要的是,如果特定对象符合垃圾回收条件,则可能有多少可用内存集合,这取决于第一个对象引用了哪些其他对象-以及是否还有其他对象引用了这些对象。

Obviously the "raw" size of an object is somewhat important too, and at least somewhat easier to predict (to an approximation, at least), but it's still VM-specific and can't easily be requested at execution time. 显然,对象的“原始”大小也很重要,并且至少在某种程度上更容易预测(至少近似),但是它仍然是特定于VM的,并且在执行时不容易被请求。 (You could create millions of objects, prevent them from being garbage collected, and measure memory differences, but that's the closest I know of, at least outside a debugger API.) (您可以创建数百万个对象,防止它们被垃圾回收,并测量内存差异,但这是我所知道的最接近的,至少在调试器API之外)。

Since you found my comment superb, I just had to post it as an answer, although it may be superflous to the already existing answers: 由于您发现我的评论很棒,因此我只需要将其发布为答案,尽管它可能与现有的答案无关:

Rest assured that a reference variable (like obj ) has a size, although there won't be a platform-independent answer on how large this size is, but there definitely should be a size. 请放心,引用变量(如obj )具有大小,尽管不会有平台无关的答案来说明该大小是多少,但绝对应该有大小。 But due to this platform (or JVM) dependence, Java is not the language to mess with such low-level details. 但是由于这种平台(或JVM)的依赖性,Java并不是使这种底层细节混乱的语言。

Maybe the compiler can optimize some reference variables away as being just aliases for others, but in general obj has to have a size, as it has to somehow store the reference (whatever this is, it is something and not nothing). 也许编译器可以将某些引用变量作为其他变量的别名进行优化,但总的来说obj必须具有一定的大小,因为它必须以某种方式存储引用(无论它是什么,而不是什么也不是)。

obj is only the reference to an instance of SampleClass . obj只是对SampleClass实例的引用。

The size that the instance of SampleClass occupies in the memory depends on the elements the files of the object, and VM. SampleClass实例在内存中占用的大小取决于对象文件和VM的元素。 But even a reference need some memory of course (like in c) 但是,即使引用也需要一定的存储空间(例如c中)

But the java memory model is much more complicated. 但是Java内存模型要复杂得多。 If you are really interessted in how much memory the object need, then I strongly recommend to use a memory analyzer. 如果您真的对对象需要多少内存感兴趣,那么我强烈建议您使用内存分析仪。

Anyway: because java is a VM and the Java VM has the garbage collector, there is no real 1:1 relation ship between the size of the memory you would expect by counting the (living) java objects and the memory the VM allocates from the Operation System. 无论如何:由于Java是VM,而Java VM具有垃圾回收器,因此通过计算(活动)java对象所需的内存大小与VM从VM分配的内存之间并没有真正的1:1关系。操作系统。

You might try getting some insight on the "size" of the reference by using the freeMemory method of the Runtime class. 您可以尝试使用Runtime类的freeMemory方法来获得对引用的“大小”的一些了解。 For example, allocate an array of 1000000 null references: 例如,分配一个包含1000000个空引用的数组:

SampleClass[] array = new SampleClass[1000000];

Check the free memory before and after; 前后检查可用内存; the difference may give a hint on what you want to know (the array itself is an object, though its size is probably much less than 1000000, so the measurement must be pretty precise). 差异可能会提示您要了解的内容(数组本身是一个对象,尽管其大小可能远远小于1000000,所以测量必须非常精确)。

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

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