How to manage. net memory

Source: Internet
Author: User

The performance bottleneck of. net is undoubtedly in memory management. Automatic memory recovery does not solve all problems, but creates performance problems. Therefore, a large number of c ++ experts do not agree to add a. net-like memory management mechanism in c ++, but there are reserved libraries to support related technologies.

Java has always said that c/c ++ is hard to manage memory and is easy to leak. However, in essence, it is not the case that the end programmer should have handled it by the Framework developer.

Since they are all programmers, why do you framework developers not see human errors?

They are experts. For most cainiao programmers, this strategy is helpful, of course. However, it is fundamentally limited to hand over to framework developers for handling. They cannot predict the Needs of the final application. They cannot come up with the most considerate solution without the specific development environment.

Therefore, do not think that memory management such as. net must be very advanced. With such a large limitation, what else can be called "No leakage "?

What is memory leakage? I want to talk about my views.

In my opinion, if the memory is not released at the moment and needs to be reused for smooth usage, it cannot be considered a good memory management.

The Management Mechanism of. net is inert and will not be released as a last resort. As a result, the memory will be used up for no reason, and the available memory of the system will be cut off if there is no end.

This is not much worse than a leak, or even worse to some extent. For example, if you leak data for a whole day, just a few megabytes, but you do not have the terminal to occupy several hundred megabytes of memory, which has a greater impact on the system performance. The so-called leakage is not the essence. The essence is how high the system is and how efficient the memory management mechanism is.

Is there a way to make the. net memory management more active? This is the content to be discussed in this article.

 

During development, performance problems often occur when framework functions are abused. For example, string is not suitable for concatenation, but programmers abuse it. For example, the solution is that we have the ability to understand the content provided by the framework and redesign a more efficient code. The reason is that we cannot forget the algorithms. The framework functions cannot be applied to specific requirements. If we leave the framework, we cannot work, the products made in this way are usually only accurate to the second and third parties. These contents do not need much, but are essential.

This is the issue of control. How to achieve more detailed control is the way to win the problem at a critical moment. In the straight road, the difference between drivers and civilians may not be very big, but turning turns reflects the real effort. We may have to spend 90% of our effort to do this 10% of the work to ensure the quality of the software.

How can we improve our control over. net managed memory?

1. Understand the. net memory management mechanism.

2. Understand the tasks required by the framework for programmers.

3. Understand the manual control methods.

 

Take the c # language as an example.

Destructor :~ By default, the class name () is not automatically generated and cannot be inherited. However, destructor do not manage the memory.. net manages the memory as an independent part of the resource, and the memory is automatically managed. Other resources are managed by the programmer through the destructor.

Because the memory is automatically managed, programmers only need to understand the third point: manual control measures.

System. GC. Collect () forces the startup of the memory garbage collection program.

The second problem is how to manage other resources except memory.

. Net adds any object that implements the destructor to a queue to be cleared. This queue will call the destructor, and resources can be released at this time.

Therefore, as long as you implement the Destructor and put the code for releasing the resource inside the destructor, you can submit it to. net to handle the remaining issues, and you do not need to worry about forgetting to clean up.

If you want him to perform this operation immediately, you can call System. GC. Collect () to start it.

The problem is ,. net is very costly to implement this mechanism. If we want to release resources and do not want to affect efficiency, and worry that we will forget to release resources, we can adopt the following design mode:

1. Implement a function to release resources. It is convenient for us to manually call.

2. Call this function in the destructor.

3. the function that releases the resource calls System. GC. supressFinalize (), the object will be taken from.. net to be released in the cleanup queue. net will not start the release mechanism.

4. You must have a flag to determine whether the resource has been released and avoid repeated resource release.

5. You must have a flag to determine whether to call System. GC. SupressFinalize (). If the programmer forgets to manually clean up, but the. net itself clears the object by calling the destructor, it is meaningless to release the object from the waiting for cleanup queue.

It can be seen that to be efficient, it is inevitable that the programmer should remember to release it manually.

 

The above is the way to manage memory and other resources. As for how to use it, my personal opinion is that as long as the resource has no utilization value, it should be released as soon as possible, rather than waiting for a slow wait. net collection mechanism.

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.