Java - Does Null Variable Require Space in Memory

Java - Does null variable require space in memory

In Java, null is just a value that a reference (which is basically a restricted pointer) can have. It means that the reference refers to nothing. In this case you still consume the space for the reference. This is 4 bytes on 32-bit systems or 8 bytes on 64-bit systems. However, you're not consuming any space for the class that the reference points to until you actually allocate an instance of that class to point the reference at.

Edit: As far as the String, a String in Java takes 16 bits (2 bytes) for each character, plus a small amount of book-keeping overhead, which is probably undocumented and implementation specific.

which object requires more memory, null or an empty list?

If, by empty list, you mean:

List<Object> empty = new ArrayList<> ();

Then that will take a lot more space than using null.

But if you plan to use:

List<Object> empty = Collections.emptyList(); // I'm a singleton

Then because all your empty lists will refer to the same object, you will end up with the same memory consumption as if you were using null.

Where is null in memory

No, because in the JVM there's no need for that.
If you're in a native language (C and C++, for instance), NULL is a pointer with a zero value, and that points to the memory base address. Obviously that's not a valid address, but you "can" dereference it anyway - especially in a system without protected memory, like old MS-DOS or small ones for embedded processors. Not that it would be a valid address - usually that location contains interrupt vectors and you shouldn't touch them. And of course in any modern OS that will raise a protection fault.

But in the JVM a reference to an object is more like a handle (i.e. an index in a table) and null is an 'impossible' value (an index that is outside the domain of the table), so it can't be dereferenced and doesn't occupy space in such table.

Does a variable of an object use memory space if declared but not initialised?

I have to disagree a bit:

private float connection[];
private float bias;

The first one (the array) is a reference type. In other words: a (potential) pointer to some memory area. Obviously: as long as that pointer points to null ("nowhere"), no extra memory is required.

But make no mistake, your object itself needs to fit into memory. Meaning: when you instantiate a new Neuron object, then the JVM requests exactly that amount of memory it needs to store a Neuron object. This means: there is a bit of memory allocated to fit that array reference into it, and of course: the memory for your float primitive values, they are all immediately allocated.

It doesn't matter whether you have 0 or 100.00 or 10394.283 stored in that member field: because the JVM made sure that you have enough memory to fit in the required bits and bytes.

Thus: when you really have millions of objects, each float field in your object adds 32 bits. No matter where the value within that field is coming from.

Sure, if your arrays will later hold 5 or 10 or 1000 entries, then that will make up most of your memory consumption. But initially, when you just create millions of "empty" Neuron objects, you have to "pay" for each and any field that exists in your class.

Meaning: when only 1 out of 100 Neuron object will ever need these two fields, then you could decide to have:

  • A BaseNeuron class that doesn't have all 4 fields
  • one or more classes deriving from that class, adding the fields they need

Also note that this can also be the better choice from a design perspective: "empty" values always mean: extra code to deal with that. Meaning: if that array can be null ... then of course, all code dealing with that field has to check whether the array is null before using it. Compare that to: a class not having that array, versus a class where you know that the array is always set and ready to use.

I am not saying that you absolutely must change your design, but as explained: you can reduce your memory footprint, and you could make your design more clear by doing so.

Do null properties of Java objects use memory?

Each field (of reference type) in a class occupies a pointer-sized (32-bit or 64-bit) chunk of memory per instance of the class.

This memory is used to store a reference to the value of the field, which may be a reference to an existing object or a null reference.

How much memory does a Java object use when all its members are null?

No, you need either 4 or 8 bytes ( depending whether it's a 32 or 64 bit system ) for each null you are storing in a field. How would the object know its field was null if there wasn't something stored somewhere to tell it so?



Related Topics



Leave a reply



Submit