Dispose Upload Failed: whether to immediately release the notebook during dispose

Source: Internet
Author: User
Starting from:

This article originated from the previous articleArticle[New, dispose, and open and close concepts of object connection], Xiao Meow's last description 【When an object is dispose, it does not clear the part of the notebook, but it is a standard. This part of the blank is no longer used.Until the GC worker clears it .]. However, when I read some web articles, some developers mentioned that when I call dispose, the memory was immediately released. Therefore, Xiao meow will do this operation to see what is going on.

Billing statistics:

Then, Xiao meow wrote a small console program, and then used a small class. When the class was new, he wrote a large text editor, and put the content of the text into a variable. Let this type use a large number of memories. Next, call dispose to release the object, and then call GC. Collect to immediately recycle the resource. In this process, Xiao meow uses the tool [performance] in the system to compile the program's memory usage program, so as to detect dispose, whether to release the notebook immediately.

Program example:

First, create a console program and open a [console application program] in the case, and then add a new class in the case, this kind of feature does not require the dispose function, so meow [implements system. the idisposable interface allows this class to have dispose operations, and then arrange attention. When writing a new class, take the text content. The related program is as follows:

Class: mytestobj

Imports system. iopublic class mytestobj implements system. idisposable private m_teststr as string public property teststr () as string get return m_teststr end get set (byval value as string) m_teststr = value end set end property public sub new () end sub public sub new (byval myfilename as string) m_teststr = My. computer. filesystem. readalltext (myfilename) end sub private disposedvalue as Boolean = fa LSE 'initiate multiple regions' calls 'idisposable protected overridable sub dispose (byval disposing as Boolean) if not me. disposedvalue then if disposing then 'todo: place another managed object in the queue ). M_teststr = "" end if 'todo: Put your own hosted items (unmanaged objects) 'todo: Set large memory to null. End if me. disposedvalue = true end sub # region "idisposable support" 'this program is added by Visual Basic to provide a correct and feasible mode. Public sub dispose () implements idisposable. Dispose 'do not change the program history. In the above dispose, place the purge program forward (byval values are placed at the Boolean value ). Dispose (true) GC. suppressfinalize (me) end sub # End regionend class

In the console program, use this type of difference, and then dispose and call GC. Collect for data source recycling. The program is as follows:

 
Module module1 sub main () dim otest as mytestobj try otest = new mytestobj ("xx.txt") catch ex as exception finally otest. Dispose () GC. Collect () end try end subend Module

The rest is to write a large text watermark, so that the memory usage is better than the plain text. Xiao meow writes a 23 MB text watermark.

Then the start of the zookeeper

Start the [system management tool] in the [console] To find the [reliability and performance calculator]

Then, press F11 to make the program dynamic. Then, in the school computer Editor, add a program, select process, and choose pagefilebytes ], select our process [consoleapplication1.vshost] to check the usage of the notebook.

 

The result is as follows:

Please refer to the following documents for the effect of the following:

Http://vip2.blueshop.com.tw/topcat/DEMO/Dispose_GC/Dispose_GC.html

Related program downloads, you can click here to renew

Http://vip2.blueshop.com.tw/topcat/DEMO/Dispose_GC/ConsoleApplication1.rar

From the process of shadow, we can see that after the system completes dispose, the system does not immediately release the software until GC is called. after collect.

Special attention:

The GC. Collect method is used to generate an industry system for data source recovery. HoweverPlease do not do this during programmingBecause GC. Collect is not only used in your program, but in the whole system. And this action is just written. In this process, we need to recycle all the East-West information sources.Other activities on the host will start after he finishes.. In fact, this action. NET Framework will handle itself according to the operator of the system. Mobile processing may lead to a crash of the system (think about the rescue car running on the road, the attack rate is very high, and everyone has to stop and wait for him ). Therefore, when writing a program, you only need to use dispose to enable the system to note that this notebook can be used. When necessary, the system is automatically cleared. Do not manually clear him.

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.