Do uninitialized primitive instance variables use

2019-01-23 10:25发布

In Java, does it cost memory to declare a class level instance variable without initializing it?
For example: Does int i; use any memory if I don't initialize it with i = 5;?

Details:

I have a huge super-class that many different (not different enough to have their own super classes) sub-classes extend. Some sub-classes don't use every single primitive declared by the super-class. Can I simply keep such primitives as uninitialized and only initialize them in necessary sub-classes to save memory?

6条回答
劫难
2楼-- · 2019-01-23 10:59

Yes. In your class level variables will assign to its default value even if you don't initialize them.

In this case you int variables will assign to 0 and will occupied 4 bytes per each.

查看更多
爷的心禁止访问
3楼-- · 2019-01-23 11:03

Neither the Java Language Specification nor the Java Virtual Machine Specification specifies the answer to this because it's an implementation detail. In fact, JVMS §2.7 specifically says:

Representation of Objects

The Java Virtual Machine does not mandate any particular internal structure for objects.

In theory, a conformant virtual machine could implement objects which have a lot of fields using set of bit flags to mark which fields have been set to non-default values. Initially no fields would be allocated, the flag bits would be all 0, and the object would be small. When a field is first set, the corresponding flag bit would be set to 1 and the object would be resized to make space for it. [The garbage collector already provides the necessary machinery for momentarily pausing running code in order to relocate live objects around the heap, which would be necessary for resizing them.]

In practice, this is not a good idea because even if it saves memory it is complicated and slow. Access to fields would require temporarily locking the object to prevent corruption due to multithreading; then reading the current flag bits; and if the field exists then counting the set bits to calculate the current offset of the wanted field relative to the base of the object; then reading the field; and finally unlocking the object.

So, no general-purpose Java virtual machine does anything like this. Some objects with an exorbitant number of fields might benefit from it, but even they couldn't rely on it, because they might need to run on the common virtual machines which don't do that.

A flat layout which allocates space for all fields when an object is first instantiated is simple and fast, so that is the standard. Programmers assume that objects are allocated that way and thus design their programs accordingly to best take advantage of it. Likewise, virtual machine designers optimize to make that usage fast.

Ultimately the flat layout of fields is a convention, not a rule, although you can rely on it anyway.

查看更多
干净又极端
4楼-- · 2019-01-23 11:08

Yes, memory allocates though you are not assigning any value to it.

int i;

That takes 32 bit memory (allocation). No matter you are using it or not.

Some sub-classes don't use every single primitive declared by the super-Class. Can I simply keep such primitives as uninitialized and only initialize them in necessary sub-classes to save memory?

Again, no matter where you initialized, the memory allocates.

Only thing you need to take care is, just find the unused primitives and remove them.

Edit: Adding one more point that unlike primitive's references by default value is null, which carries a a memory of

 4 bytes(32-bit) 
 8 bytes on (64-bit)
查看更多
仙女界的扛把子
5楼-- · 2019-01-23 11:09

All members defined in your classes have default values, even if you don't initialize them explicitly, so they do use memory.

For example, every int will be initialized by default to 0, and will occupy 4 bytes.

For class members :

int i;

is the same as :

int i = 0;

Here's what the JLS says about instance variables :

If a class T has a field a that is an instance variable, then a new instance variable a is created and initialized to a default value (§4.12.5) as part of each newly created object of class T or of any class that is a subclass of T (§8.1.4). The instance variable effectively ceases to exist when the object of which it is a field is no longer referenced, after any necessary finalization of the object (§12.6) has been completed.

查看更多
该账号已被封号
6楼-- · 2019-01-23 11:13

The original question talks about class level variables and the answer is that they do use space, but it's interesting to look at method scoped ones too.

Let's take a small example:

public class MemTest {
    public void doSomething() {
        long i = 0;  // Line 3
        if(System.currentTimeMillis() > 0) {
            i = System.currentTimeMillis();
            System.out.println(i);
        }
        System.out.println(i);
    }
}

If we look at the bytecode generated:

  L0
    LINENUMBER 3 L0
    LCONST_0
    LSTORE 1

Ok, as expected we assign a value at line 3 in the code, now if we change line 3 to (and remove the second println due to a compiler error):

long i; // Line 3

... and check the bytecode then nothing is generated for line 3. So, the answer is that no memory is used at this point. In fact, the LSTORE occurs only on line 5 when we assign to the variable. So, declaring an unassigned method variable does not use any memory and, in fact, doesn't generate any bytecode. It's equivalent to making the declaration where you first assign to it.

查看更多
神经病院院长
7楼-- · 2019-01-23 11:17

In Java, when you declare a class attribute such as String str;, you are declaring a reference to an object, but it is not pointing yet to any object unless you affect a value to it str=value;. But as you may guess, the reference, even without pointing to a memory place, consumes itself some memory.

查看更多
登录 后发表回答