词条 | Garbage (computer science) |
释义 |
In computer science, garbage includes objects, data, or other regions of the memory of a computer system (or other system resources), which will not be used in any future computation by the system, or by a program running on it. As computer systems all have finite amounts of memory, it is frequently necessary to deallocate garbage and return it to the heap, or memory pool, so the underlying memory can be reused. {{Anchor|semantic garbage|syntactic garbage}}ClassificationGarbage is generally classified into two types: semantic garbage that is any object or data never accessed by a running program for any combination of program inputs, and syntactic garbage that refers to objects or data within a program's memory space but unreachable from the program's root set. Objects and/or data which are not garbage are said to be live. Casually stated, syntactic garbage is data that cannot be reached, while semantic garbage is data that will not be reached. More precisely, syntactic garbage is data that is unreachable due to the reference graph (there is no path to it), which can be determined by many algorithms, as discussed in tracing garbage collector and only requires analyzing the data, not the code. Semantic garbage is data that will not be accessed, either because it is unreachable (hence also syntactic garbage), or reachable but will not be accessed; this latter requires analysis of the code, and is in general an undecidable problem. Syntactic garbage is a (usually strict) subset of semantic garbage as it is entirely possible for an object to hold a reference to another object without the latter object being used. ExampleIn the following simple stack implementation in Java, elements popped from the stack become semantic garbage once there are no outside references to them:{{efn|Simplified from Effective Java Item 6 by omitting resizing and explicit exceptions.}} This is because there is still a reference to the object from If a later Automatic garbage collectionAn example of the automatic collection of syntactic garbage, by reference counting garbage collection, can be produced using the Python command-line interpreter: In this session, an object is created, its location in the memory is displayed, and the only reference to the object is then destroyed—there is no way to ever use the object again from this point on, as there are no references to it. This becomes apparent when we try to access the original reference: As it is impossible to refer to the object, it has become useless: the object is garbage. Since Python uses garbage collection, it automatically deallocates the memory that was used for the object so that it may be used again: Note that the Bar instance now resides at the memory location 0x54f30; at the same place as where our previous object, the Foo instance, was located. Since the Foo instance was destroyed, freeing up the memory used to contain it, the interpreter creates the Bar object at the same memory location as before, making good use of the available resources. EffectsGarbage consumes heap memory, and thus one wishes to collect it (to minimize memory use, and allow faster memory allocation and prevent out-of-memory errors by reducing heap fragmentation and memory use). However, collecting garbage takes time, and if done manually, requires coding overhead. Further, collecting garbage destroys objects and thus can cause calls to finalizers, executing potentially arbitrary code at an arbitrary point in the program's execution. Incorrect garbage collection (deallocating memory that is not garbage), primarily due to errors in manual garbage collection (rather than errors in garbage collectors), results in memory safety violations (often security holes) due to use of dangling pointers. Syntactic garbage can be collected automatically, and garbage collectors have been extensively studied and developed. Semantic garbage cannot be automatically collected in general, and thus cause memory leaks even in garbage-collected languages. Detecting and eliminating semantic garbage is typically done using a specialized debugging tool called a heap profiler, which allows one to see what objects are live and how they are reachable, enabling one to remove the unintended reference. Eliminating garbageThe problem of managing the deallocation of garbage is a well-known one in computer science. Several approaches are taken:
Notes{{notelist}}External links
2 : Computer data|Computer programming |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。