C # Memory Management Optimization practices,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.