MANAGE MAIN MEMORY

When working with the simple programs you've played with so far, you haven't had to worry much about having enough memory to hold your variables. The Help system for Liberty Basic does not even mention memory; however, if you try to dimension some large array with statements such as the following, you find the program fails mysteriously.

dim verticesX(10000000)
dim verticesY(10000000)

This fails because your computer doesn't have an unlimited amount of space in main memory to store data, and you run into trouble when you request the space to store 20 million numbers. In fact, if you're running a typical Windows or Mac desktop, there are many programs of various sizes sharing memory so the amount available for any one program is greatly reduced.

The following are some of the types of programming that require lots of memory:

  • Games with complex graphics
  • Computer-aided design
  • Web servers and other utilities that service many users and run for long periods

This brings you to the topic of memory management. You run into two types of memory management in programming languages -- automatic, and what might be termed explicit memory management. Using C, you have to explicitly request memory from the operating system for something such as the large arrays of numbers in the previous example. You must also explicitly tell the operating system when you are through with it. Too, you must make sure that you don't have two parts of your program trying to use the same memory for different purposes.

If, through bad programming practice or an error, you forget to return the memory to the control of the operating system, your program experiences what's called a memory leak . A memory leak means your program keeps requesting memory, but never gives it back to the operating system. Eventually the operating system refuses to give you any more memory and the program dies.

A memory leak problem can be one of the most frustrating things in all of computing. Frequently they only show up when an important customer is depending on your program to run correctly on Saturday night. Memory leaks can be subtle and hard to find, which is why modern object-oriented programming languages such as Java and C# provide for automatic memory management.

Automatic Memory Management

In object-oriented programming languages, requesting memory from the operating system is handled behind the scenes by the object creation mechanism. You saw the mechanism in action in the previous lesson. The following code says to create a new Date type object.

new Date()

When that code is executed, the Java program either uses some memory space it has already reserved, or requests more from the operating system and builds a Date object there. The amount of memory naturally depends on how complicated the object is.

In object-oriented programming, each object is responsible for managing the data it contains. The memory used is isolated from other parts of the program and access is controlled by the object.

When your program is no longer using the object, Java detects this fact by a process called garbage collection . Essentially, the garbage collection process looks at all the parts of your program and determines all the objects that are still in use. Every other object is considered garbage. All you have to do is stop using a variable, and the memory is reclaimed automatically.