igc Garbage Collection

Size: px
Start display at page:

Download "igc Garbage Collection"

Transcription

1 igc Garbage Collection User s Manual Harnix Technologies Inc

2 - 2 -

3 Contents Overview... 6 Basic Concepts... 6 Architecture... 7 How the Garbage Collector Works... 7 Enabling Garbage Collection... 8 Features & Specs... 9 Performance Testing GCBench testing Pause-less testing GC Throughputs FAQs Programming Guide Object References Managed reference Strong reference Weak reference Zeroing weak reference Summary ARCGC_C & Reference Mapping Object Reclamation The cleanup routine Immediate reclamation Reclamation ordering Resurrection Dynamic associated destructor Reclaim circular depended cycles Linear Pool Features & tech specs Create a linear pool Create an object from a pool Drop references to pool or objects Evacuate objects from a pool API References Predefined Macros OBJC_GC has_feature(objc_arc) has_feature(objc_arc_gc) has_feature(objc_arc_gc_compatible) has_feature(objc_hnxgc) has_feature(objc_hnx) Objective-C Methods

4 destruct Declaration: Example: createfrompool Declaration: Example: weak autoweak C Functions _hnxgc_collect Declaration: _hnxgc_destruct_object Declaration: _hnxgc_add_memory_pressure Declaration: Example: _hnxgc_did_receive_memory_warning Declaration: Example: _hnxgc_set_trace_level Declaration: Example: _hnxgc_set_cycle_breaker Declaration: _hnxgc_set_nonrc Declaration: _hnxgc_weakref_allocate Declaration: _hnxgc_weakref_deallocate Declaration: _hnxgc_weakref_read Declaration: _hnxgc_weakref_write Declaration: <General C API> C++ API gcptr<t>::i Declaration: Example: gcptr<t> Declaration: gcnew_i Declaration:

5 Example: gcdelete Example: Example 2: <General C++ API> Articles Maintaining existing RC cleanup ordering The problem Discarded attempts igc solution Solving stack overflow due to deep-nested destructions Choosing new style coding Reducing your code writing workload Making more cycles non-sensitive to cleanup ordering Writing one code for both GC and RC Conclusion Advanced Subtle Topics Access after destruction Reference counting Aggressive garbage collection C++ Exceptions Where GC runs

6 Overview igc is an innovative runtime garbage collector designed for the ios (iphoneos) platform to collect unused objects, including those circular-referenced, which cannot be reclaimed by Apple's Automatic Reference Counting (ARC) or manual retain/release (MRR) code. Unlike some other garbage collectors, e.g. the conservative GC in Mac OS X, igc is fully compatible with reference counting. There is no need to work with two separate system libraries and frameworks for non-gc and GC apps. Just by linking in your code with the igc static library, you can use Objective-C and C++ language to write your auto-managed ios App with existing system libraries and frameworks from Apple and other 3rd parties. Only your code knows GC was enabled, while others continue to see your code running as manually managed. Basically, igc can be viewed as a combination of reference counting and tracing garbage collection, with the strengths of each merged in a unique and innovative way that retains the merits and reduces costs. The following illustrate some of its main advantages: (1) igc employs a deterministic reclamation of objects (when the last effective reference to the object goes away), where traditional GCs free an object sometime later. This meets programmers natural expectations concerning object lifetimes, and makes debugging easier as subtle memory bugs come out earlier and are reproducible. (2) The amount of memory required by an igc app is generally much lower than a traditional GC because objects are released sooner. (3) Reference counting in igc helps the system to determine roots for live object traversal. It reduces igc cost to less than RC + GC. (4) igc is accurate. It doesn t suffer from fraudulent roots because it doesn t scan stack to determine roots. (5) igc never kicks in and suspends your threads. This fully-concurrent GC maintains high responsiveness of UI apps. (6) Objects are auto-managed by the system. This makes it open to numerous performance optimizations. We can rely on GC to recycle memory space while maintaining objects being destructed immediately after use. And, the system can move some objects around in heap to provide fast linear memory allocation, increasing performance by 4x 6x faster over the standard Objective-C/C++ allocator. (7) Garbage collection in igc maintains the same RC reclamation ordering, even when they are referenced by cycles. This feature is important in order to reuse the existing ios frameworks and libraries. Basic Concepts The igc system automatically manages your object s lifetime by monitoring every assignment to ivars, the fields of instance of Objective-C class. Contributions to reference count from ivars are done automatically by the system. There is no need to explicitly send retain/release messages for ivars in your igc program. If you use ios 5 LLVM Clang 3.0 compiler with ARC enabled, you can use igc without any retain/release calls

