Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Resource management (computing)

From Wikipedia, the free encyclopedia
Techniques used by computers to manage components with limited availability

Incomputer programming,resource management refers to techniques for managingresources (components with limited availability).

Computer programs may manage their own resources[which?] by using features exposed byprogramming languages (Elder, Jackson & Liblit (2008) is a survey article contrasting different approaches), or may elect to manage them by a host – anoperating system orvirtual machine – or another program.

Host-based management is known asresource tracking, and consists of cleaning up resource leaks: terminating access to resources that have been acquired but not released after use. This is known asreclaiming resources, and is analogous togarbage collection for memory. On many systems, the operating system reclaims resources after the process makes theexitsystem call.

Controlling access

[edit]

The omission of releasing a resource when a program has finished using it is known as aresource leak, and is an issue in sequential computing. Multiple processes wish to access a limited resource can be an issue inconcurrent computing, and is known asresource contention.

Resource management seeks to control access in order to prevent both of these situations.

Resource leak

[edit]
Main article:resource leak

Formally, resource management (preventing resource leaks) consists of ensuring that a resource is released if and only if it is successfully acquired. This general problem can be abstracted as "before,body, andafter" code, which normally are executed in this order, with the condition that theafter code is called if and only if thebefore code successfully completes, regardless of whether thebody code executes successfully or not. This is also known asexecute around[1] or acode sandwich, and occurs in various other contexts,[2] such as a temporary change of program state, ortracing entry and exit into asubroutine. However, resource management is the most commonly cited application. Inaspect-oriented programming, such execute around logic is a form ofadvice.

In the terminology ofcontrol flow analysis, resource release mustpostdominate successful resource acquisition;[3] failure to ensure this is a bug, and a code path that violates this condition causes a resource leak. Resource leaks are often minor problems, generally not crashing the program, but instead causing some slowdown to the program or the overall system.[2] However, they may cause crashes – either the program itself or other programs – due toresource exhaustion: if the system runs out of resources, acquisition requests fail. This can present asecurity bug if an attack can cause resource exhaustion. Resource leaks may happen under regular program flow – such as simply forgetting to release a resource – or only in exceptional circumstances, such as when a resource is not released if there is an exception in another part of the program. Resource leaks are very frequently caused byearly exit from a subroutine, either by areturn statement, or an exception raised either by the subroutine itself, or a deeper subroutine that it calls. While resource release due to return statements can be handled by carefully releasing within the subroutine before the return, exceptions cannot be handled without some additional language facility that guarantees that release code is executed.

More subtly, successful resource acquisition mustdominate resource release, as otherwise the code will try to release a resource it has not acquired. The consequences of such an incorrect release range from being silently ignored to crashing the program or unpredictable behavior. These bugs generally manifest rarely, as they require resource allocation to first fail, which is generally an exceptional case. Further, the consequences may not be serious, as the program may already be crashing due to failure to acquire an essential resource. However, these can prevent recovery from the failure, or turn an orderly shutdown into a disorderly shutdown. This condition is generally ensured by first checking that the resource was successfully acquired before releasing it, either by having a boolean variable to record "successfully acquired" – which lacks atomicity if the resource is acquired but the flag variable fails to be updated, or conversely – or by the handle to the resource being anullable type, where "null" indicates "not successfully acquired", which ensures atomicity.

Resource contention

[edit]
Main article:resource contention

In computer science,resource contention refers to a conflict that arises when multiple entities attempt to access a shared resource, like random access memory, disk storage, cache memory, internal buses, or external network devices.

Memory management

[edit]
Main article:Memory management

Memory can be treated as a resource, butmemory management is usually considered separately, primarily because memory allocation and deallocation is significantly more frequent than acquisition and release of other resources, such as file handles. Memory managed by anexternal system has similarities to both (internal) memory management (since it is memory) and resource management (since it is managed by an external system). Examples include memory managed via native code and used from Java (viaJava Native Interface); and objects in theDocument Object Model (DOM), used fromJavaScript. In both these cases, thememory manager (garbage collector) of theruntime environment (virtual machine) is unable to manage the external memory (there is no shared memory management), and thus the external memory is treated as a resource, and managed analogously. However, cycles between systems (JavaScript referring to the DOM, referring back to JavaScript) can make management difficult or impossible.

