---
title: "Do null properties of Java objects use memory?"  
description: "Do null properties of Java objects use memory?"  
author: "Anonymous User"  
published: 2015-07-20  
updated: 2015-07-20  
canonical: https://www.mindstick.com/forum/23355/do-null-properties-of-java-objects-use-memory  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Do null properties of Java objects use memory?

I am connecting to a [webservice](https://www.mindstick.com/forum/166/webservice-method) that sends down many different JSON objects. Rather than use a [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) JSONObject I plan to use the GSON [library](https://www.mindstick.com/forum/34615/software-framework-vs-library) to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) to POJOs. Rather than a [huge amount](https://answers.mindstick.com/qa/97639/why-are-people-paying-a-huge-amount-to-buy-nft-art) of classes (one for each of the possible JSON [service](https://www.mindstick.com/articles/105963/online-thesis-writing-service-for-college-kids-with-disabilities) [responses](https://answers.mindstick.com/qa/116746/how-to-stream-responses-from-ollama-api)), 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](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties) that get sent down by the [server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) 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](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss) 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.

## Replies

### Reply by Anonymous User

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.

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.

Null is just a value that a reference can have. It is 4 bytes on 32-bit systems or 8 bytes on 64-bit systems


---

Original Source: https://www.mindstick.com/forum/23355/do-null-properties-of-java-objects-use-memory

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
