C # Memory Management Optimization practices,
The series of memory optimization and imagination articles are over. Many readers may feel that after reading these articles, they all give suggestions to Microsoft and are not implemented yet. To optimize the memory, is there any skill we can use now? My answer is: yes. There are many articles on. net Memory Optimization on the Internet. I don't want to repost them one by one. Here I will only introduce two methods that I have come up with. If they are similar, it is a coincidence. Of course, I just. net amateurs have limited practical experience. The method mentioned is only obtained through theoretical analysis and has not been verified by actual tests. Therefore, it may not be correct. Readers are welcome to criticize and correct it.
1. Even the mutual reference between "junk" objects should be removed as much as possible (before it becomes garbage. Some people may think that this is unnecessary because it is all spam objects and will be released in the next GC. What is the relationship between them? This idea ignores the fact that GC is performed by generation. If a 1-or 2-generation junk object retains a reference of a 0-generation junk object, the 0-generation garbage will not be recycled the next time a 0-generation GC occurs, it will not be recycled together with the garbage that references it until the first or second generation GC. This led to a much later time when the objects that were supposed to be recycled as soon as possible. The recovery of the young generation was incomplete, and enough memory could not be released, leading to more GC operations.
2. Use Weak references to cache objects. It is most suitable to use weak references to implement caching. cache objects can be automatically released based on memory pressure to achieve a balance. Saves weak references to some junk objects. You can "Pick up the baby from the garbage" at any time before GC to achieve "Waste Utilization", reducing the number of truly allocated objects, it can greatly reduce GC pressure, reduce memory usage, and increase memory usage ". I will not go into details here. I plan to write a weak reference cache class for your research.