Java Theory & Practice: Garbage Collection in the 1.4.1 JVM
- Object-oriented programming (OOP) represents a paradigm of programming in which programmers create data "objects" as part of their code. Programmers use these objects to represent complex data types that contain both a state and exhibit certain behaviors. So, for example, a programmer creates a "Sphere" class for a program that does work involving calculations of spheres. The Sphere class might contain data for a radius (its state) and a function to calculate its own area (its behavior).
- Both Java and C++ are OOP languages. However, the difference between the two illustrates the need for garbage collection in Java. In C++, a programmer deals directly with the operating system. This means that when a programmer wishes to create an object, she can do one of two things. First, she can create a "shallow" copy during the coding process. Second, and more common, she can write code that creates objects dynamically in "deep" memory during code execution through the use of "pointers."
- A C++ program uses pointers to allocate memory during code execution. A "pointer" contains a reference to a memory location. When a programmer needs to design code that creates objects during run time, he uses pointers to reference memory to set aside for object creation. That pointer is then the only thing that references that object. Should the program move the pointer reference during execution, the object "pointed to" can no longer be used. It simply sits in memory with no way to access it. When large amounts of objects are created through pointers and left in memory due to loss of pointer reference, this is known as a "memory leak" and can cause serious problems in a program.
- In C++, the responsibility of managing memory and preventing memory leaks falls squarely on the programmer. The Java language, unlike C++, runs on the JVM, abstracted from the operating system. Because of this, the programmer can ignore system memory management as regards objects. Rather, the JVM keeps tabs on existing objects and references, and deletes those that are no longer in use. This way, the task of managing memory falls on the JVM, and the programmer can work on different tasks while confident that memory management is taken care of.
- The 1.4.1 version of the JVM offers a model for garbage collection. The JVM uses an age division paradigm, whereas "young" objects and "old" objects exists in memory. The JVM can promote young objects to old objects based on how often the young objects are copied during program execution. Old objects, then, are considered important and, therefore, not marked for deletion. The 1.4.1 JVM garbage collection system uses a "train" method of collection/deletion, by allowing increasing small garbage collection passes rather than larger, slower collections. Also, the 1.4.1 JVM takes advantage of multiprocessing systems by offering concurrent garbage collection on multiple processors.
Object-Oriented Programming
Objects and Memory
Pointers and Memory Leaks
Garbage Collection
Garbage Collection and the 1.4.1 JVM
Source...