Garbage collection D is a language that fully uses garbage collection. This means that it never releases memory. You only need to assign on-demand, and then all unused memory is returned to the available memory pool by the garbage collection program.
C and C programmers are accustomed to explicitly manage memory allocation and release, which is likely to suspect the benefits and efficacy of garbage collection. The experience of new projects and garbage collection and improvement of existing projects with garbage collection design have shown that:
The procedure with garbage collection is faster. This has some violation of intuition, but its principles are:
The reference count is a common solution to solve explicit memory allocation issues. The code that is incremented and decreated when the assignment is usually one of the reasons why the program is slow. It does not increase the speed after hidden in the smart pointer. (In any case, the reference count is not a comprehensive solution because the loop reference never deletes.) The destructor is used to release the resources obtained by the object. For most classes, this resource is memory. With garbage collection, most destructor can be completely abandoned. When the object is assigned on the stack, those destructors responsible for releasing the memory becomes very important. For these functions, a mechanism must be established to cause an exception that occurs, and the destructor in each function frame is called to release the memory held by the object. If the destructor is not related, there is no special stack frame to handle exceptions, so that it will run faster. The total amount of memory management code will be less. The larger the program, the less the part of the cache; the more it is paged, the slower it is running. Garbage collection will only run when the memory becomes tense. When the memory is still and beyond, the program will run at full speed and will not take any time on the release memory. Modern garbage collection procedures are much more advanced relative to the slow garbage collection procedures in the past. Dissipation, replication collection procedures overcome the inefficiency of early tags & clearance algorithms. Modern garbage collection procedure for a tightening. The stack shrinks will reduce the number of pages of the program reference, which means that the memory access hits rate will be higher, and the exchange will be less. The procedure with garbage collection will not crash due to the accumulation of memory leakage. The garbage collection program recycles memory that is not used, so there will be no "memory leak" problem. Memory leaks will make long-term running applications take over the memory until the system crashes. The GC program has a longer-term stability. There are fewer difficult to discover the pointer bug using garbage collection procedures. This is because there is no suspension pointer to the memory that has been released. Because there is no explicit memory management code, it is impossible to have corresponding bugs. The development and commissioning of procedures with garbage collection is faster because explicit release code is not developed, commissioned, tested or maintained. The procedure with garbage collection will be significantly smaller, because it is not necessary to process the memory release exception without the code for managing memory release. Garbage collection is not a golden oil. It has the following shortcomings:
Memory collection is not predictable, so the program may be paused anywhere. The time running memory collection is not uploaded. Although its operation is usually very fast, this is not guaranteed. In addition to all threads other than collecting programs, they will stop running when they are recycled. The garbage collection procedure may leave some of the memory of this recovery. In practice, this is not a big problem, because the explicit memory recycling program usually leaks some memory, which makes them finally exhaust all internal memory, and another reason is that explicit memory recycling programs usually put memory back to their internal memory. The pool is not in the operating system in the pool. Garbage collection should be implemented as a basic operating system kernel service. However, because the reality is not the case, it has caused the procedure with garbage collection to run around the garbage collection. Although this implementation can be made into a shared DLL, it is also part of the program. These limits can be alleviated by techniques described in memory management.
How to work in garbage collection
Not completed.
The external code is looking for roots with the collaboration garbage collection program of garbage collection objects in static data segments and each thread. If the only root of the object is not in them, the collection program will release the memory it occupies.
If you want to avoid this behavior, you need
Maintain a root for objects, which is within the area of collecting program scans. Reassign an object or use the Malloc / Free using the C run time library using the store allocation program of the external code. The pointer in the pointer and garbage collection program D can be roughly divided into two categories: pointing to the pointer and other pointers of the garbage collection memory. The latter is a pointer to the C library, pointing to the pointer of the static data from the pointer, pointing to the pointer of the stack, and so on. For these pointers, the operation of the legitimate pointer in all C can be applied thereto.
However, there are some limitations for the operation of the pointer to the garbage collection pointer and reference. These restrictions will not cause too much defect, but it will make the garbage collection procedures are flexible.
Undefined behavior:
Perform the pointer to the other values or as a small tip of the pointer in the linked list in C. Do not use different or techniques to exchange the value of two pointers. Use type conversion and other tips to store pointers in non-pointer variables. Void * p;
...
INT x = cast (int) p; // error: behavior is undefined
The garbage collection program does not scan the non-pointer type when building a root set. The characteristics of specific guidelines are stored in low or high storage flags: P = Cast (VOID *) (Cast (int)); // error: behavior is not defined
The value that may point to the garbage collection stack is converted to a pointer: p = cast (void *) 12345678; // error: behavior is not defined
Do not store different "magic numbers" different from NULL into the pointer. Write the pointer value to the disk and then read from the disk to memory. Use the pointer value to calculate the column value. The garbage collection program with replication technology may be arbitrarily moving the object in memory, which will fail the hash value. Dependent the order: IF (P1 ... Therefore, once again, the garbage collection program may move the object in memory. Plus or subtract an offset is the result of the scope of the garbage collection program. Char * p = new char [10]; Char * q = p 6; // ok q = p 11; // error: behavior is not defined Q = P - 1; // Error: behavior is not defined Dependent behavior: Storage space using joint shared pointers: union u {void * ptr; int value} However, using such a combination is undefined by the result of the Cast (int). If there is a pointer to the inside of the garbage collection object object, you don't have to maintain the pointer to the beginning of the object. Char [] p = new char [10]; Char [] q = p [3..6]; // q can be used to point to the entire object, and there is no need to retain P. Programmers can do most of the tasks without pointers. The various characteristics provided make people need to explicitly use pointers in most cases, including: drinking objects, dynamic arrays, and garbage collection. The purpose of providing a pointer is to successfully connect and complete some low-level work with the C API. Collecting garbage collection procedures with garbage collection procedures cannot solve all memory release problems. For example, if a pointer to a large block of data is retained, then even if it is not used, the garbage collection program cannot be recycled. In order to solve this problem, a nice practice is a reference or pointer to NULL when it is no longer used an object. This recommendation is only available for static references or embedded in other objects. It has no significance for those references stored on the stack because the collection program does not scan some of the top of the stack, and the new stack frame will always be initialized.