简体   繁体   English

Java是否为类的字段分配内存,即使它们尚未初始化?

[英]Does Java allocate memory for class' fields even if they are not initialised yet?

I'm trying to perform a small memory optimisation for my Java game. 我正在尝试对Java游戏进行小的内存优化。 It is a bit unclear to me how does Java allocate memory when it comes to fields: 我有点不清楚Java在字段方面如何分配内存:

public class Test {
    private HashMap<String, String> info;

    public Test(boolean createInfo) {
        if (createInfo) {
            info = new HashMap<String, String>();
        }
    }

}

As you can observe, the HashMap info is initialised if you pass true to Test 's constructor. 如您所见,如果将true传递给Test的构造函数,则会初始化HashMap info

Does new Test(true) take up more memory than new Test(false) ? new Test(true)是否比new Test(false)占用更多的内存

Which leads to the more general question: 这导致了更普遍的问题:

When you create a field in a class, does Java "reserve" the necessary memory for such field in case it is initialised, or will it do nothing until you actually initialise it? 当您在类中创建一个字段时,Java是否会为该字段“保留”必要的内存,以防它被初始化,或者在您真正对其进行初始化之前它什么都不做?

There is this question: Is memory allocated for unused fields in Java? 这里有一个问题: 是否为Java中未使用的字段分配了内存? which seems to be almost exactly what I am looking for, but they seem to be asking what already instantiated fields that are unused, whereas I am asking for uninstantiated fields that may or may not be used. 这似乎是几乎正是我期待的,但他们似乎是问什么已经实例化是未使用的,而我要求的,可能会或可能不会使用非实例 字段的 字段

Does new Test(true) take up more memory than new Test(false) ? new Test(true)是否比new Test(false)占用更多的内存?

Yes. 是。

When you create a field in a class, does Java "reserve" the necessary memory for such field in case it is initialised, or will it do nothing until you actually initialise it? 当您在类中创建一个字段时,Java是否会为该字段“保留”必要的内存,以防它被初始化,或者在您真正对其进行初始化之前它什么都不做?

There are two things here - the HashMap reference, and the HashMap object. 这里有两件事HashMap参考和HashMap对象。 Both of them require memory. 它们都需要内存。 When a class instance is created, memory is allocated for all its instance variable. 创建类实例时,将为其所有实例变量分配内存。 In case of int type field, it will allocate 4 bytes, similarly for a reference, it will allocate some memory for that reference (I don't know exactly how much it is). int type字段的情况下,它将分配4个字节,类似于一个引用,它将为该引用分配一些内存(我不知道它到底有多少)。 But surely it will allocate memory for the fields. 但是可以肯定的是它将为这些字段分配内存。

Then when you initialize the fields in the constructor, the actual HashMap object is created, which will again take up memory to store the object. 然后,当您在构造函数中初始化字段时,将创建实际的HashMap对象,该对象将再次占用内存来存储该对象。

The field itself has space for a reference to an object, and that space is allocated when the containing class ( Test ) is instantiated, even if the value placed in that field is null . 字段本身具有用于引用对象的空间,并且在实例化包含的类( Test )时会分配该空间,即使该字段中放置的值为null Your example code doesn't create an extra object whose reference will go in that field when you pass in false . 您的示例代码不会创建一个额外的对象,当您传入false时,该对象的引用将进入该字段。

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

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