next up previous
Next: Linked lists Up: Unit 12 Previous: Dealing with the dimension

Dynamic memory management

We have just given an example of dynamic memory management, in which memory resources are allocated whenever they are needed, and freed whenever they are not necessary anymore. This is a very important aspect, since it defines the modalities by which programs interact with machine memory in order to store the data they require.

Actually, we have already seen at the beginning of the course that in Java we have the ability to allocate memory by creating new objects. This is done by means of the new operator that invokes a constructor of a class. In this case, we need to use a distinct variable for each element that we want to store and manipulate in the program.

When we want to define and manipulate collections of objects, we need an additional mechanism to allocate memory dynamically, that should take into account that, in general, the number of elements in the collection we have to deal with is not known a priori. Linked lists, which we are introducing now, are defined precisely in such a way as to allow for dynamically allocating and deallocating memory, depending on the requirements of the application.

As opposed to the dynamic memory management provided by lists, we could say that with arrays we have a static memory management, since their size cannot be changed, once they are created.


next up previous
Next: Linked lists Up: Unit 12 Previous: Dealing with the dimension