ACDK White Paper - Memory Management
Translation: Xue Changyu a programming language important direction is to achieve the memory management concept
.
This chapter content:
Quote count
Garbage collection
First step mark object
Second step to delete an object without external reference
Stack object
User stack
Quote count
For each ACDK class, there are 'R' as a prefix reference type.
An example of a RStringBuffer, a reference to an example similar to the StringBuffer object.
RStringBuffer SB = New StringBuffer; "ACDK");
Variable 'sb' saves a reference to a StringBuffer instance. An object can be destroyed, if he does not have more variables to save the reference to him, the reference count is an efficient and flexible memory memory usage policy
Example of the reference count:
RStringBuffer Append (Rstring S)
{
RStringBuffer SB = New StringBuffer (); // Refcount = 1
{
RStringBuffer SB2 = SB; // Refcount = 2
SB2-> append (s);
} // Refcount = 1
Return Sb; // Refcount = 2
} // Refcount = 1
Void foo ()
{
Rstring str = new string ("Hallo"); // refcount = 1
RStringBuffer SB = append (str); // str.refcount = 2,
// sb.refcount = 1
// str.refcount = 1
} // str.refcount = 0 => Destroy string
// sb.refcount = 0 => Destroy stringbuffer
Garbage collection
Garbage Recycling is an object based on such a fact that it cannot be referred to by static objects or local variable objects have been released. The disadvantage of the reference count is that the reference counter will never be NULL when the recirculation reference object. Therefore, all data structures cannot be released. The garbage collection mechanism does not have this limit.
Cycle reference:
Class a {
PUBLIC:
Robject Other;
}
{
Ra a1 = new a (); // a1-> refcount = 1
Ra a2 = new a (); // a1-> refcount = 1
A1-> Other = a2; // a2-> refcount = 2
A2-> Other = a1; // a1-> refcount = 2
}
// a1-> refcount = 1, a2-> refcount = 1
This garbage collection mechanism has been used in ACDK. He is based on 2 step tag / clear algorithms. The main idea behind the tag / clear algorithm is to find whether an object is directly or indirectly accessed.
example
The first step mark (this section does not translate because the translation is not as clear as the original text)
Object 1 Reference from An Object Outside of The Set (External Reference): Object Gets Marked.
2 IS Not Reference from an Object: 2 is not marked.
5 gets referened from 6. 6 is Inside the set: Continuing with 6
6 gets referened from 7.7 Is Inside the set: Continuing With 77 Gets Reference from 8. 8 IS Outside of the set: 7 gets marked. Back to 6.
6 gets referened from a marked object (7): 6 gets marked. Back to 5.
5 gets referened from a marked object (6): 5 gets marked.
9 gets referenced from 5: 9 gets marked.
For 10, 11 Und 12: All Objects Only Have Internal, Cyclic References. Objects do not get marked.
.
Second step to delete an object without external reference
In step 2, all objects that have not marked will be released because they don't have anything to access them. The memory area is generally pointed to the static data segment, stack and heap. If an object is not directly or indirectly static data Duan access, then they can be removed.
A disadvantage of the garbage collection mechanism is that it must know all objects and find references in a deep reference relationship, which requires considerable time. Traditional garbage collection work in the background, and the cost of synchronization of multiple threads must also be paid
Classic memory recycling tools are mainstream of the mainstream interpretation language because it must be familiar with and control objects in their operating environment.
The exact structure of StackFrame is well known, the platform of the platform's CPU register and address register inevitably uses a compiled language similar to C / C .
This two-step mechanism is a tool for ACDK memory management. The reference count is used on standard mechanisms, and garbage collection is only used in cyclic references.
The ACDK improves the negative impact of garbage collection by the following manner:
* Garbage recovery is optional
* Garbage recovery is supplemented and enhanced as reference technology.
* Heap can be managed through threads, so synchronization is not required for all threads.
* ACDK can also be generated on the stack.
* ACDK even allows you to select a memory management mechanism.
Stack object
Object references are similar to Java objects. Therefore, access invalid objects will generate empty pointer exception (nullpointerException) or use synchronization method to protect the object's concurrent access.
In order to provide a suitable operating overhead at runtime, synchronous objects are allocated in the stack.
Unlike C / C , there is no overhead of any more running moments.
Different heap objects and stack objects:
// Use the stack object T
StringBuffer SB (10); // no cost for allocating with new
For (int i = 0; i <10; i ) {
Sb.Add (i); / * no cost for checking SB == NIL
NO COST for Dispatch Virtual Function
No cost for synchronizing in add
* /
}
-------------------------------------------------- -----------------------
Void
AddSomething (RstringBuffer SB) / / Reference Object
{
SB-> Add ("Save!"); / * Cost for checking SB == NIL
Cost for Dispatch Virtual Function
NO COST for SYNCHRONIZING IN ADD * /
}
StringBuffer SB (10);
Addsomething (SR (StringBuffer, SB)); // Convert StringBuffer to RSTRINGBUFFER -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------
Void
Addsomething (StringBuffer & Sb) // Stack object
{
Sb.Add ("fast!"); / * no cost for checking SB == NIL
NO COST for Dispatch Virtual Function
NO COST for SYNCHRONIZING IN ADD * /
}
Rstringbuffer sb = getStringBufferFroMELSEWHERE ();
Addsomething (* sb); // cost for checking sb == nil
User stack
Advanced can define a heap to implement memory.
The following strategy is permissible:
* Large-capacity memory. Fast reappointment of memory, is released after all calculations, before this, even if the object is executed, it is not released.
* Real-time memory. Large-capacity memory can provide real-time capabilities for the process.
* Shared memory. Use communication between processes, synchronous mechanisms use mutex management.
* Memory map. It is easy to map the data structure in the file in the memory structure.
* ROM. Object images in read-only memory.
* Buffer pool. When using many forms of objects, the buffer pool is used to provide high performance. So the object release is only ended, but it is not released, and they can be reused by the next request.
This article is an articles related to ACDK.
Translation: Xue Changyu
2004-12