Memory allocation!

Memory allocation in Java:

Rupam Jha
JavaMadeTranquil
Published in
5 min readFeb 27, 2021

--

Hello Comrades,

Keeping it simple to understand I will be covering here just a basic walkthrough on what a memory allocation in Java is all about? As we all are aware that a JVM is an abstract computing machine which enables a computer to run a program. The JVM loads the code, verifies it, executes it, manages the memory[allocating memory from OS, managing Java allocation including heap compaction and garbage collection] and provides the runtime environment.

Memory allocation being a part of JVM [Java Virtual Machine]. So understanding why Java needs a JVM and no other languages needs a VM is important at this point. In many other languages, the compiler produces the machine code for a specific system, however, in Java, the Java compiler always produces a code for the virtual machine which is called JVM and not for the specific system. It is JVM which holds tag of being platform dependent, thus making Java - a platform independent programming language. A JVM converts the Java bytecode to the machine interpretable language. The two primary functions of JVM is to allow Java program to run on any device or operating system and to manage and optimize the program memory.

Memory management is a form of resource management applied to a computer memory. The essential requirement of memory management is to provide ways to allocate portions of memory to programs at their request, and free it for reuse when no longer needed.

To have an even better understanding of the article, I would suggest you to read this article prior to proceeding ahead.

As mentioned above we will proceed ahead understanding concepts on how a memory allocation is done and what errors can occur in case of memory shortage. Memory allocation is an action of assigning the physical or the virtual memory address space to a process.

JVM memory is divided into multiple parts : Heap |Non-Heap |others. Heap memory is the runtime data area from which the memory for all the java class instances and arrays is allocated. The heap is created when JVM starts-up and may increase or decrease in size as application runs. Non-Heap memory is created at the time of JVM start-up and stores per-class structures such as fields and methods data as well as interned strings [String Interning is a method of storing only one copy of each distinct String Value, which must be immutable. By applying String.intern() on a couple of strings will ensure that all strings having the same contents share the same memory]. The other memory is used to store JVM code itself, JVM internal structures, loaded profile agent codes and data. The two fundamental methods of memory allocation are static and dynamic memory allocation.

Static memory allocation assigns the memory before its execution where as dynamic memory allocation assigns the memory to a process during the execution.

Memory allocation is general aspect of term “binding”. Assume we declare a variable of a class type and create a reference to it. For storing any values to these variables, we must have memory allocated to them. Hence we can term memory allocation as “an act of assigning the memory address to the attributes of the variables”

eg: int a = 10;

Static memory allocation is when a compiler compiles the program and generates object files, the linker merges these object files to an executable file and with the help of a loader this executable file is loaded in the main memory. The size of the data required by the process has to be known prior to execution in static allocation, thus making it more efficient leading to a faster execution of a process. Dynamic memory allocation takes place during the execution of a program. Here the memory is allocated to the entities when they are used for the first time while the program executes. It reduces memory wastage as it allocates the exact memory space needed by the entity.

Memory management is the functionality of an operating system which handles primary memory and move the processes back and forth the main memory and disk during execution. Some basic concepts dealing with memory management could be Process Address space, Static|Dynamic loading and linking.

  • Process Address space: It is a set of logical address that a process references in its code. The OS takes care of mapping the logical address to the physical address at the time of memory allocation to the program. There are three types of address being used before and after memory allocation is done. Symbolic address, Relative address and Physical address… Symbolic addresses are the addresses that are used in a source code by variable names, constants and instruction labels. Relative addresses are the addresses that are converted from symbolic address at the compile time. And when the program is loaded into the main memory, the loader generated Physical Addresses. The Virtual and Physical address remains same in case of compile time but differs while execution [runtime].
  • Static Vs Dynamic Loading and Linking: The decision of choosing static or dynamic loading is to be taken at the time of program being developed. If we have to load the program statically, then at the time of compilation, the complete program will be compiled and linked without leaving any external module dependencies. The linker combines the object program with other necessary object modules along with logical addresses.Whereas if we are loading a dynamic program, the compiler will then compile the program and the references of module dependencies that shall be needed. by the program will be provided. When static linking is used, the linker combines all other modules needed by the program into a single executable program to avoid any runtime dependencies. Whereas in case of dynamic linking it is not needed to link the actual module or library with the program, rather references to thm can be attached.

Memory isn’t allocated at the time of object declaration, it gets allocated at the time of reference. For the memory allocation of objects , new() method is used, hence objects are always allocated in the heap memory. The Java Memory Allocation is divided into Heap, Stack, Code and Static section for an effective management. The Code section contains the bytecode, stack contains methods, local variables and reference variables, Heap section containing Objects and might also contain reference variables and the Static section contains only the static data|methods.

A most common error caused in Memory Allocation is “java.lang.OutOfMemoryError ”, usually this is thrown when the JVM cannot allocate an object because it is out of memory and no more memory could be available by the garbage collector. [We ll read about exceptions and errors in upcoming article]

Hope this article helps you in a little better understanding of the memory allocation in Java.

Until next time…

Peace Out!

Rupam Pawan Jha

--

--