7 Let s take a look at an example of igc (with ARC Node : NSObject { Node Node + (void)work { Node * pa = [Node alloc]; pa->next = [Node alloc]; // create obj `A // create obj `B } Node * pc = [Node alloc]; pc->next = pc; // create obj `C // form a In the "work" method, when assigning an object reference to ivar `next, the system will monitor the operation and retain the object, e.g. object B is retained by ivar next, and will be released immediately when object A is destructed. Circular referenced objects, like object `C, will be eventually collected by garbage collection. There is no memory leak. Architecture igc garbage collector is implemented as a static library (libigc.a). When creating an instance of a class that uses default system [NSObject alloc] method, the system will automatically invoke igc API to return a managed object. C++ malloc or other objects created from custom routes are unmanaged. The system monitors ivar assignments and performs different operations according to whether the type of object is managed or not. igc Managed Application Frameworks & Libraries igc Runtime Objective-C Runtime Platform OS Services The collector only scans managed heap. It doesn t scan traditional root-set areas of other GCs, such as stack, global variables, malloc heap, etc. Objects created outside managed heap are not affected by the collector. How the Garbage Collector Works We noticed that cyclic references are only formed within managed heap. Although there are references from root-set, like stacks, to managed objects, but there is no effective reference from managed heap to the outside. From the collector s point of view, the reference graph between outside and heap is acyclic. It is appropriate to use reference counting to describe this situation

8 In general, igc maintains two reference counts for a managed object (there is nothing related to Objective-C retain count). One count (LRC) reflects the number of references from outside the heap. The other (ERC) reflects the number of references from within the heap. If both counts (LRC and ERC) of an object reach zero, then the object is freed immediately. If LRC is not zero, then the object is root for tracing in garbage collection. Unreachable objects are of zero LRC and non-zero ERC. Stacks Globals Managed Heap Unmanaged memory 0/1 0/1 1/0 0/1 1/0 0/2 1/1 These two counts, with some other control data, are stored in a special area separate from user-visible content of Objective-C objects. The area is packed and efficient for collector to scan. When garbage collection is triggered, the collector scans this area instead scanning or traversing all objects to determine roots. So the process of root determination is very fast compared to traditional GCs, which need to invoke system services to suspend threads, wait for synchronizations of multiple threads, avoid unsafe kick-in points, scan stacks (which may be deeply nested), etc. Several points of note regarding the collector: The collector is fully-concurrent. That is, it runs concurrently in a dedicated background thread, and never suspends any of your app threads. Write barriers are used to ensure the collector see the correct graph while app threads are constantly changing them. The system automatically triggers garbage collection if some conditions are reached, like memory threshold. And you can trigger garbage collection explicitly with some parameters, altering its behaviour. After the collector determines unreachable objects, each unreachable object is given a chance to declare a relationship of dependence for the process of destructing the object, and then the collector will figure out a reclamation ordering that satisfies all dependence requirements. igc has no "finalizer" concept, as is the case with some traditional GCs. Only destructor in C++ or [dealloc] method in Objective-C is associated with the lifetime of an object. The collector guarantees they are executed only once, even with resurrections. Enabling Garbage Collection You can mix igc code with non-gc code within an ios App project. If you use C++ or Objective-C++ smart pointer techniques, you don t even need to make patches on existing compilers. After applying patches to Xcode compilers, including GCC-4.2 and LLVM 3.0, you can use "-fobjc-gc" compiler flag to turn on igc garbage collection for ios platform

9 NOTE: We reuse this flag in ios, while in Mac OS X it is used to enable libauto conservative GC. The flag "-fobjc-gc-only" is still forbidden in igc for ios. For using igc with LLVM 3.0 Automatic Reference Counting (ARC), you can use the "-fobjc-arc-gc" compiler flag to turn garbage collection on. We use the term "ARCGC" mode for using igc with ARC. Also, you can add "-fobjc-arc-gc-compatible" compiler flag for recompilation of existing ARC source code. NOTE: GCC-4.2 doesn t support ARC, so ARCGC currently can only be used in LLVM 3.0 compiler. NOTE: Alternatively, you can use both compiler flags "-fobjc-arc" and "-fobjc-gc" to turn on ARCGC. But notice that Xcode IDE will remove the ARC "-fobjc-arc" flag if it detects either " fobjc-gc" or "-fobjc-gc-only" is on. So we recommend the use of "-fobjc-arc-gc". To link in igc static library, you need to add "-u igc " and "-ligc" to your Linker Flags and provide correct library search paths. Sometimes you need to add "-lstdc++" to include standard c++ library. NOTE: For ARCGC mode, in order to link in Apple s ARC wrapper library for pre-ios5 platforms, you need to add "-fobjc-arc" or "-fojb-arc-gc" to your Linker Flags. You can disable GC or ARCGC for ios with the compiler flag "-fno-objc-gc" or " fno-objc-arc". The last appearance of flags wins. We don t provide the corresponding "-fno-objc-arc-gc" flag, since it is not necessary. Your code can use macro OBJC_GC to check if GC is enabled, and use has_feature(objc_arc_gc) to check for ARCGC mode. See document "installation.pdf" in the igc SDK for more detailed instructions. Features & Specs igc provides you with accurate garbage collection for the ios platforms. You can write your ios managed application in Objective-C, Objective-C++ and C++ languages. This fully-concurrent collector runs in a dedicated background thread. It never suspends your application threads, giving you a very smooth experience and maintaining the responsiveness of your UI apps. Linear allocation gives you an option to boost your app s speed by 4x-6x times faster if it contains massive small-size objects. Main Features: Accurate collection igc is able to accurately identify all required references for garbage collection. Deterministic reclamation when the last effective reference to an object goes away, the object is immediately destructed. Reclamation ordering control Let you declare a dependence relationship for the process of destructing objects, and igc will figure out a reclamation ordering that satisfies all dependence requirements. Pause-less This fully-concurrent collector never suspend your works, maintaining high responsiveness of your UI apps

10 Compatibility using the existing ios frameworks and libraries, maintaining the same RC behaviour and reclamation ordering even if objects are referenced by cycles. Technical Details platforms & devtools Provide patches to GCC-4.2 and LLVM 3.0 to support GC for ios platform with or without ARC. Support Xcode development tools from "iphone SDK 3.2 Final with Xcode for Snow Leopard (10.6.0)" to the latest version. Running on ios platforms from version 3.0 at least. Support weak reference (using weak keyword) to remove cycles at design time for all ios versions. Support zeroing weak reference C API for all ios versions. Support zeroing weak reference using autoweak keyword in LLVM 3.0 compiler with ARC. Your source code is directly compiled into native ARM machine code, no virtual machine. Pointers are still pointers except ivars operations are emitted with write barrier routines. Technical Details reclamation You can send a [dealloc] message directly or a safer [destruct] message to destruct a live object safely. Solve the stack overflow issue of deeply nested destruction, which lurks in C++ RAII, reference counting, etc. Use one cleanup routine instead of a pair of "finalizer" and "destructor" methods, which is complicated and error prone. Allows resurrection multiple times on an object. Automatically detects resurrections by the system, which is constantly monitoring ivar assignments. You can safely access other objects in your destructor, which is impossible in traditional GC without reclamation ordering control. You can use "strong reference" to retain the target object for safe access from [dealloc] method. Most cycles formed by existing MRR objects can be detected, and you can use a non-intrusive way (no changes to class implementation code) to help the system reclaim them in a safe order. You can associate multiple destructors at runtime to a managed object, including primitive types. Technical Details Misc

11 You can use C++ smart pointer to reference an object s interior, undifferentiated the same as to the head of the object. The "interior => head" lookup mechanism is almost zero-cost, making it possible for applying interior pointer to frequent RC updates. Allows multiple-inheritance, embedded object as a field, object array, etc., saving space and unnecessary indirect references costs. You can have native C/C++ types and primitives under automatic management as a single object or an array. The count in igc is almost 64-bit, reducing the chance of count overflow. You can use Linear Pool to perform linear allocation (bumper allocation) to create massive small-sized objects. These objects can be moved around by the system when safe. Objects allocated from thread private pool can be shared between threads. You can switch back to pure reference counting by replacing the static library libigc.a with libigc_rr.a, which does not contain any GC code. Once you are sure your app hasn t any reference cycles, you are free to use this feature (some performance boost may be lost). Performance Testing In this section, we will give a quick look at the performance testing of igc, including GCbench testing, pause-less testing, and GC throughputs. GCBench testing GCBench is a widely-used testing program for GC performance. Basically, it creates large amounts of garbage nodes of binary trees with various depths and object lifetimes. It does not create circular reference, so that it can be used for reference counting testing as well as for real cycle collectors. We have built several GCBench testing programs using different memory management methods. The execution times of them are measured and compared. Here is a chart of the standardized results from ipod Touch 2G (7E18) and ipod Touch 4G (8L1). It shows significant performance boost by using igc in this program. Pause-less testing igc shares the same core of HnxGC. Here we present the testing result of HnxGC on Win32 platforms instead (because we don t have any other ios GCs at hand). In this testing program, an event is raised every 5 milliseconds with 1 millisecond resolution. Time elapsed between

12 two consecutive events are measured, which means in ideal situation the event interval should be 5 milliseconds. Here is the resulting graph of programs using HnxGC, Microsoft.NET and Boehm conservative GC, comparing with an ideal (idle running, no garbage collection) program. We can see that the result of HnxGC is indistinguishable from the ideal one. GC Throughputs This program tests how the size of live objects in heap affects the throughputs of garbage collections. It shows that although the time spent on object traversal are proportional to the size of live objects, HnxGC outperforms other competitors significantly, up to almost 1000x(Boehm s GC) and 100x (.NET CLR) times faster, when the amount of live objects is small. FAQs What languages does igc support? igc supports Objective-C, Objective-C++, and C++. What s the difference between HnxGC and igc? HnxGC is designed for C++ based cross-platform applications. igc is for ios platforms, primarily for Objective-C apps (also supports Objective-C++ and C++). HnxGC and igc share the same runtime core. Why not replace weak reference with zeroing weak reference? Zeroing weak reference is not zero-cost. The synchronization between application threads and the collector cannot be overlooked, since application thread may convert an object back to active via weak reference. App code must take special care to ensure correctness between threads. Therefore, zeroing weak reference cannot be used in the same way of non-zeroing weak reference in programming grammar. igc weak reference (non-zeroing) operates as it was used in traditional RC for design time cycle-breaking. It is a raw pointer, and igc doesn t monitor it. It is zero cost, and we recommend use it freely as in traditional manual management. In a hybrid GC (like igc), weak references are widely used to avoid cyclic references. The performance impact from weak reference is more important than in a non-rc garbage collection environment, where the uses of weak references are fewer

13 Can I use zeroing weak reference for pre-ios5 platforms? Yes. Zeroing weak reference is provided by igc runtime. It does not depend on Apple s ios 5 runtime. The keyword autoweak requires the LLVM 3.0 compiler, which is provided by Xcode 4.2 and ios SDK 5. You must install ios 5 devtools to build app for pre-ios 5 platforms. Why we need a runtime collector instead a dev-tool that detects cycles? If you use dev-tools to detect cycles, you have to modify your source code to eliminate cycles. Sometime it is easy, sometime it is not. You have to change some class definitions with weak ivars, or add some code to break cycles at proper runtime. Using a runtime collector, you don t need to inspect every related data structures in detail to figure out how to break cycles; just let the system to collect cycles, and you can explicitly invoke GC to after massive uses of objects. It shortens your software development cycles. Furthermore, garbage collection can boost the performance of your apps. Sometimes the boost is significant for a complicate data structure with massive small-sized objects. What s Direct Kill? If an object is mistakenly retained or referenced by other long-lived objects or code, automatic memory management system will think it may be actively used by others. To reclaim these types of unused objects, igc provides Direct Kill feature, which allows you explicitly reclaim a live object and in turn release resources it occupies directly or indirectly

14 Programming Guide The following sections describe programming changes from existing ios app development for writing an igc managed app. For something not mentioned in this document, they are the same as before. For example, the statement [[Foo alloc] init] creates an igc managed instance of class Foo. Object References Once your Objective-C source code is compiled by an igc compiler with garbage collection enabled, classes defined in the source code are managed classes, and code accessing ivars are monitored by the igc runtime. In igc, when assigning an object reference to an ivar, the system will automatically maintain reference counts of related objects and invoke write barrier routines, if necessary. Therefore, you don t need to send retain/release messages for ivars assignments. For other operations on non-ivar references, such as function return value and parameters, local and global variables, fields of unmanaged objects, etc., you need to use traditional ways to retain and release objects involved. For example, when assigning an object reference to a local variable (auto variable) in a function, you need to explicitly send a "release" message to the old value, and send a "retain" message to the new value. One exception is in ARCGC mode, since ARC compiler will generate [retain/release] calls for you. There are four types of references that can be used to declare ivars and property of an Objective-C class. They are "managed", "weak", strong", and "zeroing weak". Details of these 4 reference types are described below. Managed reference In igc managed code, a default ivar/property declaration is a "managed reference". For example, in the code below, ivar "_next" and property "next" are both managed Node : NSObject { Node * _next; (nonatomic, assign) next = igc runtime monitors assignments to managed references" to maintain correct retain counts, and follow them in tracing garbage collection. Cycles formed by "managed references" will be collected automatically. A "managed reference" holds the target object alive except when the containing object is a member of unreachable cycles being collected by garbage collector. Use "strong reference" instead to demand a reclamation ordering requests, to enforce the target is accessible in [dealloc] method

15 Strong reference A strong reference is a special managed reference that holds the target object alive even when the containing object is being destructed as unreachable cycles. For example, if object A points to object B via a strong reference, then when both A and B become unreachable, either as member of a cycle or being referenced by cycles, the collector will destruct object A prior to object B. Therefore, it is safe to access object B in the [dealloc] of object A. Only a property can be declared as a "strong reference" for compatibility reasons. A property with the "retain" attribute is a "strong Node : NSObject { Node * _next; (nonatomic, retain) Node next = Notice that, if "strong references" constitute a cycle, that raises a circular demands for reclamation ordering. This kind of situation should be avoided whenever possible. If this situation did happen, you can use igc runtime API _hnxgc_set_cycle_breaker() to overcome this problem by choosing some objects be reclaimed first. Weak reference Weak references in igc are *not* monitored by the system. They are raw pointers without any associated action emitted. Weak reference ivar and property must be declared explicitly with the weak keyword. For Node : NSObject { weak Node * _next; (nonatomic, assign) weak Node next = A weak reference does not hold target objects alive. To prevent an object from being reclaimed by the system, there must be at least one effective (non-weak) reference path to the object, or explicitly retain it by sending retain message. Zeroing weak reference A "zeroing weak reference" is a special weak reference. Its content will be auto-zeroed by the system when the target object is reclaimed. Synchronization rules between application threads and the collector should be carefully followed, because while an application thread is loading value from a "zeroing weak reference", the value may be "zeroing" by the collector. So, the loading operation should be atomic and returning a non-weak value. Although, you can use C API _hnxgc_weakref_xxx() to manipulate "zeroing weak reference", such as loading, storing, etc., the igc LLVM 3.0 compiler provides autoweak keyword to simplify this work as showed below

16 @interface Node : NSObject { autoweak Node * _next; (nonatomic, assign) autoweak next = Summary "Managed reference" retains the target object alive, and cycles formed by managed references can be automatically collected. However, it doesn't enforce any reclamation ordering for unreachable cycles, thus the target object of a managed reference may has been destructed before the [dealloc] cleanup method of the containing object. "Strong reference" guarantees the target object remains alive for the cleanup method by demanding a reclamation ordering to igc system. "Weak reference" can be used to avoid cycles at design time to collect objects immediately. "Zeroing weak reference" makes your code safe, but does introduce some extra runtime cost compared to non-zeroing weak. ARCGC_C & Reference Mapping ARCGC_Compatible(ARCGC_C) mode is designed to reuse ARC source code without change in igc garbage collection environment. It maps ARC ivar and property attributes and qualifiers to igc equivalents. For example, is mapped to igc managed reference. To enter ARCGC_Compatible mode, add a compiler flag -fobjc-arc-gc-compatible in addition to existing ARCGC flag -fobjc-arc-gc in project settings. Below is a table showing reference equivalence of various modes

17 - 17 -

18 - 18 -

19 Object Reclamation The cleanup routine In igc, to define a cleanup routine with an object, you can merely define a [dealloc] method in Objective-C or a destructor in C++. The grammar is the same as in standard Objective-C and C++ languages. In igc, there is no concept of "finalizer" or the like as in many other GCs. Remember, with garbage collection enabled, you should not send [release] message to ivars in your [dealloc] method, as igc has already handled that for ivars. Additionally, for ARCGC mode, you don t need to invoke the [super dealloc] method call, since ARC will do that for you. - (void)dealloc { printf("[object (%p) dealloc]\n", self); free(data); // essential cleanup jobs #if!defined( OBJC_GC ) [aivar release]; // for manual management #endif #if! has_feature(objc_arc) [super dealloc]; // for non-arc #endif } Immediate reclamation When an object loses all effective references to it, the system will destruct it immediately. Ivars of an object become invalid just after the object has been destructed. Thus, child objects lose references after destruction of the parent object. Notice that the ivar references are not dropped during the destruction. That is different from previous manual management which drops member references manually in the [dealloc] method. One of the main reasons for this design is to avoid the nesting of destructions, which may cause stack overflow in some large data structures. For example, a single linked list with more than 30,000 nodes. Another reason is for performance, as references dropped by the system are usually slightly faster than in a user-defined [dealloc] method. If you need, you can assign nil to the ivar reference in your [dealloc] method. The child will be released immediately. If there are no other references to the child, the child will be destructed nested inside your [dealloc] method call. Acyclic group of objects can be destructed immediately by destructing the head object of the group. However, if an acyclic group of objects are referenced by a cycle, then these objects are not destructed immediately. You can use (1) weak reference to avoid cyclic graph at design time, or (2) assign nil to ivars at runtime to break existing cycles. (3) If you cannot modify your class interface, or access some private ivars, you can use the following approach to destruct a live object to break a cycle. You can send an igc specific [destruct] message to a live object to destruct the object immediately You can directly send a [dealloc] message to a live object to destruct the object immediately (NOTE: Apple ios UI framework did do in this way, although in their document they recommend not do this). The [dealloc] message directly sent should stand-in a [release] message and balance with a [retain] message as shown in the following igc code snippet (non-arc mode)

20 Node *a = [Node alloc]; Node *b = [Node alloc]; a->next = b; b->next = a; [a destruct]; // immediately execute [dealloc] of 'a' and 'b' [a destruct]; // it is safe to send multiple times, only the first [a destruct]; // one is effective [b release]; [a release]; Alternative, you can use C API _hnxgc_destruct_object() or C++ gcdelete keyword to do the same thing. NOTE: LLVM 3.0 ARC disallows you to send [dealloc] message. But, in ARCGC mode, this limitation is removed after applying a patch to SDK headers. Reclamation ordering Many traditional GCs collect unreachable objects in an unpredictable order. That means, in the [dealloc] method, that the target object referenced by an ivar may have been destructed even though you hold an effective ivar reference to it. Methods provided by the destructed object may malfunction. igc provides a way to let you control the reclamation ordering of unreachable objects. You can define an "OnReclaim" call back method in your C++ class, and when the collector has determined all unreachable objects, the system will invoke the "OnReclaim" method of each unreachable object. In the method, you can use API to declare a dependences relationship for the process of destructing the object. For example, you can declare that the process of destructing object A depends on object B and C, which may be directly or indirectly referenced. Object A could be another object, not limited to the "this" pointer of the "OnReclaim" method. After all unreachable objects have claimed their dependence requirements, the system will figure out a reclamation ordering that satisfies all these requirements. Therefore, you know the objects that you want to use in your cleanup routine are guaranteed to be alive, and you are free to access them the same as if they were not in a cleanup routine. For Objective-C and Objective-C++ language, you can use "strong reference" ivar to retain the target object alive during the destruction of the containing object. Resurrection In igc, once the destruction of an object is started, the object cannot be reverted to live again. Because partial destruction may change the consistency of the internal status of the object, restoring its status to live may require the introduction of complicated API and high runtime costs. So, we just disallow it and provide an alternative solution. You can resurrect an object that is going to be reclaimed. In the "OnReclaim" method described previously, you can resurrect another object or itself by assigning the object to an effective reference to make it reachable again. The collector will monitor that change and put the object and its descendants back to live again, before real destruction starts. There is no limitation on the number of resurrections for a single object. Once an object is resurrected, its status is reverted back to original, as if nothing has happened

21 Dynamic associated destructor At runtime, you can dynamically associate multiple destructors to an existing managed object, including primitives. For example, suppose you create a managed integer array, you can associate a destructor to perform some cleanup tasks for it. NOTE: We do not use any hash table or the like to implement this feature, so the negative performance impacts on object reclamation are minimized almost to zero. Reclaim circular depended cycles Garbage collector can reclaim circular referenced objects without difficulty, but how should those that are circular depended be handled? For example, suppose object A and B both request that the system keep each other alive for processing their cleanup routine. In another example, suppose some design pattern sends "retain" messages to both objects A and B, to keep those objects alive, and sends "release" messages in the [dealloc] methods of A and B. This makes it impossible to determine a correct reclamation order for objects A and B. igc provides a mechanism to address this issue. You don t need to alter your designs or class layouts. Once you know which objects form circular dependence cycles by studying debug outputs, you can use API _hnxgc_set_cycle_breaker(obj) to tell the system where to start reclamation for these cycles. Then, when the collector detects these unreachable cycles, it knows where to start to reclaim the circular dependence cycles. Linear Pool Linear Pool is a mechanism that can be used in C++ and Objective-C/C++ to provide fast linear (bumper) allocation for small-sized managed objects. As illustrated in the figure below, objects are allocated linearly from the pool. The type of allocation is very fast, and unused objects are freed automatically by the GC system, not individually by the application threads. Thus, application threads gain the maximum performance benefit from it Head Obj 1 Obj 2 Obj n-1 Obj n Free Space Features & tech specs Different sized objects can be allocated linearly from the same pool. You can create multiple pools in a process. Object allocation from a pool is not thread safe, but once the object is created, it is thread safe like other usual objects. If a pool is shared between multiple threads, i.e. not a per-thread private pool, you have to use external synchronizations to ensure safety for the operation of object allocation. Objects inside a pool can reference other objects or vice versa, regardless of whether they exist outside the pool or in other pools

22 The maximum size of an object in a pool is close to 64KB, though we recommend making them not larger than 2KB for performance considerations. There is no limitation on the number of objects or on total object size in a pool. Objects are not freed individually; they are freed as a group when all objects inside the group are unreachable or evacuated. Objects can be evacuated from a pool, i.e., they are moved out of the pool and become a standalone object. The system will automatically update related references when objects move. You can explicitly request an evacuation when it is safe. The pool itself is also auto managed; it will be reclaimed when there is no reference to the pool or to any objects inside the pool. Objects in pool should not contain any cleanup method, and they should not reference RC type standalone objects. Create a linear pool You can use C API _hnxgc_linearpool_create() to create a pool. It returns a locked handle to the newly-created pool. Or you can use a wrapper C++ class, e.g. gcptr<gclinearpool> ppool = GCLinearPool::Create(); Create an object from a pool C++ with wrapper class: gcptr<cnode> p = gcnew_pool(ppool) CNode; Objective-C: Node * p = [Node createfrompool:hpool]; An object returned from [createfrompool] is a weak reference, not retained. If you want to retain it longer than the lifetime of the pool, you should make at least one effective reference to it before all references are dropped, e.g., send a [retain] message to it, or assign it to an ivar. After objects are effectively referenced or retained, you can invoke _hnxgc_linearpool_safetorelease(hpool) to let the system to release space of non-referenced objects in the pool. Drop references to pool or objects References to pools or objects in pools are normal references, the same as for any general standalone objects. You can drop them as usual

23 Evacuate objects from a pool You can invoke _hnxgc_linearpool_evacuate(hpool); to move all live objects out of the specific pool. During the service call, the system will update related references to new object locations. All spaces in the pool are released. The time taken by this operation varies depending on some factors, such as the size of active objects in the pool and the number of related references, etc

24 API References

25 Predefined Macros OBJC_GC has_feature(objc_arc) has_feature(objc_arc_gc) defined if garbage collection is enabled for ios platforms true if it is in ARC, ARCGC, or ARCGC_Compatible mode true if it is in ARCGC or ARCGC_Compatible mode has_feature(objc_arc_gc_compatible) has_feature(objc_hnxgc) has_feature(objc_hnx) true if it is in ARCGC_Compatible mode true if it is in igc garbage collection is enabled, including in ARCGC and ARCGC_Compatible mode true if the compiler is igc LLVM

26 Objective-C Methods destruct destruct a live object immediately NSObject () - Example: id p = [[Foo alloc]init];... [p destruct]; // immediately destruct `p'... [p release]; createfrompool allocate a managed object from specified linear pool NSObject () + ( weak Node: NSObject { Node * next; uintptr_t hpool = _hnxgc_linearpool_create(); Node * p = [[Node createfrompool:hpool] retain];... [p release];

27 weak Objective-C keyword to declare a weak (non-zeroing) ivar or property. <see "Programming Guide: Object Reference"> autoweak Objective-C keyword to declare an auto-zeroing weak ivar or property. <see "Programming Guide: Object Reference">

28 C Functions _hnxgc_collect explicitly request tracing garbage collection Declaration: void _hnxgc_collect(); _hnxgc_destruct_object safely destruct a live object Declaration: void _hnxgc_destruct_object (void * obj); _hnxgc_add_memory_pressure notify the system to perform garbage collection more frequently. Declaration: void _hnxgc_add_memory_pressure(size_t size, bool bremove); Example: int nextramemsize = 64 * 1024; + (id)alloc {... // allocate some memory for this object _hnxgc_add_memory_pressure(nextramemsize, 0); memory pressure... } - (void)dealloc {... // free the associated memory _hnxgc_add_memory_pressure(nextramemsize, 1); pressure... } // increase // reduce memory

29 _hnxgc_did_receive_memory_warning _hnxgc_set_trace_level notify igc that the memory available is very low Declaration: void _hnxgc_did_receive_memory_warning(); Example: - (void)applicationdidreceivememorywarning:(uiapplication *)application {... _hnxgc_did_receive_memory_warning(); } trace igc behaviors, such as performing GC, allocate objects and reclaim garbages Declaration: int _hnxgc_set_trace_level(int level); // returns: existing trace level // input: new trace level [0 : not trace, 1-999: the high value the more print-outs] Example: _hnxgc_set_trace_level(200); _hnxgc_set_cycle_breaker set an object where igc start to reclaim dependence cycles Declaration: void _hnxgc_set_cycle_breaker (void * obj); _hnxgc_set_nonrc set an object to non-rc state NOTE: ivar references to non-rc object are traced but do not contribute to reference count, so the object will

30 not be reclaimed immediately after use (can only be reclaimed by garbage collection). Declaration: void _hnxgc_set_nonrc(void * obj); _hnxgc_weakref_allocate allocate a zeroing weak reference slot Declaration: uintptr_t _hnxgc_weakref_allocate(); _hnxgc_weakref_deallocate free a zeroing weak reference slot Declaration: void _hnxgc_weakref_deallocate(uintptr_t slot); _hnxgc_weakref_read return a "retained" reference from a zeroing weak reference slot Declaration: void * _hnxgc_weakref_read(uintptr_t slot); _hnxgc_weakref_write assign a reference to a zeroing weak reference slot Declaration: void _hnxgc_weakref_write(uintptr_t slot, void * obj); <General C API> manipulate managed objects in C, C++, Objective-C and Objective-C++ langugages, see HnxGC documents for more details

31 C++ API gcptr<t>::i smart pointer for managed ivar reference Declaration: gcptr<t>::i <ivar name> Node : NSObject { gcptr<node>::i nextnode; gcptr<t> flexible smart pointer for non-ivar reference Declaration: gcptr<t> <ivar name> gcnew_i create C++ smart pointer style objective-c object Declaration: gcnew_i <expression returns a retained object> Example: gcptr<node> pnode = gcnew_i [Node alloc]; gcdelete safely destruct a live object Example: Node * pnode = [[Node alloc] init];... gcdelete pnode;

32 Example 2: gcptr<node> pnode = gcnew_i [[Node alloc] init];... gcdelete pnode; <General C++ API> manipulate managed objects in C++ and Objective-C++ languages, see HnxGC documents for more details

33 Articles The following will contain some selected articles regarding igc / HnxGC. Some of them are advanced and may not be updated with the changes of new release of igc / HnxGC

34 Maintaining existing RC cleanup ordering Adding GC to existing RC-based system brings in some new challenges. In this section, we will describe the problem of cleanup ordering and how it was solved in igc. The problem Reference counting has implied a very strong ordering on cleanup of unused objects. Naively switching RC system to GC will break this implicit behaviour on which many existing RC codes depend. For instance, if existing RC-based objects (acyclic) are referenced by cyclic garbage, then they can only be collected by GC and may be disposed in a wrong ordering different from existing RC code. Therefore, some existing code may crash because in their cleanup code, they may access some other objects which have been destructed by GC as effective reference cannot keep object alive during cleanup code. Therefore, in order to reuse the existing RC-base code, we must maintain the same cleanup ordering of those RC-based acyclic objects. Discarded attempts One attempt to solve this problem is to treat RC-based objects as native non-managed, but this way is inefficient and objects are beyond the management of GC system causing cycles of them uncollectible. Some GC systems (such as Boehm GC) only cleanup unreachable objects that are not referenced by others, then in turn expect more objects become eligible for this rule. Using this way to maintain cleanup ordering has at least two issues: (1) the cleanup of cycles and their descendants are skipped undesirably, e.g. in Figure-1, if C2 needs cleanup, then because C2 references C1, A1 to A4, all the cleanup routines of these objects are not executed under this rule; (2) if the GC system does not immediately reclaim zero-rc objects, then some orderings enforced by RC-based code are violated. For example, RC can enforce a cleanup ordering of A3 prior to A4 by explicitly nullifying reference from A2 to A3 in A2 s cleanup code. In conclusion, such GCs are not suitable for a RC compatible system. igc solution C1 A1 A3 C2 GC Collected Same RC Order A2 A4 In igc, RC-based objects are under the management of the system. Existing RC code (retain/release Unreachable Garbage message) are replaced with maintaining a per-object count that represents the number of references from Figure 1 root-set. That is, references from original RC-based objects are treated as coming from root-set, thus the referents will not be collected by normal GC when they become unreachable. After GC collects other cycles that are referencing them, the reference counts of these RC-based objects will drop to zero, then the objects will be collected consequently and the ordering of cleanup will be exactly the same as original RC. For example, as showed in Figure-1, unreachable RC-based objects A2, A3 and A4 are treated as being referenced by root-set. They are not collected until C1, C2 and A1 are collected by GC. After GC collects and destructs A1, then objects A2, A3 and A4 will consequently lose their last references and be destructed in the same order as in the original RC-based system. See also: "Aggressive garbage collection" collect cycles of existing RC objects

35 Solving stack overflow due to deep-nested destructions Many C++ programmers may be unaware of a bug residing in their data structures. Consider the following innocent looking code which defines a node for a linked list: struct Foo { Foo * m_pnext; ~Foo() { delete m_pnext; } }; As is typical, this structure contains the logic for cleaning up the linked list in its destructor by deleting its sibling node. The sibling node then deletes its sibling and so on, until the entire linked list is deleted. A typical routine for constructing and using the linked list might look something like this: #define NESTED_DEPTH static void dotest() { // create a singlely-linked list Foo * pfirst = 0; for (int i = 0; i < NESTED_DEPTH; i++) { Foo * pnode = new Foo; pnode->m_pnext = pfirst; pfirst = pnode; } } // dispose the list delete pfirst; When it comes time to delete the list, clean up is invoked by deleting the first node. In applications where there are a relatively small number of nodes, this code works just fine. But what happens when the length of the linked list is really long such as in the example above where there are 100,000 nodes? Since Foo's destructor causes a stack based, depth first traversal of all of the nodes in the data structure, the traversal of such a large topology will overflow the call stack causing a stack overflow exception. This problem can also occur on smaller topologies when the destructor requires a larger stack frame, e.g. when the destructor contains a fixed sized array. The worse part is that this bug may not show up until the code is being used in a shipped application handling massive amounts of data. In HnxGC, this problem is easily solved by rewriting the code as shown here: struct Foo { HNXGC_ENABLE(Foo) gcptr<foo> m_pnext; // replace Foo * with gcptr<foo> ~Foo() { // removed call to delete-deletion of m_pnext is automatically // handled by gcptr } }; gcptr is a smart pointer class provided by HnxGC which takes care of the destruction for you. With HnxGC you simply redefine all pointers to your structure using the templated gcptr class, passing in your structure name as the template parameter

36 You then delete the linked list by clearing all pointers to the first node of the data structure as shown here: #define NESTED_DEPTH static void dotest() { // create a singlely-linked list gcptr<foo> pfirst = 0; // replace Foo * with gcptr<foo> for (int i = 0; i < NESTED_DEPTH; i++) { gcptr<foo> pnode = gcnew Foo; // replace Foo * with gcptr<foo> pnode->m_pnext = pfirst; pfirst = pnode; } } // dispose the list pfirst = 0; // using HnxGC, deletion is performed by simply // removing all references to pfirst // all objects will be destructed before the next // instruction Behind the scenes HnxGC decreases the reference count when pfirst is set to 0. When the reference count reaches 0, HnxGC safely deletes the entire linked list for you. The reason why HnxGC safely deletes the list is because HnxGC deletes each node one by one, thereby releasing the stack frame after execution of the current destructor before calling the next destructor. This means that the size of the destructor's call stack does not grow and therefore topologies of any size will be safely deleted. This also results in faster execution than with nested destruction because the space required for the stack is smaller. With HnxGC you can finally encapsulate the initialization and clean up logic of your data structures directly in the structure itself without worrying about stack overflow problems. And because destruction executes immediately upon invoking clean up, HnxGC's garbage collection is deterministic, as opposed to other implementations where you have little or no control over when clean up occurs. Destruction of circular references (while not immediate even with HnxGC) is also faster with HnxGC because it avoids the costly suspension of application threads. Stack overflows during destruction are a serious problem, and can exist across different C++ development frameworks. In traditional C++ it's common to implement Resource Acquisition Is Initialization (RAII) where by construction and destruction of a resource are encapsulated to take advantage of the fact that objects on the stack are cleaned up. The problem can also exist in other frameworks which may give the illusion that they enable safe destruction. For example, Boost's shared_ptr and intrusive_ptr classes define an interface for clean up, but because it's up to the programmer to implement the destruction logic, one can easily implement the nested destruction problem. Another example is Managed C++ where, despite the apparent safety of the garbage collected environment, programmers are still free to implement nested destruction which is executed on a growing call stack just like regular C

37 Choosing new style coding In new style coding for Objective-C, you don t write codes explicitly to maintain RC for references from an object. Instead, the underlying runtime library will do the RC maintenance for you. Using the new style of coding has the following advantages over previous styles: Reduced code writing workloads Making more cycles non-sensitive to cleanup ordering Writing one code for both GC and RC Reducing your code writing workload Previously, programmers typically used property mechanisms to alleviate the work for manual maintenance of RC. For example, individuals may to declare a property and then allow a compiler to generate appropriate accessor methods to perform RC work. But, since the compiler has no direct interaction with the dealloc method, properties are not automatically released in the dealloc method for you. So, in a dealloc method you have to release objects directly or invoke accessor with nil as the parameter. If you forget to do this, or do it in an incorrect way (e.g., releasing a non-retain property), your application may leak or even crash. In new style coding, runtime library will automatically release property for you. So you can (retain) to declare property as usual, but in the dealloc method you don t need to write code to release it manually. The dealloc method should only contain the essential cleanup code. This allows you to focus on your application logic and lets the system take care of the rest of the work, such as object lifetime management. Also, new style coding uses (assign) (retain) keywords to denote specific uses of properties and pointer fields. This makes codes more readable and the purpose of pointers more clearly distinguishable. Making more cycles non-sensitive to cleanup ordering In the new style coding the dealloc method only contains essential cleanup code, as the responsibility of releasing referent objects is passed to the underlying runtime library. If there is no need to do any essential cleanup job, then there is no need to define a dealloc method. This increases the chance of not define a dealloc method, compared to the previous style where usually there are many dealloc methods that exist only to perform object releasing work. If an object doesn t have a dealloc method, then it is not concerned with any other objects that are alive or not. No other objects should be kept alive for this object (igc allows doing release operation on a dead object). Therefore, it gives more freedom to the system in reclamation ordering of unreachable cycles. NOTE: In the previous style, if a cycle has been formed, then it is almost impossible to auto-determine a correct cleanup ordering, as dealloc methods usually have been defined. To cleanup these cyclic objects in a safe order, you must activate the Aggressive GC option and manually specify where to start

ITP 342 Advanced Mobile App Dev. Memory

ITP 342 Advanced Mobile App Dev. Memory ITP 342 Advanced Mobile App Dev Memory Memory Management Objective-C provides two methods of application memory management. 1. In the method described in this guide, referred to as manual retain-release

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 - Spring 2013 1 Memory Attributes! Memory to store data in programming languages has the following lifecycle

More information

Run-Time Environments/Garbage Collection

Run-Time Environments/Garbage Collection Run-Time Environments/Garbage Collection Department of Computer Science, Faculty of ICT January 5, 2014 Introduction Compilers need to be aware of the run-time environment in which their compiled programs

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC 330 Spring 2017 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

More information

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection CMSC 330: Organization of Programming Languages Memory Management and Garbage Collection CMSC330 Fall 2018 1 Memory Attributes Memory to store data in programming languages has the following lifecycle

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Programming Language Implementation

Programming Language Implementation A Practical Introduction to Programming Language Implementation 2014: Week 10 Garbage Collection College of Information Science and Engineering Ritsumeikan University 1 review of last week s topics dynamic

More information

Programmer Directed GC for C++ Michael Spertus N2286= April 16, 2007

Programmer Directed GC for C++ Michael Spertus N2286= April 16, 2007 Programmer Directed GC for C++ Michael Spertus N2286=07-0146 April 16, 2007 Garbage Collection Automatically deallocates memory of objects that are no longer in use. For many popular languages, garbage

More information

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11 CS 536 Spring 2015 1 Handling Overloaded Declarations Two approaches are popular: 1. Create a single symbol table

More information

Mobile Application Development

Mobile Application Development Mobile Application Development Lecture 13 Introduction to ObjectiveC Part II 2013/2014 Parma Università degli Studi di Parma Lecture Summary Object creation Memory management Automatic Reference Counting

More information

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs

Performance of Non-Moving Garbage Collectors. Hans-J. Boehm HP Labs Performance of Non-Moving Garbage Collectors Hans-J. Boehm HP Labs Why Use (Tracing) Garbage Collection to Reclaim Program Memory? Increasingly common Java, C#, Scheme, Python, ML,... gcc, w3m, emacs,

More information

Garbage Collection (1)

Garbage Collection (1) Garbage Collection (1) Advanced Operating Systems Lecture 7 This work is licensed under the Creative Commons Attribution-NoDerivatives 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/4.0/

More information

CS 241 Honors Memory

CS 241 Honors Memory CS 241 Honors Memory Ben Kurtovic Atul Sandur Bhuvan Venkatesh Brian Zhou Kevin Hong University of Illinois Urbana Champaign February 20, 2018 CS 241 Course Staff (UIUC) Memory February 20, 2018 1 / 35

More information

Robust Memory Management Schemes

Robust Memory Management Schemes Robust Memory Management Schemes Prepared by : Fadi Sbahi & Ali Bsoul Supervised By: Dr. Lo ai Tawalbeh Jordan University of Science and Technology Robust Memory Management Schemes Introduction. Memory

More information

Garbage Collection. Steven R. Bagley

Garbage Collection. Steven R. Bagley Garbage Collection Steven R. Bagley Reference Counting Counts number of pointers to an Object deleted when the count hits zero Eager deleted as soon as it is finished with Problem: Circular references

More information

Memory Management: The Details

Memory Management: The Details Lecture 10 Memory Management: The Details Sizing Up Memory Primitive Data Types Complex Data Types byte: char: short: basic value (8 bits) 1 byte 2 bytes Pointer: platform dependent 4 bytes on 32 bit machine

More information

Automatic Memory Management

Automatic Memory Management Automatic Memory Management Why Automatic Memory Management? Storage management is still a hard problem in modern programming Why Automatic Memory Management? Storage management is still a hard problem

More information

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection. Automatic Memory Management #1 One-Slide Summary An automatic memory management system deallocates objects when they are no longer used and reclaims their storage space. We must be conservative and only

More information

Structure of Programming Languages Lecture 10

Structure of Programming Languages Lecture 10 Structure of Programming Languages Lecture 10 CS 6636 4536 Spring 2017 CS 6636 4536 Lecture 10: Classes... 1/23 Spring 2017 1 / 23 Outline 1 1. Types Type Coercion and Conversion Type Classes, Generics,

More information

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008 Dynamic Storage Allocation CS 44: Operating Systems Spring 2 Memory Allocation Static Allocation (fixed in size) Sometimes we create data structures that are fixed and don t need to grow or shrink. Dynamic

More information

A new Mono GC. Paolo Molaro October 25, 2006

A new Mono GC. Paolo Molaro October 25, 2006 A new Mono GC Paolo Molaro lupus@novell.com October 25, 2006 Current GC: why Boehm Ported to the major architectures and systems Featurefull Very easy to integrate Handles managed pointers in unmanaged

More information

Lecture 13: Garbage Collection

Lecture 13: Garbage Collection Lecture 13: Garbage Collection COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer/Mikkel Kringelbach 1 Garbage Collection Every modern programming language allows programmers

More information

Lecture Notes on Garbage Collection

Lecture Notes on Garbage Collection Lecture Notes on Garbage Collection 15-411: Compiler Design Frank Pfenning Lecture 21 November 4, 2014 These brief notes only contain a short overview, a few pointers to the literature with detailed descriptions,

More information

Lecture Notes on Advanced Garbage Collection

Lecture Notes on Advanced Garbage Collection Lecture Notes on Advanced Garbage Collection 15-411: Compiler Design André Platzer Lecture 21 November 4, 2010 1 Introduction More information on garbage collection can be found in [App98, Ch 13.5-13.7]

More information

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1

CS 345. Garbage Collection. Vitaly Shmatikov. slide 1 CS 345 Garbage Collection Vitaly Shmatikov slide 1 Major Areas of Memory Static area Fixed size, fixed content, allocated at compile time Run-time stack Variable size, variable content (activation records)

More information

Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/ WG21/N0932. Bjarne Stroustrup. AT&T Research.

Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/ WG21/N0932. Bjarne Stroustrup. AT&T Research. Proposal to Acknowledge that Garbage Collection for C++ is Possible X3J16/96-0114 WG21/N0932 Bjarne Stroustrup AT&T Research Murray Hill New Jersey 07974 USA The ARM, "The C++ Programming Language (2nd

More information

Lecture 15 Garbage Collection

Lecture 15 Garbage Collection Lecture 15 Garbage Collection I. Introduction to GC -- Reference Counting -- Basic Trace-Based GC II. Copying Collectors III. Break Up GC in Time (Incremental) IV. Break Up GC in Space (Partial) Readings:

More information

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case)

Design Phase. Create a class Person. Determine the superclass. NSObject (in this case) Design Phase Create a class Person Determine the superclass NSObject (in this case) 8 Design Phase Create a class Person Determine the superclass NSObject (in this case) What properties should it have?

More information

Heap Management. Heap Allocation

Heap Management. Heap Allocation Heap Management Heap Allocation A very flexible storage allocation mechanism is heap allocation. Any number of data objects can be allocated and freed in a memory pool, called a heap. Heap allocation is

More information

Item 18: Implement the Standard Dispose Pattern

Item 18: Implement the Standard Dispose Pattern Item 18: Implement the Standard Dispose Pattern 1 Item 18: Implement the Standard Dispose Pattern We ve discussed the importance of disposing of objects that hold unmanaged resources. Now it s time to

More information

Static initializers in C++

Static initializers in C++ Static initializers in C++ Umesh Nair June 2001 1 Introduction The problem of wrong order of static initialization causes applications to crash. People try several workarounds-changing the order of dynamic

More information

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection

Deallocation Mechanisms. User-controlled Deallocation. Automatic Garbage Collection Deallocation Mechanisms User-controlled Deallocation Allocating heap space is fairly easy. But how do we deallocate heap memory no longer in use? Sometimes we may never need to deallocate! If heaps objects

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 2 Thomas Wies New York University Review Last week Programming Languages Overview Syntax and Semantics Grammars and Regular Expressions High-level

More information

QUIZ Friends class Y;

QUIZ Friends class Y; QUIZ Friends class Y; Is a forward declaration neeed here? QUIZ Friends QUIZ Friends - CONCLUSION Forward (a.k.a. incomplete) declarations are needed only when we declare member functions as friends. They

More information

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties

CS193P - Lecture 3. iphone Application Development. Custom Classes Object Lifecycle Autorelease Properties CS193P - Lecture 3 iphone Application Development Custom Classes Object Lifecycle Autorelease Properties 1 Announcements Assignments 1A and 1B due Wednesday 1/13 at 11:59 PM Enrolled Stanford students

More information

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers CSCI-1200 Data Structures Fall 2011 Lecture 24 Garbage Collection & Smart Pointers Review from Lecture 23 Basic exception mechanisms: try/throw/catch Functions & exceptions, constructors & exceptions Today

More information

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime Runtime The optimized program is ready to run What sorts of facilities are available at runtime Compiler Passes Analysis of input program (front-end) character stream Lexical Analysis token stream Syntactic

More information

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides Garbage Collection Last time Compiling Object-Oriented Languages Today Motivation behind garbage collection Garbage collection basics Garbage collection performance Specific example of using GC in C++

More information

Summary: Issues / Open Questions:

Summary: Issues / Open Questions: Summary: The paper introduces Transitional Locking II (TL2), a Software Transactional Memory (STM) algorithm, which tries to overcomes most of the safety and performance issues of former STM implementations.

More information

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file? QUIZ on Ch.5 Why is it sometimes not a good idea to place the private part of the interface in a header file? Example projects where we don t want the implementation visible to the client programmer: The

More information

Announcement. Final Project Proposal Presentations and Updates

Announcement. Final Project Proposal Presentations and Updates Announcement Start Final Project Pitches on Wednesday Presentation slides dues by Tuesday at 11:59 PM Email slides to cse438ta@gmail.com Extensible Networking Platform 1 1 - CSE 438 Mobile Application

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection

CS577 Modern Language Processors. Spring 2018 Lecture Garbage Collection CS577 Modern Language Processors Spring 2018 Lecture Garbage Collection 1 BASIC GARBAGE COLLECTION Garbage Collection (GC) is the automatic reclamation of heap records that will never again be accessed

More information

Principles of Programming Languages. Objective-C. Joris Kluivers

Principles of Programming Languages. Objective-C. Joris Kluivers Principles of Programming Languages Objective-C Joris Kluivers joris.kluivers@gmail.com History... 3 NeXT... 3 Language Syntax... 4 Defining a new class... 4 Object identifiers... 5 Sending messages...

More information

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank

Algorithms for Dynamic Memory Management (236780) Lecture 4. Lecturer: Erez Petrank Algorithms for Dynamic Memory Management (236780) Lecture 4 Lecturer: Erez Petrank!1 March 24, 2014 Topics last week The Copying Garbage Collector algorithm: Basics Cheney s collector Additional issues:

More information

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

the gamedesigninitiative at cornell university Lecture 9 Memory Management

the gamedesigninitiative at cornell university Lecture 9 Memory Management Lecture 9 Gaming Memory Constraints Redux Wii-U Playstation 4 2GB of RAM 1GB dedicated to OS Shared with GPGPU 8GB of RAM Shared GPU, 8-core CPU OS footprint unknown 2 Two Main Concerns with Memory Getting

More information

HOT-Compilation: Garbage Collection

HOT-Compilation: Garbage Collection HOT-Compilation: Garbage Collection TA: Akiva Leffert aleffert@andrew.cmu.edu Out: Saturday, December 9th In: Tuesday, December 9th (Before midnight) Introduction It s time to take a step back and congratulate

More information

Advanced Programming & C++ Language

Advanced Programming & C++ Language Advanced Programming & C++ Language ~6~ Introduction to Memory Management Ariel University 2018 Dr. Miri (Kopel) Ben-Nissan Stack & Heap 2 The memory a program uses is typically divided into four different

More information

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc. Chapter 1 GETTING STARTED SYS-ED/ Computer Education Techniques, Inc. Objectives You will learn: Java platform. Applets and applications. Java programming language: facilities and foundation. Memory management

More information

Object-Oriented Programming for Scientific Computing

Object-Oriented Programming for Scientific Computing Object-Oriented Programming for Scientific Computing Dynamic Memory Management Ole Klein Interdisciplinary Center for Scientific Computing Heidelberg University ole.klein@iwr.uni-heidelberg.de 2. Mai 2017

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 14: Memory Management Viktor Leijon Slides largely by Johan Nordlander with material generously provided by Mark P. Jones. 1 First: Run-time Systems 2 The Final Component:

More information

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley

Managed runtimes & garbage collection. CSE 6341 Some slides by Kathryn McKinley Managed runtimes & garbage collection CSE 6341 Some slides by Kathryn McKinley 1 Managed runtimes Advantages? Disadvantages? 2 Managed runtimes Advantages? Reliability Security Portability Performance?

More information

Lecture Notes on Garbage Collection

Lecture Notes on Garbage Collection Lecture Notes on Garbage Collection 15-411: Compiler Design André Platzer Lecture 20 1 Introduction In the previous lectures we have considered a programming language C0 with pointers and memory and array

More information

Inside the Garbage Collector

Inside the Garbage Collector Inside the Garbage Collector Agenda Applications and Resources The Managed Heap Mark and Compact Generations Non-Memory Resources Finalization Hindering the GC Helping the GC 2 Applications and Resources

More information

the gamedesigninitiative at cornell university Lecture 10 Memory Management

the gamedesigninitiative at cornell university Lecture 10 Memory Management Lecture 10 Gaming Memory (Current Generation) Playstation 4 8 GB RAM (unified) X-Box One (X) 12 GB RAM (unified) 9 GB for games Nintendo Switch 3 GB RAM (unified) 1 GB only for OS iphone/ipad 2 GB RAM

More information

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Management! Goals of this Lecture! Dynamic Memory Management!!! 1 Goals of this Lecture! Help you learn about:! Dynamic memory management techniques! Garbage collection by the run-time system (Java)! Manual deallocation by the programmer

More information

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2 CPSC 213 Introduction to Computer Systems Winter Session 2017, Term 2 Unit 1c Jan 24, 26, 29, 31, and Feb 2 Instance Variables and Dynamic Allocation Overview Reading Companion: Reference 2.4.4-5 Textbook:

More information

Managed runtimes & garbage collection

Managed runtimes & garbage collection Managed runtimes Advantages? Managed runtimes & garbage collection CSE 631 Some slides by Kathryn McKinley Disadvantages? 1 2 Managed runtimes Portability (& performance) Advantages? Reliability Security

More information

CS 167 Final Exam Solutions

CS 167 Final Exam Solutions CS 167 Final Exam Solutions Spring 2018 Do all questions. 1. [20%] This question concerns a system employing a single (single-core) processor running a Unix-like operating system, in which interrupts are

More information

Bypassing Memory Leak in Modern C++ Realm

Bypassing Memory Leak in Modern C++ Realm Annales Mathematicae et Informaticae 48 (2018) pp. 43 50 http://ami.uni-eszterhazy.hu Bypassing Memory Leak in Modern C++ Realm Dorottya Papp, Norbert Pataki Dept. of Programming Languages and Compilers,

More information

Garbage Collection (2) Advanced Operating Systems Lecture 9

Garbage Collection (2) Advanced Operating Systems Lecture 9 Garbage Collection (2) Advanced Operating Systems Lecture 9 Lecture Outline Garbage collection Generational algorithms Incremental algorithms Real-time garbage collection Practical factors 2 Object Lifetimes

More information

Garbage Collection. CS 351: Systems Programming Michael Saelee

Garbage Collection. CS 351: Systems Programming Michael Saelee Garbage Collection CS 351: Systems Programming Michael Saelee = automatic deallocation i.e., malloc, but no free! system must track status of allocated blocks free (and potentially reuse)

More information

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers

CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers CSCI-1200 Data Structures Spring 2017 Lecture 27 Garbage Collection & Smart Pointers Announcements Please fill out your course evaluations! Those of you interested in becoming an undergraduate mentor for

More information

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill Binding and Storage Björn B. Brandenburg The University of North Carolina at Chapel Hill Based in part on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts. What s

More information

Dynamic Memory Management

Dynamic Memory Management Dynamic Memory Management 1 Goals of this Lecture Help you learn about: Dynamic memory management techniques Garbage collection by the run-time system (Java) Manual deallocation by the programmer (C, C++)

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

Concurrency. Glossary

Concurrency. Glossary Glossary atomic Executing as a single unit or block of computation. An atomic section of code is said to have transactional semantics. No intermediate state for the code unit is visible outside of the

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

Garbage Collection. Weiyuan Li

Garbage Collection. Weiyuan Li Garbage Collection Weiyuan Li Why GC exactly? - Laziness - Performance - free is not free - combats memory fragmentation - More flame wars Basic concepts - Type Safety - Safe: ML, Java (not really) - Unsafe:

More information

Memory management COSC346

Memory management COSC346 Memory management COSC346 Life cycle of an object Create a reference pointer Allocate memory for the object Initialise internal data Do stuff Destroy the object Release memory 2 Constructors and destructors

More information

Creating a String Data Type in C

Creating a String Data Type in C C Programming Creating a String Data Type in C For this assignment, you will use the struct mechanism in C to implement a data type that models a character string: struct _String { char data; dynamically-allocated

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 4a Andrew Tolmach Portland State University 1994-2017 Semantics and Erroneous Programs Important part of language specification is distinguishing valid from

More information

Pointers and References

Pointers and References Steven Zeil October 2, 2013 Contents 1 References 2 2 Pointers 8 21 Working with Pointers 8 211 Memory and C++ Programs 11 212 Allocating Data 15 22 Pointers Can Be Dangerous 17 3 The Secret World of Pointers

More information

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there.

ACM Trivia Bowl. Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there. #1 ACM Trivia Bowl Thursday April 3 rd (two days from now) 7pm OLS 001 Snacks and drinks provided All are welcome! I will be there. If you are in one of the top three teams, I will give you one point of

More information

Chapter 5. Names, Bindings, and Scopes

Chapter 5. Names, Bindings, and Scopes Chapter 5 Names, Bindings, and Scopes Chapter 5 Topics Introduction Names Variables The Concept of Binding Scope Scope and Lifetime Referencing Environments Named Constants 1-2 Introduction Imperative

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 6 Some project extensions. Pointers and heap allocation. Object-oriented languages. Module systems. Memory structure Javalette restrictions Only local variables and parameters

More information

Project 0: Implementing a Hash Table

Project 0: Implementing a Hash Table Project : Implementing a Hash Table CS, Big Data Systems, Spring Goal and Motivation. The goal of Project is to help you refresh basic skills at designing and implementing data structures and algorithms.

More information

Exploiting the Behavior of Generational Garbage Collector

Exploiting the Behavior of Generational Garbage Collector Exploiting the Behavior of Generational Garbage Collector I. Introduction Zhe Xu, Jia Zhao Garbage collection is a form of automatic memory management. The garbage collector, attempts to reclaim garbage,

More information

Smart Pointers. Some slides from Internet

Smart Pointers. Some slides from Internet Smart Pointers Some slides from Internet 1 Part I: Concept Reference: Using C++11 s Smart Pointers, David Kieras, EECS Department, University of Michigan C++ Primer, Stanley B. Lippman, Jesee Lajoie, Barbara

More information

Risk Management. I.T. Mock Interview Correction

Risk Management. I.T. Mock Interview Correction Risk Management I.T. Mock Interview Correction General I.T. Questions 1) A computer can only run binary code, whose human-readable version is assembly language. Any higher-level language is only an abstraction

More information

Dynamic Memory Management

Dynamic Memory Management Dynamic Memory Management Professor Jennifer Rexford http://www.cs.princeton.edu/~jrex 1 Goals of Today s Lecture Dynamic memory management o Garbage collection by the run-time system (Java) o Manual deallocation

More information

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java)

Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) COMP 412 FALL 2017 Sustainable Memory Use Allocation & (Implicit) Deallocation (mostly in Java) Copyright 2017, Keith D. Cooper & Zoran Budimlić, all rights reserved. Students enrolled in Comp 412 at Rice

More information

Vector and Free Store (Pointers and Memory Allocation)

Vector and Free Store (Pointers and Memory Allocation) DM560 Introduction to Programming in C++ Vector and Free Store (Pointers and Memory Allocation) Marco Chiarandini Department of Mathematics & Computer Science University of Southern Denmark [Based on slides

More information

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps

Memory management has always involved tradeoffs between numerous optimization possibilities: Schemes to manage problem fall into roughly two camps Garbage Collection Garbage collection makes memory management easier for programmers by automatically reclaiming unused memory. The garbage collector in the CLR makes tradeoffs to assure reasonable performance

More information

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector.

Older geometric based addressing is called CHS for cylinder-head-sector. This triple value uniquely identifies every sector. Review: On Disk Structures At the most basic level, a HDD is a collection of individually addressable sectors or blocks that are physically distributed across the surface of the platters. Older geometric

More information

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation

Memory Management. Memory Management... Memory Management... Interface to Dynamic allocation CSc 453 Compilers and Systems Software 24 : Garbage Collection Introduction Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Dynamic Memory Management

More information

Mobile Application Programming. Objective-C Classes

Mobile Application Programming. Objective-C Classes Mobile Application Programming Objective-C Classes Custom Classes @interface Car : NSObject #import Car.h + (int) viper; - (id) initwithmodel:(int)m; @implementation Car Point position; float velocity;

More information

D Programming Language

D Programming Language Group 14 Muazam Ali Anil Ozdemir D Programming Language Introduction and Why D? It doesn t come with a religion this is written somewhere along the overview of D programming language. If you actually take

More information

Lecture 13: more class, C++ memory management

Lecture 13: more class, C++ memory management CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 13:

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c/su06 CS61C : Machine Structures Lecture #6: Memory Management CS 61C L06 Memory Management (1) 2006-07-05 Andy Carle Memory Management (1/2) Variable declaration allocates

More information

Concept as a Generalization of Class and Principles of the Concept-Oriented Programming

Concept as a Generalization of Class and Principles of the Concept-Oriented Programming Computer Science Journal of Moldova, vol.13, no.3(39), 2005 Concept as a Generalization of Class and Principles of the Concept-Oriented Programming Alexandr Savinov Abstract In the paper we describe a

More information

The Dynamic Typing Interlude

The Dynamic Typing Interlude CHAPTER 6 The Dynamic Typing Interlude In the prior chapter, we began exploring Python s core object types in depth with a look at Python numbers. We ll resume our object type tour in the next chapter,

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: Static Data Structures Outline of Lecture 18 Recap:

More information

StackVsHeap SPL/2010 SPL/20

StackVsHeap SPL/2010 SPL/20 StackVsHeap Objectives Memory management central shared resource in multiprocessing RTE memory models that are used in Java and C++ services for Java/C++ programmer from RTE (JVM / OS). Perspectives of

More information

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; : Runtime Environment Relationship between names and data objects (of target machine) Allocation & de-allocation is managed by run time support package Each execution of a procedure is an activation of the

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 3a Andrew Tolmach Portland State University 1994-2017 Binding, Scope, Storage Part of being a high-level language is letting the programmer name things: variables

More information

Lecture 13: Complex Types and Garbage Collection

Lecture 13: Complex Types and Garbage Collection Lecture 13: Complex Types and Garbage Collection COMP 524 Programming Language Concepts Stephen Olivier March 17, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts

More information