From HandWiki - Reading time: 10 min
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
(Learn how and when to remove this template message)
|
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 languages used in industry supported manual memory management, though garbage collection has existed since 1959, when it was introduced with Lisp.[1] Today, however, languages with garbage collection such as Java are increasingly popular and the languages Objective-C and Swift provide similar functionality through Automatic Reference Counting. The main manually managed languages still in widespread use today are C and C++ – see C dynamic memory allocation.
Many programming languages use manual techniques to determine when to allocate a new object from the free store. C uses the malloc function; C++ and Java use the new operator; and many other languages (such as Python) allocate all objects from the free store. Determining when an object ought to be created (object creation) is generally trivial and unproblematic, though techniques such as object pools mean an object may be created before immediate use. The real challenge is object destruction – 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 for re-use. 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++ – this contrasts with automatic destruction of objects held in automatic variables, notably (non-static) local variables of functions, which are destroyed at the end of their scope in C and C++.
For example:
Manual memory management is known to enable several major classes of bugs into a program when used incorrectly, notably violations of memory safety or memory leaks. These are a significant source of security bugs.[2]
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.
Manual memory management has one correctness advantage, which is that it allows automatic resource management via the resource acquisition is initialization (RAII) paradigm.
This arises when objects own scarce system resources (like graphics resources, file handles, or database connections) which must be relinquished when an object is destroyed – when the lifetime of the resource ownership should be tied to the lifetime of the object. Languages with manual management can arrange this by acquiring the resource during object initialization (in the constructor), and releasing during object destruction (in the destructor), which occurs at a precise time. This is known as Resource Acquisition Is Initialization (RAII).
This can also be used with deterministic reference counting. In C++, this ability is put to further use to automate memory deallocation within an otherwise-manual framework, use of smart pointers in the language's standard library to perform memory management is a common paradigm. In languages like Rust which enforce strict ownership, RAII becomes the default behavior for managing resources.[3]
This approach is not usable in most garbage collected languages – notably tracing garbage collectors or more advanced reference counting – due to finalization being non-deterministic, and sometimes not occurring at all. That is, 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 languages implementing a garbage collector frequently use manual management for scarce system resources besides memory via the dispose pattern: any object which manages 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. For stack resources (resources acquired and released within a single block of code), this can be automated by various language constructs, such as Python's with, C#'s using-with-resources (which call an object's Dispose() method) or Java's try-with-resources (usable on any object which implements java.lang.AutoCloseable and calls its close() method).
A defer mechanism is a feature that allows the postponing of execution of a piece of code until later, usually either the end of scope or end of a function. This can be seen as similar to a finally block in other languages.
C (beginning in C29) introduces a defer mechanism. A defer block happens only execution reaches it, and executes in reverse order. It runs on scope exit.[4]
#include <stddefer.h>
#include <stdio.h>
#include <stdlib.h>
int processFile(const char path[]) {
FILE* f = fopen(path, "r");
if (!f) {
return -1;
}
defer {
printf("Closing file.\n");
fclose(f);
};
char* buffer = malloc(1024);
if (!buffer) {
return -2;
}
defer {
printf("Freeing buffer.\n");
free(buffer);
};
// Simulate multiple early exits
if (fgets(buffer, 1024, f)) {
return -3; // both defers still run
}
if (buffer[0] == '#') {
return -4; // both defers still run
}
printf("Processing: %s\n", buffer);
return 0;
}
C++ has not yet commented on whether or not it will integrate C defer, as RAII covers this use case.
In Go, defer is used to schedule a function call to run when the surrounding function returns. Multiple defers run in last-in-first-out (LIFO) order.
package main
import "fmt"
func main() {
fmt.Println("start")
defer fmt.Println("first defer")
defer fmt.Println("second defer")
fmt.Println("end")
}
In Zig, defer is block-level and runs when the current scope ends, not just the function. Unlike Go, Zig defer incurs no overhead.
const std = @import("std");
pub fn main() void {
std.debug.print("start\n", .{});
{
defer std.debug.print("inner defer\n", .{});
std.debug.print("inside block\n", .{});
}
std.debug.print("end\n", .{});
}
Zig also includes errdefer, which runs only if the function exits with an error.
Manual allocation is also known to be more appropriate for systems where memory is a scarce resource, due to faster reclamation. 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 remain in an unreclaimed state for longer than in manually managed systems, because they are not immediately reclaimed, increasing the effective working set size.
Manual management has a number of documented performance disadvantages:
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.
Manual memory management and garbage collection both suffer from potentially unbounded deallocation times – manual memory management because deallocating a single object may require deallocating its members, and recursively its members' members, etc., while garbage collection may have long collection cycles. This is especially an issue in real time systems, where unbounded collection cycles are generally unacceptable; real-time garbage collection is possible by pausing the garbage collector, while real-time manual memory management requires avoiding large deallocations, or manually pausing deallocation.