Memory Allocation

Two different methods of memory allocation are used by programming languages, static and dynamic. With static memory allocation, memory is allocated at compile time and remains allocated for the duration of the program. With dynamic memory allocation, memory is allocated when a new instance of an object is created. In Java this is done by means of the new constructor. Dynamically-allocated memory can be discarded when it is no longer required.

One of the major differences between differences between Java and older languages such like C and C++ is the way in which Java manages memory. Memory for everything apart from primitive data types is allocated dynamically. Java avoids the use of “pointers” that can reference arbitrary areas of memory and adds object garbage collection and highlevel arrays. These features avoid problems in areas like safety, portability, and optimisation. The single biggest source of programming errors in C or C++ was explicit memory allocation and deallocation.

As well as maintaining objects in memory, the Java runtime system keeps track of all references to those objects. When an object is no longer needed, Java automatically removes it from memory. This generally takes place in the background.

The key idea in dynamic memory allocation is to record all the available memory as a large pool that can be used for any purpose whatsoever.  When you require some memory for your structure you ask for it and if it is available it is given from the pool. We can represent the memory pool available in the computer by one large block, as shown in the diagram below. We will use grey to indicate that this pool of memory is empty.  All of the memory is free at present.

Memory Pool - Empty

So what happens if we need to store the name “George”?  Using a string to store this information would result in the allocation of enough space to hold the contents of the string.  So, we can assume that we need a byte for each character stored in the string; in this instance we are storing 6 characters so we need six bytes.  Unlike other languages, like C or C++, Java does not need an additional character to mark the end of the string.

Therefore, we would require 6 bytes in order to store the information. In Java we would create a new string by calling the new constructor. What will happen? The memory allocation routine will allocate 6 bytes of memory to you. If we obtain the first 6 bytes from the pool then the memory pool would look as shown below:

Memory Pool 6 Alloc

The memory allocator keeps track of what memory is free and what memory has been allocated. It knows that the 6 bytes that it supplied to you are no longer free. We represent this fact by the cells that are no longer grey.

The bytes have a question mark ‘?’ in them, indicating that we do not know the contents of these bytes. Some programming languages make no guarantee about the contents of the memory allocated by the memory allocator process, but Java will always ensure that there are null values in the memory allocated. Initialising the memory with the contents of the string “George” would give us:

Memory Pool - GeorgeNext: Adding Data