Garbage collection is a memory management activity carried out by a system to reclaim dynamically allocated memory that is no longer being used. Garbage collection may be triggered in the system when free memory falls below a certain level, or after a certain number of memory allocations.
Garbage collection can relieve programmers from the burden of freeing allocated memory. For example, in certain programming languages, a heap may store objects created by an executing program. The objects created by the executing program may be allocated on the heap at run time. Garbage collection is the process of automatically freeing objects that are no longer referenced by the program. Periodic garbage collection frees the programmer from having to keep track of the need to free allocated memory.
When an object is no longer referenced by the program, the heap space occupied by the object may be recycled so that the heap space is available for new objects. A garbage collector may determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects.
In addition to freeing unreferenced objects, a garbage collector may also reduce heap fragmentation that may occur during the course of program execution. If new objects are allocated, and unreferenced objects are freed, then free blocks of heap memory may be left in between blocks occupied by live objects. Requests to allocate new objects may have to be filled by extending the size of the heap even though there is enough total unused space in an existing fragmented heap. This may occur if there is not enough contiguous free heap space available into which the new object will fit. Therefore, a garbage collector may also compact the memory that is in use in the heap to reduce the working space needed for the heap.
Referring now to the drawings in which like reference numbers represent corresponding parts throughout:
a illustrates pseudo-code corresponding to a mark/scan based tracing process, in accordance with certain embodiments;
b illustrates pseudo-code corresponding to a compacting process, in accordance with certain embodiments;
In the following description, reference is made to the accompanying drawings which form a part hereof and which illustrate several embodiments. It is understood that other embodiments may be utilized and structural and operational changes may be made.
Garbage collection may be a component of memory management in managed runtimes, such as, the Java* Runtime Environment (JRE) and the Common Language Runtime (CLR). The performance metrics for garbage collection on a computational device may include throughput and responsiveness. Throughput may depend on cumulative garbage collection pause time, while responsiveness may be determined by average and worst case pause times. Garbage collection may include a plurality of sequential or interleaving phases, such as, tracing, compacting, and sweeping, where the plurality of phases, may contribute to the pause time.
Java is a trademark or registered trademark of Sun Microsystems.
Certain embodiments, provide garbage collection and compaction for reducing the pause time of mark and sweep algorithms by using reversed references records and an address mapping table. In certain embodiments, the time complexity is proportional to the size of living objects instead of the total heap size. Certain embodiments trade space for compaction time, while at the same time controlling the amount of space overhead.
The computing platform 102 may comprise any suitable computational device including those presently known in the art, such as, a personal computer, a workstation, a server, a mainframe, a hand held computer, a palm top computer, a laptop computer, a telephony device, a network computer, a blade computer, etc. Code stored in a computer readable medium may be loaded into the memory 106 and be executed by the processor 104.
The managed runtime environment 108 may comprise a set of executables that allow a program to execute in the computing platform 102. In certain embodiments, the managed runtime environment 108 may include a JRE or a CLR. The managed runtime environment 108 may include a heap 110, a garbage collector 112 and associated data structures 114. The heap 110 comprises dynamic memory allocated by programs that execute in the computing platform 102. The garbage collector 112 frees unused memory from the heap 110 by using and updating information included in the data structures 114. The garbage collector 112 may also compact the heap 110 to reduce fragmentation of memory in the heap 110.
The data structures 114 may include a plurality of reversed reference records (RRR) 200a, 200b, . . . , 200n and an address mapping table (AMT) 202. In certain embodiments, the RRRs 200a . . . 200n and the AMT 202 may be generated and updated by the garbage collector 112.
Reversed references of an object are the addresses of the locations containing legitimate, i.e., valid, references pointing to the object. An RRR, such as RRR 200a, corresponding to an object may store all references to the object.
In certain embodiments, when the RRRs 200a . . . 200n for survived objects, i.e., objects that are still being used and that cannot be freed, have been constructed by the garbage collector 112, the compacting phase of garbage collection starts and survived objects are compacted and pushed to one end of the heap 110. The contents of the RRRs 200a . . . 200n may be updated with new addresses after the heap 110 has been compacted. The efficiency of updating references in the RRRs 200a . . . 200n may depend on the AMT 202. The AMT 202 stores pairs of <old_address, new_address> mappings in such a manner, such that, given an old address, the new address can be recovered. Therefore the AMT 202 facilitates the process of recovering stale reversed references caused by the movement of objects in the heap 110. In the meantime, AMT maintains the old addresses of the living objects in a sorted manner so as to provide the objects without changing the order in which the living objects were allocated.
A snapshot of the heap 110 can be represented by the notation (V, A), where V denotes a collection of nodes 303, 304, 305, 306, 307, 308, 309 residing in the heap 110 with roots 301, 302 (annotated by shading), and A denotes the collection of arcs that correspond to inter-object references or references from the roots 301, 302. The nodes 302, 303, 304, 305, 306, 307, 308, 309 and roots 301, 302 represent objects in the heap 110.
The exemplary directed graph 300 depicts a snapshot of the heap 110 before the garbage collector 112 reclaims dead objects, i.e. objects that are no longer in use. In the exemplary directed graph 300, root1301 and root2302 are roots of live objects discovered during root set enumeration, and nodes 303, 304, 305, 306, 307, 308, 309 represent heap objects. The arrowed arcs denote references. For example, the arrowed arc 311 indicates that root1301 references the object obj3303.
In the exemplary heap 400 the addresses increase from left to right. The exemplary heap 400 also shows the locations of the objects 303, 304, 305, 306, 307, 308, 309 of the directed graph 300. In the exemplary heap 400, the object with the lowest address is obj6306 and the object with the highest address is obj10310.
In certain embodiments, a tracing procedure for determining live nodes, i.e., nodes that are in use, starts from the roots 301, 302 and traverses through the references to all living objects reachable from the roots 301, 302. As can be seen from the directed graph 300 corresponding to the exemplary heap 400, the tracing procedure first reaches obj3303 and obj4304 from root1301 and root2302, and then reaches obj5305 and obj8308 via obj4304. All other objects, namely obj6306, obj7307, obj9309 and obj10310, are never traced, and are garbage.
Therefore,
After objects have been deleted from a heap 110, the heap 110 may need to be compacted to remove fragmentation in the heap. In certain embodiments, the compaction task may be decomposed into four subtasks:
Among the four subtasks, subtask 4 may dominate the total pause time. Certain compaction algorithms may perform one or more heap walks to deal with the reference updates. Certain embodiments may use semi-space and forwarding-addresses to trade size for time complexity in garbage compaction.
As shown earlier in
In the exemplary heap 400 of
The construction of the RRRs 200a . . . 200n is based on the snapshot of the heap 110 before the compacting phase begins. The subsequent moving of survived objects may change the heap 110 and in turn invalidate the reversed references. For example, in
RRR(obj4)={<obj3's address, &ptr1's offset>, &ptr6/root2}.
In certain embodiments, for the ease of tracking and recovering stale reversed reference, one extra word may be used for each reversed reference except for those from roots.
When all RRRs 200a . . . 200n for survived objects have been constructed, the compacting phase starts and survived objects are compacted and pushed to one end of the heap. The content of each RRR may be updated with the referred object's new address. The efficiency of updating references relies on the AMT 202. The AMT 202 stores pairs of <old_address, new_address> mappings in such a manner that, given an old address, certain embodiments can retrieve the new address efficiently. Therefore the AMT 202 facilitates the process of recovering stale reversed references caused by movements of objects. In the meantime, the AMT 202 maintains old addresses of the living objects in a sorted manner so as to provide the objects without changing the order in which the objects were allocated.
Certain embodiments, may perform garbage collection as follows:
a illustrates pseudo-code corresponding to a mark/scan based tracing process, and
In certain embodiments corresponding to the exemplary heap 400 shown in
In the compacting stage shown in
After moving obj3, obj4 is the chosen candidate in the AMT. As soon as obj4 is moved to a new position (reference numeral 800), all the reversed references of obj4 are redirected to the new address. Note that the update of <obj3's addr, ptr1's offset> 610 is shown in
Control starts at block 1000 where objects are allocated in a heap 110. The garbage collector 112 starts (at block 1002) the tracing phase of garbage collection in which the garbage collector 112 may initiate the process of marking and scanning of objects represented as nodes of a directed graph to delete unreferenced objects.
Control proceeds to block 1004, where the garbage collector 112 determines reversed references for the allocated objects. The garbage collector stores (at block 1006) the reversed references in reverse reference records 200a . . . 200n by using relative addresses for the reversed references.
The garbage collector 112 maintains (at block 1008) an address mapping table 202 that can store status of old and new addresses of objects when objects are moved from old to new addresses in the heap 110. The garbage collector 112 deletes (at block 1010) unreferenced objects from the heap 110, where unreferenced objects are objects that cannot be reached from roots, such as, roots 301, 302, and ends the tracing phase of garbage collection.
At the conclusion of block 1010, the heap 110 may be fragmented. The garbage collector 112 starts (at block 1012) the compaction phase of garbage collection. The garbage collector compacts (at block 1014) the heap 110 by adjustments to the reverse references. The compaction phase ends and control returns to block 1000, where new objects are allocated to the compacted heap.
Certain embodiments may spend processor time in maintaining and searching the AMT 202. In certain embodiments, the AMT 202 may be implemented as a sorted array, hash table, or tree-based data structures to reduce the processing time requirements. Certain embodiments that utilize hash table may provide a balance between performance and the complexity of the embodiment.
Certain embodiments maintain the allocation order of the survivor objects. Behavior of a cache and/or translation lookaside buffer (TLB) may be improved by maintaining the allocation order.
As far as space is concerned, certain embodiments need two additional spaces for the RRRs 200a . . . 200n and AMT 202 respectively. In certain embodiments the size of the AMT 202 is proportional to the number of living objects (number of nodes in a directed-graph model), while the space of RRR is proportional to the number of references (number of edges in a directed-graph model). The space of RRR may dominate the total space overhead in certain embodiments. Therefore, the space requirement of certain embodiments is controlled and in the worst cases, the embodiments can still deliver improved memory utilization.
In an exemplary worst case scenario, the heap 110 is completely filled with small objects that consist of a header and one reference field; all these objects survive the scavenging, and constitute a circular linked list, e.g., the reference field of each object points to the next object except that the reference field of the last object points to the first object. For a runtime system adopting certain copying garbage collectors, the object header is usually of 3 words to accommodate information such as virtual method dispatching, lock and forwarding address. For a heap of S words, there're totally N=S/(3+1) living objects. According to certain illustrated embodiments, AMT and RRR may budget 2*N and 2*N respectively, which is roughly the same size as the heap. At that point, only half of the consumed memory is available for living objects, which is the same as the semi-space copying garbage collection. From a quantitative point of view, garbage collection phase may introduce a burst space demand which leads to a space utilization no worse than 50%, which is favorable than the consistent utilization of 50% for the copying garbage collection. Moreover, certain embodiment have a mostly stable space utilization of 100% at the runtime.
Certain embodiments, theoretically demand higher space requirement than some conventional compaction algorithms. Nevertheless due to the low survival rate and reference density in practice, the extra space overhead is usually affordable for most runtime systems.
Certain embodiments provide a garbage collection and compaction algorithm where the time complexity proportional to the size of living objects instead of the total heap size. The space overhead is controlled in certain embodiments.
The described techniques may be implemented as a method, apparatus or article of manufacture involving software, firmware, micro-code, hardware and/or any combination thereof. The term “article of manufacture” as used herein refers to program instructions, code and/or logic implemented in circuitry [e.g., an integrated circuit chip, Programmable Gate Array (PGA), Application Specific Integrated Circuit (ASIC), etc.] and/or a computer readable medium (e.g., magnetic storage medium, such as hard disk drive, floppy disk, tape), optical storage (e.g., CD-ROM, DVD-ROM, optical disk, etc.), volatile and non-volatile memory device (e.g., Electrically Erasable Programmable Read Only Memory (EEPROM), Read Only Memory (ROM), Programmable Read Only Memory (PROM), Random Access Memory (RAM), Dynamic Random Access Memory (DRAM), Static Random Access Memory (SRAM), flash, firmware, programmable logic, etc.). Code in the computer readable medium may be accessed and executed by a machine, such as, a processor. In certain embodiments, the code in which embodiments are made may further be accessible through a transmission medium or from a file server via a network. In such cases, the article of manufacture in which the code is implemented may comprise a transmission medium, such as a network transmission line, wireless transmission media, signals propagating through space, radio waves, infrared signals, etc. Of course, those skilled in the art will recognize that many modifications may be made without departing from the scope of the embodiments, and that the article of manufacture may comprise any information bearing medium known in the art. For example, the article of manufacture comprises a storage medium having stored therein instructions that when executed by a machine results in operations being performed. Furthermore, program logic that includes code may be implemented in hardware, software, firmware or many combination thereof. The described operations associated with
Certain embodiments illustrated in
In certain embodiments, the storage device 1214 may be absent in the system 1200. Instead of the storage device 1214, in alternative embodiments the system 1200 may include another device, such as, a video controller or graphics controller device that renders information to display on a monitor coupled to the system 1200, where the system 1200 may comprise a desktop, workstation, server, mainframe, laptop, handheld computer, etc. An operating system may be capable of execution by the system, and the video controller may render graphics output via interactions with the operating system. Alternatively, some embodiments may be also be implemented in a computer system that does not include a video or graphics controller but includes a switch, router, etc.
At least certain of the operations associated with
The data structures and components shown or referred to in