- Try to clarify the GC of. NET (1)
- Try to clarify the GC of. NET (2)
The first two articles are some of the GC's concepts and details of what it will take to write code later in this article.
Root
In the first article, the GC iterates through the surviving objects, starting with root, which is a reference to some objects, such as global objects, static objects, and so on.
If you want to reduce the number of root, then you can start from a static object, reduce static objects, after all, static objects have survived until the end of the program.
Why are global objects not considered? The reason is simple, the static object is easier to optimize, the global object of a program is not a few, if the optimization process, it is necessary to put the contents of the global object to other places, such as plug into another class, it will cause this class to swell up.
This is, of course, a method.
LOH
Previously learned that the >=85000 bytes of the object, will be placed in the heap of large objects, and in the Gen2 collection of time to recycle, and do not compress memory, memory is slightly tense, it will cause a little waste, so
We need to control the volume of the object and make it as <85000 as possible.
Gen0, Gen1, Gen2
When the GC triggers the recovery of the Gen0, then the surviving object will rise, ideally let the size of gen0 in a single recovery process, you can get the memory space, that is, the need to reduce the life cycle of an object, so that the life of the object as short as possible.
In the case of a very short lifetime, the recovery of Gen0 will fetch a large amount of memory space at a time, and the number of objects in the ascending generation is very small, then the GC will not need to request memory resources from the OS.
GC Request Memory Resource
When creating an object, the GC will request new memory resources from the OS if it has insufficient memory available, and will not return memory to the OS after the GC reclaims the memory. So as mentioned earlier in the previous section, reduce the object
Allows the OS to have more resources to provision.
Weak Reference
In GC recycling, weak reference objects will be treated as garbage, so this is used with caution, based on this feature, in some scenarios, weak reference objects can be used to determine whether it has been recycled.
If you recycle, you can use weak reference to reduce the problem of memory tension by re-applying, for example, some external resources that account for memory.
Finalize method
Remember the feature of an object that contains a finalize method? A GC is required two times before it can be recycled, and the GC's time is variable, so the unmanaged resource does not use finalize as much as possible, and in the case of tight memory, do not use it, otherwise
It takes 2 times to GC,GC the program performance is particularly poor.
Workstation, Server GC mode
Conservative resources Use workstation GC mode, a non-conservative resource with Server GC mode, and then use Cocurrent to improve GC performance and reduce program execution pending time.
Summarize
With the theory to guide the actual, need the scene to support, now many of the program's various structures and algorithms are designed to improve performance, so in order to performance, a lot of architecture or program algorithms have other auxiliary functions, is also very reasonable.
Try to clarify the GC of. NET (3)