Determining When Objects Die to Improve Garbage Collection
Total Page:16
File Type:pdf, Size:1020Kb
Determining When Objects Die to Improve Garbage Collection A dissertation submitted by Nathan P. Ricci In partial fulfillment of the requirements for the degree of Doctor of Philosophy in Computer Science TUFTS UNIVERSITY May 2016 ADVISER: Samuel Z. Guyer ii 0.1 Abstract Although garbage collection frees programmers from the burden of manually free- ing objects that will no longer be used, and eliminates many common programming errors, it can come with a significant performance penalty. This penalty can become particularly great when memory is constrained. This thesis introduces two pieces of work to address this problem. The first is Elephant Tracks, a tool that uses novel techniques to measure memory properties of Java programs Elephant Tracks is novel in that it places object death events more accurately than any existing tool, and is able to do so with out modification to the underlying VM. The second is the Deferred Garbage collector. Built based on observations from the Elephant Tracks traces, the Deferred Garbage Collector reduces redundant work performed by the garbage collector In this thesis, we show that the techniques used by the Deferred Collector can reduce garbage collector tracing workload in some programs. iii 0.2 Acknowledgements I would like to thank the Tufts University Department of Computer Science for building the environment that makes this work possible. I am especially grateful for the patient mentoring of my advisor, Sam Guyer. I also wish to express gratitude to my comitee members; Norman Ramsey, Alva Couch, Mark Hempstead, and Tony Printezis. Additionally, I would like to thank my wife Gizem, and my parents Susan, and David, without whose support I never would have made it here. I would also be remiss if I did not thank the Tufts Department of Athletics, for providing barbells, without which I would have long ago gone mad. Lastly, I must also thank Tufts University and the National Science Foundation for their financial support. iv Contents 0.1 Abstract . iii 0.2 Acknowledgements . iv 1 Introduction 1 2 Garbage Collection Background 3 2.1 On Object Lifetime . .3 2.2 Garbage Collector Overhead . .4 2.2.1 Mark Sweep . .6 2.2.2 Generational Mark Sweep . .7 2.3 Weak References . .8 2.4 The Java Virtual Machine Tool Interface . .9 3 Elephant Tracks 11 3.1 Elephant Tracks Introduction . 11 3.2 Trace time . 13 3.3 Background and related work . 15 3.3.1 Garbage collection tracing . 16 3.3.2 Why a new trace generator? . 19 3.3.3 Related work . 21 3.4 Elephant Tracks Design . 23 3.4.1 Kinds of trace records . 23 v 3.4.2 Execution model . 25 3.5 Implementation . 29 3.5.1 Timestamping strategy . 30 3.5.2 The instrumenter . 31 3.5.3 The agent . 36 3.5.4 Properties of our implementation approach . 39 3.6 Results . 40 3.6.1 Performance . 40 3.6.2 Trace analysis . 42 3.7 Conclusions . 43 4 Deferred Collector 49 4.1 Finding Key Objects . 50 4.2 Deferred Collector Design . 54 4.2.1 Defer all Object Reachable from the Key . 54 4.2.2 Large Object Space . 56 4.3 Mitigating Bad Hints . 57 4.3.1 Application Programming Interface . 58 4.4 JikesRVM . 58 4.4.1 Sanity Checker . 60 4.5 Experimental Results for The Deferred Collector . 61 4.5.1 Methodology . 61 4.5.2 Doubly Linked List . 62 4.5.3 sunflow . 63 4.6 Related Work . 66 5 Conclusion 68 vi Bibliography 68 vii List of Figures 2.1 The events in the life of an object. First it is allocated, then it is used. Eventually, it is used for the last time. Sometime after this, it may become unreachable. Finally its memory will be reclaimed. .4 2.3 A weak reference object has a field called a ”referent” that refers to some object (A). When the object referred to by the referent ob- ject is reachable only through weak reference objects, it may be collected. If this happens, the referent field is nulled (B). .9 3.1 Pseudo code for the merlin algorithm. 18 4.1 The live size of each of the dacapo bench benchmaks with respect to time. The blue line shows the number of objects live over time. The lower green dashed line shows the number of live objects that fall into clusters as described in 4.1 . 51 4.1 The live size of each of the dacapo bench benchmaks with respect to time. The blue line shows the number of objects live over time. The lower green dashed line shows the number of live objects that fall into clusters and described in 4.1 . 52 viii 4.1 The live size of each of the dacapo bench benchmaks with respect to time. The blue line shows the number of objects live over time. The lower green dashed line shows the number of live objects that fall into clusters and described in 4.1 . 53 4.2 mark/cons ratio for the doubly linked list program, run with a 16 MB heap, and differing values of immediate collection period. 63 4.3 mark/cons ratio for sunflow, run with a 27 MB heap, and differing values of immediate collection period. 65 4.4 mark/cons ratio for sunflow, run with a 32 MB heap, and differing values of immediate collection period. 66 ix Chapter 1 Introduction Since the task of allocating and freeing memory in computer programs is tedious and error prone, automated memory management (known as garbage collection) has been a boon to computer programmers. However, automated memory manage- ment comes with costs; extra memory overhead or time. The hypothesis of this thesis is that a greater understanding of the structure of a program’s heap can improve the performance of garbage collectors. Intuitively, this seems obvious; the data structures used in a program govern in large part how the memory of a program is used, and understanding those data structures should give us more information about the lifetime of objects. However, in order to examine this intuitive idea, we will need to develop new techniques to observe how programs use memory. In particular, we want to know precisely when objects become unreachable and may be collected. Thus, this thesis is two parts. In the first half, we present Elephant Tracks, a tool which uses novel techniques to trace the execution of Java programs. The traces produced by Ele- phant Tracks contain a record of all object allocation and death events, and enough information to place these events in a specific calling context (a novel feature of Ele- phant Tracks). These traces can be used to analyze runtime properties of programs, 1 including prototyping of new garbage collection algorithms. Since they place death so precisely, it is easy test schemes that require less precision by ignoring some of the information in the trace. A central claim of this thesis is that that this can be done with out relying on any modification to the underlying VM. In the second half of this thesis, we present the deferred collector, built based on data gather with Elephant Tracks. The deferred collector exploits the observation that much of the heap does not change between two successive runs of a tracing- style garbage collector, and as a result there is much work repeated between such successive runs. The deferred collector reduces this repeated work based on pro- grammer hints. We claim that this can reduce garbage collector tracing workload in some programs. 2 Chapter 2 Garbage Collection Background In order to understand the performance characteristics of garbage collectors, it is necessary to understand how they work. Thus, we will describe their operation in this section. As there is copious work on garbage collection, this chapter will only focus on details relevant to this thesis. 2.1 On Object Lifetime First, we must make a brief aside to explain the events of interest in an objects life- time. We will borrow terminology first used by Rojemo¨ and Runciman (1996). A program allocates an object, and sometime later it is first used; the period between these events is called the lag. Eventually, the program uses an object for the last time, and this point we can say the object is dead. In principle, it would be safe for the garbage collector to reclaim an objects memory the very instant of its death. However, in general it is not possible to determine whether a use is the last use. So, instead the first point the collector could collect an object is later, when it be- comes unreachable. The period between last use and the time an object becomes unreachable is called use drag. 3 Lifetime of An Object Figure 2.1: The events in the life of an object. First it is allocated, then it is used. Eventually, it is used for the last time. Sometime after this, it may become unreach- able. Finally its memory will be reclaimed. Finally, although the garbage collector could collect an object as soon as the object becomes unreachable, in practice the collector runs only intermittently; the collector will not collect the object until its next run. This engenders another period of drag, the reachability drag. 2.2 Garbage Collector Overhead How much overhead does garbage collection entail? While there have been many significant advances in garbage collection performance, overhead can still be very high when memory is constrained. To understand why, first consider how most typical garbage collection algorithms work: As the program requests new objects, 4 (a) Heap, memory usage below threshold (b) Heap full (c) Heap after collection (d) Key: Blue objects are allocated and reach- able.