Where Does Java Store Variables and Objects?

Storage in Java
Storage in Java

Where Does Java Store Variables and Objects? Or Where Storage lives?

The java storage is divided into the below parts:
  • Register
  • The stack-RAM
  • The heap-RAM
  • Static Storage
  • Constant Storage
  • Non RAM storage


The register is the fastest of all storage(inside processor). Mainly registers are limited so are allocated based on the need of compiler.Users have no direct access or control over these memory devices.Only compiler can use them as needed.

The stack-RAM:

This is the general RAM(Random Access Memory) area and acts as bridge between compiler and users. User can allocate and access space in the Stack-RAM area. There is stack pointer to allocate or deallocate the storage. The stack pointer is moved down to create new memory and moved up to release the memory.It is slower and cheaper than register but an cost effective and faster way to allocate or deallocate memory.As the java compiler must know while it is creating the program,the exact size and lifetime of all the data that is stored on the stack due to the fact that it must allocate or deallcate accordingly, it puts a constraint as limit of flexibility.All variables,methods,local variables are stored here.

The heap-RAM:

It is a pool of memory(in RAM area) where all java objects live.Unlike the stack,the compiler does not need to know how much storage, it need to allocate from the heap or how long the storage must stay on the heap.Thus there is a flexibility in using storage on the heap. Whenever we need to create an object we write using new but takes sometimes to allocate heap storage. It is slower than stack.

Static Storage:

Static storage refers in a fixed location of RAM.Static storage contains data that is available for entire time of the program execution.Some certain elements of an objects are static but java objects are not static.

Constant Storage:

Constant values are often placed directly in the code itself.They are safe as they do not change.Sometimes constants are cordoned off by themselves so they can be optionally placed in a ROM.(Read Only Memory mainly in Embedded Systems ).

Non RAM storage:

In this case java data lives completely outside of a program.The data still can exist while the program is not running.They are outside the control of the program.


Streamed Objects:

Where objects are tuned into streams of bytes. Generally we use this technique to send to another machine.

Persistent Objects:

Where the objects are placed on the disk. So they will hold the state even when the program is not running.Turning object into flat file or something that can exist or some other medium. They can restructured and restored into regular RAM based object.

Java provides support for lightweight persistence and mechanism (JDBC,Hibernate etc) to support for storing and retrieving objects.

Where Does Java Store Variables and Objects? Where Does Java Store Variables and Objects? Reviewed by Animesh Chatterjee on December 24, 2018 Rating: 5

No comments:

Powered by Blogger.