Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do null properties of Java objects use memory?

Tags:

java

memory

gson

I am connecting to a webservice that sends down many different JSON objects. Rather than use a Java JSONObject I plan to use the GSON library to convert to POJOs. Rather than a huge amount of classes (one for each of the possible JSON service responses), I want to have a generic java object that can hold all possibilities:

public class GenericJSONResponse{
    public long objectKey;
    public GenericJSONResponse subObject1;
    public String description;
    // ...
}

I think this is a good aproach since the properties that get sent down by the server vary greatly. All server responses will only include a subset of the possible GenericJSONResponse properties. Will the properties that do not get populated take up memory even if they are null? My generic object will have many properties that are not used and I don't want them taking up precious memory.

like image 511
jmcdale Avatar asked Aug 12 '13 15:08

jmcdale


People also ask

Does a null object take up 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.

IS null stored in memory?

First of all, null is not a valid object instance, so there is no memory allocated for it. It is simply a value that indicates that the object reference is not currently referring to an object.

Does null have a memory address?

Memory address 0 is called the null pointer. Your program is never allowed to look at or store anything into memory address 0, so the null pointer is a way of saying "a pointer to nothing". Note that a null pointer is not the same as a null character; do not confuse the two.

Does object occupy memory in Java?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.


2 Answers

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.

like image 76
SLaks Avatar answered Sep 20 '22 01:09

SLaks


If you don't initialize your references then only the amount of memory required for storing the reference will be used.

If you create a new object and assign it to the reference then memory for new object will also be used and the memory required for reference will be used.

If you simply assign an existing object to an existing reference no new memory allocation is required.

like image 29
Juned Ahsan Avatar answered Sep 21 '22 01:09

Juned Ahsan