Manual memory management
Encyclopedia
In computer science
, manual memory management refers to the usage of manual instructions by the programmer to identify and deallocate unused objects, or garbage
. Up until the mid 1990s, the majority of programming language
s used in industry supported manual memory management. Today, however, languages with garbage collection
are becoming increasingly popular; the main manually managed languages still in widespread use today are C
and C++
.
function; C++ and Java use the new operator; determination of when an object ought to be created is trivial and unproblematic. The fundamental issue is determination of when an object is no longer needed (i.e. is garbage), and arranging for its underlying storage to be returned to the free store so that it may be re-used to satisfy future memory requests. In manual memory allocation, this is also specified manually by the programmer; via functions such as free in C, or the delete operator in C++.
Languages which exclusively use garbage collection
are known to avoid the last two classes of defects. Memory leaks can still occur (and bounded leaks frequently occur with generational or conservative garbage collection), but are generally less severe than memory leaks in manual systems.
s, can arrange for such actions to occur at the precise time of object destruction; this is known as the resource acquisition is initialization
paradigm (RAII). In C++, this ability is put to further use to automate memory deallocation within an otherwise-manual framework, use of the auto ptr
template in the language's standard library to perform memory management is a common paradigm. (auto_ptr is not suitable for all object usage patterns).
Garbage collected languages have difficulty with this paradigm, as it is difficult to define (or determine) when or if a finalizer
method might be called; this is commonly known as the finalizer problem. Java and other GC'd languages frequently use manual management for scarce system resources besides memory (such as graphics resources); any object which manages graphics resources is expected to implement the dispose method, which releases any such resources and marks the object as inactive. Programmers are expected to invoke dispose manually as appropriate; to prevent "leaking" of scarce graphics resources. Depending on the finalize method (how Java implements finalizers) to release graphics resources is widely viewed as poor programming practice among Java programmers.
. Manual allocation does not suffer from the long "pause" times often associated with garbage collection (although modern garbage collectors have collection cycles which are often not noticeable), and manual allocation frequently has superior locality of reference. This is especially an issue in real time
systems, where unbounded collection cycles are generally unacceptable.
Manual allocation is also known to be more appropriate for systems where memory is a scarce resource. Memory systems can and do frequently "thrash" as the size of a program's working set
approaches the size of available memory; unused objects in a garbage-collected system tend to remain in an un-reclaimed state for longer than in manually managed systems, increasing the effective working set size.
On the other hand, manual management has documented performance disadvantages:
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...
, manual memory management refers to the usage of manual instructions by the programmer to identify and deallocate unused objects, or garbage
Garbage (computer science)
Garbage, in the context of computer science, refers to objects, data, or other regions of the memory of a computer system , which will not be used in any future computation by the system, or by a program running on it...
. Up until the mid 1990s, the majority of programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s used in industry supported manual memory management. Today, however, languages with garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...
are becoming increasingly popular; the main manually managed languages still in widespread use today are C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
and C++
C++
C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
.
Description
All programming languages use manual techniques to determine when to allocate a new object from the free store. C uses the mallocMalloc
C dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....
function; C++ and Java use the new operator; determination of when an object ought to be created is trivial and unproblematic. The fundamental issue is determination of when an object is no longer needed (i.e. is garbage), and arranging for its underlying storage to be returned to the free store so that it may be re-used to satisfy future memory requests. In manual memory allocation, this is also specified manually by the programmer; via functions such as free in C, or the delete operator in C++.
Manual management and correctness
Manual memory management is known to enable several major classes of bugs into a program, when used incorrectly.- When an unused object is never released back to the free store, this is known as a memory leakMemory leakA memory leak, in computer science , occurs when a computer program consumes memory but is unable to release it back to the operating system. In object-oriented programming, a memory leak happens when an object is stored in memory but cannot be accessed by the running code...
. In some cases, memory leaks may be tolerable, such as a program which "leaks" a bounded amount of memory over its lifetime, or a short-running program which relies on an operating systemOperating systemAn operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
to deallocate its resources when it terminates. However, in many cases memory leaks occur in long-running programs, and in such cases an unbounded amount of memory is leaked. When this occurs, the size of the available free store continues to decrease over time; when it finally is exhausted the program then crashes.
- When an object is deleted more than once, or when the programmer attempts to release a pointer to an object not allocated from the free store, catastrophic failure of the dynamic memory management system can result. The result of such actions can include heap corruption, premature destruction of a different (and newly created) object which happens to occupy the same location in memory as the multiply deleted object, and other forms of undefined behavior.
- Pointers to deleted objects become wild pointers if used post-deletion; attempting to use such pointers can result in difficult-to-diagnose bugs.
Languages which exclusively use garbage collection
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...
are known to avoid the last two classes of defects. Memory leaks can still occur (and bounded leaks frequently occur with generational or conservative garbage collection), but are generally less severe than memory leaks in manual systems.
Resource acquisition is initialization
Manual memory management has one correctness advantage, which comes into play when objects own scarce system resources (like graphics resources, file handles, or database connections) which must be relinquished when an object is destroyed. Languages with manual management, via the use of destructorDestructor (computer science)
In object-oriented programming, a destructor is a method which is automatically invoked when the object is destroyed...
s, can arrange for such actions to occur at the precise time of object destruction; this is known as the resource acquisition is initialization
Resource Acquisition Is Initialization
Resource Acquisition Is Initialization is a programming idiom used in several object-oriented languages like C++, D and Ada. The technique was invented by Bjarne Stroustrup to deal with resource deallocation in C++...
paradigm (RAII). In C++, this ability is put to further use to automate memory deallocation within an otherwise-manual framework, use of the auto ptr
Auto ptr
auto_ptr is a class template available in the C++ Standard Library that provides some basic RAII features for C++ raw pointers....
template in the language's standard library to perform memory management is a common paradigm. (auto_ptr is not suitable for all object usage patterns).
Garbage collected languages have difficulty with this paradigm, as it is difficult to define (or determine) when or if a finalizer
Finalizer
In object-oriented programming languages that use garbage collection, a finalizer is a special method that is executed when an object is garbage collected. It is similar in function to a destructor...
method might be called; this is commonly known as the finalizer problem. Java and other GC'd languages frequently use manual management for scarce system resources besides memory (such as graphics resources); any object which manages graphics resources is expected to implement the dispose method, which releases any such resources and marks the object as inactive. Programmers are expected to invoke dispose manually as appropriate; to prevent "leaking" of scarce graphics resources. Depending on the finalize method (how Java implements finalizers) to release graphics resources is widely viewed as poor programming practice among Java programmers.
Performance
Many advocates of manual memory management argue that it affords superior performance when compared to automatic techniques such as garbage collectionGarbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...
. Manual allocation does not suffer from the long "pause" times often associated with garbage collection (although modern garbage collectors have collection cycles which are often not noticeable), and manual allocation frequently has superior locality of reference. This is especially an issue in real time
Real-time computing
In computer science, real-time computing , or reactive computing, is the study of hardware and software systems that are subject to a "real-time constraint"— e.g. operational deadlines from event to system response. Real-time programs must guarantee response within strict time constraints...
systems, where unbounded collection cycles are generally unacceptable.
Manual allocation is also known to be more appropriate for systems where memory is a scarce resource. Memory systems can and do frequently "thrash" as the size of a program's working set
Working set
Peter Denning defines “the working set of information W of a process at time t to be the collection of information referenced by the process during the process time interval ”. Typically the units of information in question are considered to be memory pages...
approaches the size of available memory; unused objects in a garbage-collected system tend to remain in an un-reclaimed state for longer than in manually managed systems, increasing the effective working set size.
On the other hand, manual management has documented performance disadvantages:
- Calls to delete and such incur an overhead each time they are made, this overhead can be amortized in garbage collection cycles. This is especially true of multithreaded applications, where delete calls must be synchronized.
- The allocation routine may be more complicated, and slower. Some garbage collection schemes, such as those with heap compaction, can maintain the free store as a simple array of memory (as opposed to the complicated implementations required by manual management schemes).
External links
- The Memory Management Reference
- Richard Jones and Rafael Lins, Garbage Collection: Algorithms for Automated Dynamic Memory Management, Wiley and Sons (1996), ISBN 0-471-94148-4