Object lifetime
This articleneeds additional citations forverification.(January 2013) |
Inobject-oriented programming(OOP),object lifetimeis the period of time between anobject'screationand its destruction. In some programming contexts, object lifetime coincides with thelifetimeof avariablethat represents the object. In other contexts – where the object is accessed byreference– object lifetime is not determined by the lifetime of a variable. For example, destruction of the variable may only destroy the reference; not the referenced object.
Terminology
[edit]Aspects of object lifetime vary betweenprogramming languagesand within implementations of a language. The core concepts are relatively common, but terminology varies. For example, the concepts of create and destroy are sometimes termedconstructanddestructand the language elements are termedconstructor(ctor) anddestructor(dtor).
Determinism
[edit]Creation of an object is generallydeterministic,but destruction varies by programming context. Some contexts allow for deterministic destruction, but some do not. Notably, in a garbage-collection environment, objects are destroyed when the garbage collector chooses.
The syntax for creation and destruction varies by programming context. In many contexts, including C++, C# and Java, an object is created via special syntax likenewtypename()
.In C++, that providesmanual memory management,an object is destroyed via thedelete
keyword. In C# and Java, with no explicit destruction syntax, the garbage collector destroys unused objects automatically and non-deterministically.
An alternative and deterministic approach to automatic destruction is where the object is destroyed when code decrements the object'sreference countto zero.
With anobject pool,where objects may be created ahead of time and reused, the apparent creation and destruction of an object may not correspond to actual. The pool provides reinitialization for creation and finalization for destruction. Both creation and destruction may be non-deterministic.
Objects withstatic memory allocationhave a lifetime that coincides with therunof a program, but theorderof creation and destruction of the various static objects is generally non-deterministic.
Life cycle
[edit]Objectlife cyclerefers to the events that an object experiences including and between creation and destruction.
Life cycle generally includesmemory managementand operations after allocation and before deallocation. Object creation generally consists ofmemory allocationandinitializationwhere initialization includes assigning values to fields and running initialization code. Object destruction generally consists offinalization(a.k.a. cleanup) andmemory deallocation(a.k.a. free). These steps generally proceed in order as: allocate, initialize, finalize, deallocate.
Based on programming context, these steps may be partially or fully automated, and some of the steps can be customized.
The frequency of customization tends to vary by step and programming context. Initialization is the most commonly customized step. Finalization is common in languages with deterministic destruction, notably C++, but rare in garbage-collected languages. Allocation is rarely customized, and deallocation generally cannot be customized.
Creation
[edit]
The way to create objects varies across programming contexts. In some class-based languages, aconstructorhandles initialization. Like opther methods, a constructor can beoverloadedin order to support creating with different initial state.
Destruction
[edit]
Generally, an object is removed from memory after it is no longer needed. However, if there is sufficient memory or a program has a short run time, object destruction may not occur; memory simply being deallocated at process termination.
In some cases, object destruction consists solely of deallocating memory, particularly with garbage-collection, or if the object is aplain old data structure.In other cases, cleanup is performed prior to deallocation, particularly destroying member objects (in manual memory management), or deleting references from the object to other objects to decrement reference counts (in reference counting). This may be automatic, or a special destruction method may be called on the object.
In class-based languages with deterministic object lifetime, notably C++, adestructoris called when an instance is deleted, before the memory is deallocated. In C++, destructors differ from constructors in various ways. They cannot be overloaded, must have no arguments, need not maintainclass invariants,and can cause program termination if they throw exceptions.
Withgarbage collection,objects may be destroyed when they can no longer be accessed by the program. The garbage-collector calls afinalizerbefore memory deallocation.
Destroying an object will cause any references to the object to become invalid. With manual memory management, any existing reference becomes adangling reference.With garbage collection, objects are only destroyed when there are no references to them.
Consistency
[edit]Object lifetime begins when allocation completes and ends when deallocation starts. Thus, during initialization and finalization, an object is alive, but may not be in a consistent state. The period between when initialization completes to when finalization starts is when the object is both alive and in a consistent state.
If creation or destruction fail, error reporting (for example, raising an exception) can be complicated since the object or related objects may be in an inconsistent state.
For astatic variable,with lifespan coinciding with the program run, a creation or destruction error is problematic since program execution is before or after normal execution.
Class-based programming
[edit]In class-based programming, object creation is also known asinstantiation(creating aninstanceof aclass). Creation and destruction can be customized via aconstructorand adestructorand sometimes with separateinitializerandfinalizermethods.
Notably, a constructor is a class method as there is no object (instance) available until the object is created, but destructors, initializers, and finalizers are instance methods.
Further, constructors and initializers often can accept arguments, while destructors and finalizers generally do not as they are often implicitly callable.
Resource management
[edit]In languages with deterministic lifetime objects, lifetime may be used to piggybackresource management.This is called theResource Acquisition Is Initialization(RAII) idiom. Resources are acquired during initialization, and released during finalization. In languages with non-deterministic lifetime objects (i.e. garbage collected), the management of memory is generally kept separate from management of other resources.
Examples
[edit]C++
[edit]A C++ class can be declared with defaults as:
classFoo{};
When declared in an automatic context, the object is destroyed at the close of the block it is declared.
intbar(){
Foofoo();
// foo is destroyed here
}
When created dynamically, it lives until it is explicitly destroyed.
intbar(){
Foo*pFoo=newFoo();
deletepFoo;
}
Java
[edit]A Java class can be declared with defaults as:
classFoo{}
After an instance is created (i.e.new Foo()
) it lives until it has no references and the garbage collector deletes it.