Lexical management and explicit management

[edit]

A key distinction in resource management within a program is betweenlexical management andexplicit management – whether a resource can be handled as having a lexical scope, such as a stack variable (lifetime is restricted to a single lexical scope, being acquired on entry to or within a particular scope, and released when execution exits that scope), or whether a resource must be explicitly allocated and released, such as a resource acquired within a function and then returned from it, which must then be released outside of the acquiring function. Lexical management, when applicable, allows a better separation of concerns and is less error-prone.

Basic techniques

[edit]

The basic approach to resource management is to acquire a resource, do something with it, then release it, yielding code of the form (illustrated with opening a file inPython):

fromtypingimportTextIOf:TextIO=open(filename)...f.close()

This is correct if the intervening... code does not contain an early exit (return), the language does not have exceptions, andopen is guaranteed to succeed. However, it causes a resource leak if there is a return or exception, and causes an incorrect release of unacquired resource ifopen can fail.

There are two more fundamental problems: the acquisition-release pair is not adjacent (the release code must be written far from the acquisition code), and resource management is not encapsulated – the programmer must manually ensure that they are always paired. In combination, these mean that acquisition and release must be explicitly paired, but cannot be placed together, thus making it easy for these to not be paired correctly.

The resource leak can be resolved in languages that support afinally construction (like Python) by placing the body in atry clause, and the release in afinally clause:

fromtypingimportTextIOf:TextIO=open(filename)try:...finally:f.close()

This ensures correct release even if there is a return within the body or an exception thrown. Further, note that the acquisition occursbefore thetry clause, ensuring that thefinally clause is only executed if theopen code succeeds (without throwing an exception), assuming that "no exception" means "success" (as is the case foropen in Python). If resource acquisition can fail without throwing an exception, such as by returning a form ofnull, it must also be checked before release, such as:

fromtypingimportTextIOf:TextIO=open(filename)try:...finally:iff:f.close()

While this ensures correct resource management, it fails to provide adjacency or encapsulation. In many languages there are mechanisms that provide encapsulation, such as thewith statement in Python:

withopen(filename)asf:...

The above techniques – unwind protection (finally) and some form of encapsulation – are the most common approach to resource management, found in various forms inC#,Common Lisp,Java,Python,Ruby,Scheme, andSmalltalk,[1] among others; they date to the late 1970s in theNIL dialect of Lisp; seeException handling § History. There are many variations in the implementation, and there are also significantly differentapproaches.

Approaches

[edit]

Unwind protection

[edit]

The most common approach to resource management across languages is to use unwind protection, which is called when execution exits a scope – by execution running off the end of the block, returning from within the block, or an exception being thrown. This works for stack-managed resources, and is implemented in many languages, including C#, Common Lisp, Java, Python, Ruby, and Scheme. The main problems with this approach is that the release code (most commonly in afinally clause) may be very distant from the acquisition code (it lacksadjacency), and that the acquisition and release code must always be paired by the caller (it lacksencapsulation). These can be remedied either functionally, by using closures/callbacks/coroutines (Common Lisp, Ruby, Scheme), or by using an object that handles both the acquisition and release, and adding a language construct to call these methods when control enters and exits a scope (C#using, Javatry-with-resources, Pythonwith); see below.

An alternative, more imperative approach, is to write asynchronous code indirect style: acquire a resource, and then in the next line have adeferred release, which is called when the scope is exited – synchronous acquisition followed by asynchronous release. This originated in C++ as the ScopeGuard class, byAndrei Alexandrescu and Petru Marginean in 2000,[4] with improvements by Joshua Lehrer,[5] and has direct language support in D via thescope keyword (ScopeGuardStatement), where it is one approach toexception safety, in addition to RAII (see below).[6] It has also been included in Go, as thedefer statement.[7] This approach lacks encapsulation – one must explicitly match acquisition and release – but avoids having to create an object for each resource (code-wise, avoid writing a class for each type of resource).

Object-oriented programming

[edit]

