Garbage Collection Memory Management Under C Leonard Francis
Garbage Collection & Memory Management Under C++ Leonard Francis Student ID 0415257
The Nuts & Bolts of Memory Management • Memory Allocation – Stack – Dynamic Heap
How Heap Allocation Works · Static initialization _heapinit · _crtheap Created for Large Block · Small Block Heap initialized (SBH) · Small block Heap Threshold default set to 1016 bytes
Small Block Heap Dynamic Large Block Heap Growing Heap Dynamic There can be more than one small block heap allocations depending on the requirement. Small block heaps and Large block heaps may be scattered all over the memory space available.
Approach towards Garbage Collection • Using _Crt. Mem. Block. Header (pointer to next and previous _Crt. Mem. Block. Header) 32 bytes Data Area No. Mans. Land 4 bytes
• Using _Crt. Mem. Block. Header · Get a Hook onto the linked list of _Crt. Mem. Block. Headers by creating a dummy object on the heap. The Block header is 32 bytes prior to the data pointer. The dummy object is created by a static initialization of the garbage collection class. · Whenever Garbage Collection is invoked traverse the linked list of _Crt. Mem. Block. Headers and delete all NORMAL_BLOCKS i. e. ones created by the user program. Do not delete the dummy object. • On Program termination, the static Garbage Collection objects destructor is invoked which then deletes all remaining leaked objects and the dummy object.
· Using Heap. Walk · Follow similar static initialization, which obtains the CRT heap created using Get. Process. Heaps API. · Set Small Block Heap threshold to 0 in the static initialization using _setsbhthreshold(). · When Garbage collection is invoked use Heap. Walk API to traverse through the heap and free each heap block. A heap block can be freed using Heap. Free API as Heap. Free( HANDLE h. Heap, // handle to the heap DWORD dw. Flags, // heap freeing flags LPVOID lp. Mem //pointer to memory to free);
· Using Heap. Walk · Call garbage collection through the destructor when the program exits and the destructor of the garbage collection object is invoked statically. This will clean up any remaining Heap objects.
Memory Optimizing under Object Oriented Programming • Data Alignment · Each primitive type under C/C++ have predefined alignment factor which is generally equal to the size of that primitive type. Type Alignment char Align on byte boundaries short (16 -bit) Align on even byte boundaries int and long (32 -bit) Align on 32 -bit boundaries float Align on 32 -bit boundaries double Align on 64 -bit boundaries structures Largest alignment requirement of any member unions Alignment requirement of the first member
• Layout of Objects under OO Programming · Each class/struct has a alignment factor equal to the maximum alignment factor of any of its members. Class Foo { char ch; //alignment = 1 char * str; //alignment = 4 int data_i; //alignment = 4 double data_d; //alignment = 8 } Since the strickest alignment factor is 8 the class alignment becomes 8.
• Layout of Objects under OO Programming Class Foo { char ch; //alignment = 1 char * str; //alignment = 4 int data_i; //alignment = 4 double *data_d; //alignment = 4 } In this case note that data_d is a pointer of size 4 bytes (on 32 bit platforms) and so the class alignment is 4, since the maximum alignment for any member of the class is 4.
• If a class has members as class itself each member class is treated just as any other member of that class. Class base { int base_data_i; float base_data_f; char * base_data_c; unsigned char base_data_uc; double base_data_d; }; Alignment for class base = 8 //alignment = 4 //alignment = 1 //alignment = 8
class Foo { char * datat_str; int data_i; base data_base; } //alignment = 4 //alignment = 8 Hence alignment for class Foo is 8 as one of its member, data_base has an alignment of 8 which happens to be the maximum alignment factor.
· A similar rule follows if classes have base classes. The class alignment factor becomes equal to the highest alignment factor of any of its base classes or members. Class base_2 { char data_char_base 2; int data_int_base 2; }
And if class Foo were defined as Class Foo : public base, base 2 { char * datat_str; int data_I; } //alignment = 4 Then alignment factor of Foo would be equal to 8 since one of its base classes has an alignment factor of 8.
·Any member or base class is laid out following the rule that, that member or base class starts at an address which is a multiple of the alignment factor for that member. i. e. Every object is allocated at an offset such that Offset % alignment requirement = = 0
For a class structure like Class Foo : public base, base 2 { char * datat_str; int data_I; } //alignment = 4 the layout will be base, alignment = 8 , offset = 0 base 2, alignment = 4 , offset =24 Members of Foo
• Layout of Classes int base_data_i; float base_data_f; char * base_data_c; unsigned char base_data_uc; double base_data_d; alignment = 4, offset = 0, size = 4 alignment = 4, offset = 4, size = 4 alignment = 4, offset = 8, size = 4 alignment = 1, offset = 12, size = 1 alignment = 8, offset = 16, size = 8 base char data_char_base 2; int data_int_base 2; alignment = 1, offset = 24, size = 1 alignment = 4, offset = 28, size = 4 base 2 char * datat_str; int data_I; alignment = 4, offset = 32, size = 4 alignment = 4, offset = 36, size = 4 Foo
The order in which base class are laid out follows the rules • Base classes are laid out in the order of declaration. • Classes with vtable pointers are placed before any class without vtable pointers in the order of declaration.
• Base class Layout Ordering Foo base_a base_d base_e(v) base_i(v) base_b base_f base_c base_g(v) base_h base_j : denotes ‘derived from’ relationship : denotes class has virtual function • Thus following the rules of base class layout the ordering of base classes with vtable pointers will be.
Vtable base_i base_j base_e base_d Foo and its base class layout order base_a vtable base_g base_h base_c base_f base_b Foo
- Slides: 21