Problems caused by. Net's garbage collection mechanism

Source: Internet
Author: User
Some time ago, I made a C # project. One of the requirements was as follows: First, I wrote some data to an MDB file, then compressed the MDB file, and finally deleted the MDB file. After the test is completed for the first time, it finds that the MDB file is not deleted occasionally. After tracking and debugging, it finds that the program throws an exception of "other processes are using this resource, the file cannot be deleted.
The process of correcting this bug is a painful process. When writing data to the MDB file, use oledbconnection to open the connection to the MDB file, and then use dataadapter to add, delete, and modify data, close the connection. The whole process is very clear, which leads to a very strange problem. The reason is basically known because some resources held by the database are not released in time. The solution is divided into two phases. In the first phase, the operations on the MDB file are changed from the sharing mode to the exclusive mode, and then the connection is checked step by step to check whether the connection is not closed, after closing the service, I tried it several times and it was normal. So I handed it over to the customer. As a result, the customer could not delete the service within a few days, so it entered the second painful stage, it has been confirmed that all connections have been closed, which makes it difficult to find other reasons. After thinking and searching, I feel the problem is.. net.
Since no similar problems have occurred before, I have not paid attention to garbage collection ,. net's garbage collection mechanism adopts the reference counting method. When an object A in the heap has 0 reference counting, that is, no variable points to it in the stack, in this case, object A is marked as recycled, but not immediately released. Therefore,. Net's garbage collection mechanism is not real-time collection. Only when the memory is insufficient will the collection mechanism release objects marked as recyclable.
In this way, we can have a reasonable explanation for the previous problem: although the MDB connection is closed at last, the resources related to the MDB File held by the connection are not released in time, in this case, deleting the MDB file will naturally cause problems.
The problem was finally solved. The solution was to call GC after adding, deleting, and modifying the MDB file. collect (); Method to notify the garbage collection mechanism and immediately perform garbage collection. However, many cool people on the Internet say that it is best not to call this method based on security considerations. I still don't know why it causes insecurity. If danale in the garden knows why, please explain it to me. Thank you first.

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.