Inobject-oriented programming, resources are encapsulated within objects that use them, such as afile object having afield whose value is afile descriptor (or more generalfile handle). This allows the object to use and manage the resource without users of the object needing to do so. However, there is a wide variety of ways that objects and resources can be related.

Firstly, there is the question of ownership: does an objecthave a resource?

  • Objects canown resources (viaobject composition, a strong "has a" relationship).
  • Objects canview resources (viaobject aggregation, a weak "has a" relationship).
  • Objects cancommunicate with other objects that have resources (viaAssociation).

Objects that have a resource can acquire and release it in different ways, at different points during theobject lifetime; these occur in pairs, but in practice they are often not used symmetrically (see below):

  • Acquire/release while the object is valid, via (instance) methods such asopen ordispose.
  • Acquire/release during object creation/destruction (in the initializer and finalizer).
  • Neither acquire nor release the resource, instead simply having aview orreference to a resource managed externally to the object, as independency injection; concretely, an object that has a resource (or can communicate with one that does) is passed in as an argument to a method or constructor.

Most common is to acquire a resource during object creation, and then explicitly release it via an instance method, commonly calleddispose. This is analogous to traditional file management (acquire duringopen, release by explicitclose), and is known as thedispose pattern. This is the basic approach used in several major modern object-oriented languages, includingJava,C# andPython, and these languages have additional constructs to automate resource management. However, even in these languages, more complex object relationships result in more complex resource management, as discussed below.

RAII

[edit]
Main article:Resource Acquisition Is Initialization

