Open In App

Where is the memory allocated for Arrays in Java?

Last Updated : 16 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Each time an array is declared in the program, contiguous memory is allocated to it. 

Array base address: The address of the first array element is called the array base address. Each element will occupy the memory space required to accommodate the values for its type, i.e.; depending on the data type of the elements, 1, 4, or 8 bytes of memory are allocated for each element. The next memory address is assigned to the next element in the array. This memory allocation process continues until the number of array elements is exceeded.

JVM memory locations: Before moving to the question of where an array is stored in Java, we must know about the memory locations in JVM (Java Virtual Machine). They are:

  • Heap − Java objects are stored in an area called the heap. The heap is created at the JVM startup and can grow or shrink while the application is running. When the piles of the heap fill up, the waste is collected. During garbage collection, objects that are no longer in use are deleted, making room for new objects.
  • Stack − A stack is a (Last In First Out) data structure. It supports two basic operations called push and pop. The push operation adds an item to the top of the stack and the pop operation removes an item from the top of the stack. It acts as a storage for the partial variable.
  • PC Registers −The program counter (PC) register keeps track of the current instruction being executed at any given time. It is like a pointer to the current instruction in a program’s instruction sequence.
  • Execution Engine − The runtime engine is the main component of the Java Virtual Machine (JVM). It communicates with different memory areas of the JVM. Each thread of a running application is a separate instance of the virtual machine runtime
  • Native Method Stack- Native methods can access system-specific functions and APIs that are not available directly in Java. Native Method is an interface for Java to call non-Java code. A Native Method is a Java method: the method is implemented in a non-Java language, such as C.

Where is the memory allocated for an array in Java?

Memory is allocated in Heap are for the Array in Java.

In Java reference types are stored in the Heap area. As arrays are also reference types, (they can be created using the “new” keyword) they are also stored in the Heap area. Arrays are used to store multiple values ​​in a single variable, instead of declaring separate variables for each value. In Java, an array stores primitive values ​​(int, char, etc) or references (i.e. pointers) to objects.

Single-dimension Array:

int arr[] = new int[5];

The int[] arr is just the reference to the array of five integers. If you create an array with 50 integers, it is the same – an array is allocated and a reference is returned.

int intArray[];    //declaring array
intArray = new int[10];  // allocating memory to array

We use new to allocate an array, you must specify the type and number of elements to allocate.

Array of Objects:

In case of array of objects, the reference to the array is stored in the heap. And the array elements themselves also store the reference to the objects. 

class A{
    . . . 
}

public class Ex{
    public static void main(String[] args) {
        A arr[] = new A[5]
    }
}


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads