Adding Data

We could now use the remainder of the memory to store other names. Suppose the next name we wish to store is “Alexandra”. Our memory pool may look as follows:

Memory Pool - George Alex

Notice that the next memory block may not be contiguous with the previous one. This can happen if the memory allocator decides to allocate the next block from another part of memory.

Often the memory allocator needs to allow for a little bit more than the user requested so that it can store additional information, such as its size, who owns it etc. This information is required so that when the space is no longer required the memory allocator can return the correct number of memory locations to the global memory pool.

The important points to remember are:

  1. The memory allocator keeps track of what is allocated to a user and what is free in the global memory pool.
  2. When we receive memory from the memory pool we must assume that it will contain garbage and we should initialise it to a known value.  (Note: Java will initialise it to null values).
  3. We cannot assume that successive requests will allocate contiguous memory blocks.
  4. We must free the memory when our program is finished with it so that it can be returned to the global memory pool. Java will do this automatically.

Next: File Compression