A natural approach is to make holding a resource be aclass invariant: resources are acquired during object creation (specifically initialization), and released during object destruction (specifically finalization). This is known asResource Acquisition Is Initialization (RAII), and ties resource management toobject lifetime, ensuring that live objects have all necessary resources. Other approaches do not make holding the resource a class invariant, and thus objects may not have necessary resources (because they've not been acquired yet, have already been released, or are being managed externally), resulting in errors such as trying to read from a closed file. This approach ties resource management to memory management (specifically object management), so if there are no memory leaks (no object leaks), there are noresource leaks. RAII works naturally for heap-managed resources, not only stack-managed resources, and is composable: resources held by objects in arbitrarily complicated relationships (a complicatedobject graph) are released transparently simply by object destruction (so long as this is done properly!).

RAII is the standard resource management approach in C++, but is little-used outside C++, despite its appeal, because it works poorly with modern automatic memory management, specificallytracing garbage collection: RAIIties resource management to memory management, but these have significant differences. Firstly, because resources are expensive, it is desirable to release them promptly, so objects holding resources should be destroyed as soon as they become garbage (are no longer in use). Object destruction is prompt in deterministic memory management, such as in C++ (stack-allocated objects are destroyed on stack unwind, heap-allocated objects are destroyed manually via callingdelete or automatically usingunique_ptr) or in deterministic reference-counting (where objects are destroyed immediately when their reference count falls to 0), and thus RAII works well in these situations. However, most modern automatic memory management is non-deterministic, making no guarantees that objects will be destroyed promptly or even at all! This is because it is cheaper to leave some garbage allocated than to precisely collect each object immediately on its becoming garbage. Secondly, releasing resources during object destruction means that an object must have afinalizer (in deterministic memory management known as adestructor) – the object cannot simply be deallocated – which significantly complicates and slows garbage collection.

Complex relationships

[edit]

When multiple objects rely on a single resource, resource management can be complicated.

A fundamental question is whether a "has a" relationship is one ofowning another object (object composition), orviewing another object (object aggregation). A common case is when one two objects are chained, as inpipe and filter pattern, thedelegation pattern, thedecorator pattern, or theadapter pattern. If the second object (which is not used directly) holds a resource, is the first object (which is used directly) responsible for managing the resource? This is generally answered identically to whether the first objectowns the second object: if so, then the owning object is also responsible for resource management ("having a resource" istransitive), while if not, then it is not. Further, a single object may "have" several other objects, owning some and viewing others.

Both cases are commonly found, and conventions differ. Having objects that use resources indirectly be responsible for the resource (composition) providesencapsulation (one only needs the object that clients use, without separate objects for the resources), but results in considerable complexity, particularly when a resource is shared by multiple objects or objects have complex relationships. If only the object that directly uses the resource is responsible for the resource (aggregation), relationships between other objects that use the resources can be ignored, but there is no encapsulation (beyond the directly using object): the resource must be managed directly, and might not be available to the indirectly using object (if it has been released separately).

Implementation-wise, in object composition, if using the dispose pattern, the owning object thus will also have adispose method, which in turn calls thedispose methods of owned objects that must be disposed; in RAII this is handled automatically (so long as owned objects are themselves automatically destroyed: in C++ if they are a value or aunique_ptr, but not a raw pointer: seepointer ownership). In object aggregation, nothing needs to be done by the viewing object, as it is not responsible for the resource.

Both are commonly found. For example, in theJava Class Library,Reader#close() closes the underlying stream, and these can be chained. For example, aBufferedReader may contain aInputStreamReader, which in turn contains aFileInputStream, and callingclose on theBufferedReader in turn closes theInputStreamReader, which in turn closes theFileInputStream, which in turn releases the system file resource. Indeed, the object that directly uses the resource can even be anonymous, thanks to encapsulation:

try(BufferedReaderreader=newBufferedReader(newInputStreamReader(newFileInputStream(fileName)))){// Use reader.}// reader is closed when the try-with-resources block is exited, which closes each of the contained objects in sequence.

However, it is also possible to manage only the object that directly uses the resource, and not use resource management on wrapper objects:

try(FileInputStreamstream=newFileInputStream(fileName)))){BufferedReaderreader=newBufferedReader(newInputStreamReader(stream));// Use reader.}// stream is closed when the try-with-resources block is exited.// reader is no longer usable after stream is closed, but so long as it does not escape the block, this is not a problem.

By contrast, in Python, acsv.reader does not own thefile that it is reading, so there is no need (and it is not possible) to close the reader, and instead thefile itself must be closed.[8]

importcsvfromtypingimportIteratorwithopen(filename)asf:r:Iterator[list[str]]=csv.reader(f)# Use r.# f is closed when the with-statement is exited, and can no longer be used.# Nothing is done to r, but the underlying f is closed, so r cannot be used either.

In.NET, convention is to only have direct user of resources be responsible: "You should implement IDisposable only if your type uses unmanaged resources directly."[9]

In case of a more complicatedobject graph, such as multiple objects sharing a resource, or cycles between objects that hold resources, proper resource management can be quite complicated, and exactly the same issues arise as in object finalization (via destructors or finalizers); for example, thelapsed listener problem can occur and cause resource leaks if using theobserver pattern (and observers hold resources). Various mechanisms exist to allow greater control of resource management. For example, in theGoogle Closure Library, thegoog.Disposable class provides aregisterDisposable method to register other objects to be disposed with this object, together with various lower-level instance and class methods to manage disposal.

Structured programming

[edit]

Instructured programming, stack resource management is done simply by nesting code sufficiently to handle all cases. This requires only a single return at the end of the code, and can result in heavily nested code if many resources must be acquired, which is considered ananti-pattern by some – theArrow Anti Pattern,[10] due to the triangular shape from the successive nesting.

Cleanup clause

[edit]

One other approach, which allows early return but consolidates cleanup in one place, is to have a single exit return of a function, preceded by cleanup code, and to usegoto to jump to the cleanup before exit. This is infrequently seen in modern code, but occurs in some uses of C.

See also

[edit]

References

[edit]
  1. ^abBeck 1997, pp. 37–39.
  2. ^abElder, Jackson & Liblit 2008, p. 3.
  3. ^Elder, Jackson & Liblit 2008, p. 2.
  4. ^"Generic: Change the Way You Write Exception-Safe Code — Forever", byAndrei Alexandrescu and Petru Marginean, December 01, 2000,Dr. Dobb's
  5. ^ScopeGuard 2.0, Joshua Lehrer
  6. ^D:Exception Safety
  7. ^Defer, Panic, and Recover, Andrew Gerrand,The Go Blog, 4 August 2010
  8. ^Python: No csv.close()?
  9. ^"IDisposable Interface". Retrieved2016-04-03.
  10. ^Flattening Arrow Code, Jeff Atwood, 10 Jan 2006

Further reading

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Resource_management_(computing)&oldid=1322087288"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp