日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【转载自codeproject】Another Look At IDisposable

發布時間:2024/8/22 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【转载自codeproject】Another Look At IDisposable 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

少了一些圖片,我懶得拷了,大家還是去源地址看吧

http://www.codeproject.com/KB/cs/idispose.aspx

極力推薦!!!!

Introduction


This is yet another article on the use of the interface class IDisposable. Essentially, the code that you are going to see here is no different than the code on MSDN or these two excellent articles:


Garbage Collection in .NET by Chris Maunder:
http://www.codeproject.com/managedcpp/garbage_collection.asp

General Guidelines for C# Class Implementation by Eddie Velasquez:
http://www.codeproject.com/csharp/csharpclassimp.asp

What's different is that this article illustrates the functional aspects of:
the destructor
the Dispose method
memory allocation
garbage collection issues

In other words, there's lots of articles showing how to implement IDisposable, but very few demonstration of why to implement IDisposable.
How To Implement IDisposable

The salient features of the code below are:
Implement the Dispose method of the IDisposable interface
Only Dispose of resources once
The implementing class requires a destructor
Prevent the GC from disposing of resources if they've already been manually disposed
Track whether the GC is disposing of the object rather than the application specifically requesting that the object is disposed. This concerns how resources that the object manages are handled.

And here's a flowchart:



Note two things:
If Dispose is used on an object, it prevents the destructor from being called and manually releases managed and unmanaged resources.
If the destructor is called, it only releases unmanaged resources. Any managed resources will be de-referenced and also (possibly) collected.

There are two problem with this, which I'll come back to later:
Using Dispose does not prevent you from continuing to interact with the object!
A managed resource may be disposed of, yet still referenced somewhere in the code!

Here's an example class implementing IDisposable, which manages a Image object and has been instrumented to illustrate the workings of the class.
Collapse
public class ClassBeingTested : IDisposable
{
private bool disposed=false;
private Image img=null;

public Image Image
{
get {return img;}
}

// the constructor

public ClassBeingTested()
{
Trace.WriteLine("ClassBeingTested: Constructor");
}

// the destructor

~ClassBeingTested()
{
Trace.WriteLine("ClassBeingTested: Destructor");
// call Dispose with false. Since we're in the

// destructor call, the managed resources will be

// disposed of anyways.

Dispose(false);
}

public void Dispose()
{
Trace.WriteLine("ClassBeingTested: Dispose");
// dispose of the managed and unmanaged resources

Dispose(true);

// tell the GC that the Finalize process no longer needs

// to be run for this object.

GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposeManagedResources)
{
// process only if mananged and unmanaged resources have

// not been disposed of.

if (!this.disposed)
{
Trace.WriteLine("ClassBeingTested: Resources not disposed");
if (disposeManagedResources)
{
Trace.WriteLine("ClassBeingTested: Disposing managed resources");
// dispose managed resources

if (img != null)
{
img.Dispose();
img=null;
}
}
// dispose unmanaged resources

Trace.WriteLine("ClassBeingTested: Disposing unmanaged resouces");
disposed=true;
}
else
{
Trace.WriteLine("ClassBeingTested: Resources already disposed");
}
}

// loading an image

public void LoadImage(string file)
{
Trace.WriteLine("ClassBeingTested: LoadImage");
img=Image.FromFile(file);
}
}
Why Implement IDisposable?

Let's put this class into a simple GUI driven test fixture that looks like this:



and we'll use two simple tools to monitor what's going:
DebugView to watch the execution: http://www.sysinternals.com/ntw2k/freeware/debugview.shtml
the Task Manager's Performance view for memory utilization.

The test is very simple, involving loading a 3MB image file several times, with the option to dispose of the object manually:
Collapse
private void Create(int n)
{
for (int i=0; i<n; i++)
{
ClassBeingTested cbt=new ClassBeingTested();
cbt.LoadImage("fish.jpg");
if (ckDisposeOption.Checked)
{
cbt.Dispose();
}
}
}

The unsuspecting fish, by the way, is a Unicorn Fish, taken at Sea World, San Diego California:



Observe what happens when I create 10 fish:



Ten fish took up 140MB, (which is odd, because the fish is only a 3MB file, so you'd think no more than 30MB would be consumed, but we won't get into THAT).

Furthermore, observe that the destructors on the objects were never invoked:



If we create 25 fish, followed by another 10, notice what happens to the time it takes to haul in the fish, as a result of heavy disk swapping:



This is now taking two seconds on average to load one fish! And did the GC start collecting garbage any time soon? No! Conversely, if we dispose of the class as soon as we're done using it (which in our test case is immediately), there is no memory hogging and no performance degradation. So, to put it mildly, it is very important to consider whether or not a class needs to implement the IDispose interface, and whether or not to manually dispose of objects.
Behind The Scenes

Let's create one fish and then force the GC to collect it. The resulting trace looks like:



Observe that in this case, the destructor was called and managed resources were not manually disposed.

Now, instead, let's create one fish with the dispose flag checked, then force the GC to collect it. The resulting trace looks like:



Observe in this case, that both managed and unmanaged resources are disposed, AND that the destructor call is suppressed.
Problems

As described above, even though an object is disposed, there is nothing preventing you from continuing to use the object and any references you may have acquired to objects that it manages, as demonstrated by this code:
Collapse
private void btnRefTest_Click(object sender, System.EventArgs e)
{
ClassBeingTested cbt=new ClassBeingTested();
cbt.LoadImage("fish.jpg");
Image img=cbt.Image;
cbt.Dispose();
Trace.WriteLine("Image size=("+img.Width.ToString()+", "+img.Height.ToString()+")");
}

Of course, the result is:


Solutions

Ideally, one would want to assert or throw an exception when:
Dispose is called and managed objects are still referenced elsewhere
methods and accessors are called after the object has been disposed

Unfortunately (as far as I know) there is no way to access the reference count on an object, so it becomes somewhat difficult to determine if a managed object is still being referenced elsewhere. Besides require that the application "release" references, the best solution is to simply not allow another object to gain a reference to an internally managed object. In other words, the code:
Collapse
public Image Image
{
get {return img;}
}

should simply not be allowed in a "managing" class. Instead, the managing class should implement all the necessary support functions that other classes require, implementing a facade to the managed object. Using this approach, the application can throw an exception if the disposed flag is true--indicating that the object is still being accessed after it has technically been disposed of.
Conclusion - Unit Testing

The reason I went through this rigmarole is that I wanted to demonstrate the inadequacies of unit testing. For example, let us assume that the test class I described above does not implement IDisposable. Here we have an excellent example of how a single test on a class and its functions will succeed wonderfully, giving the illusion that all is well with a program that uses the class. But all is not well, because the class does not provide a means for the application to dispose of its managed resources, ultimately causing the entire computer system to bog down in fragmented memory and disk swapping.

This does not mean that unit testing is bad. It does however illustrate that it is far from a complete picture, and unit testing applications such as NUnit could use considerable growth in order to help the programmer automate more complex forms of unit testing.

And that, my friends, is going to be the topic of the next article.
Downloading The Demonstration Project

I have intentionally left out the "fish.jpg", being 3MB in size. Please edit the code and use your own JPG if you wish to play with the code.
License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here
About the Author
Marc Clifton


Architect
Interacx
United States

Member

轉載于:https://www.cnblogs.com/ZJoy/archive/2010/12/17/1909601.html

總結

以上是生活随笔為你收集整理的【转载自codeproject】Another Look At IDisposable的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 自拍偷拍欧美视频 | 久久色在线 | 久久精品人人 | 日韩手机在线观看 | 最近中文字幕免费 | 91久久精品www人人做人人爽 | 成人特级毛片69免费观看 | 日韩视频一区二区三区 | 免费久久精品 | 99精品一区二区三区无码吞精 | 美腿丝袜亚洲综合 | 精品国产污污免费网站入口 | 国产区一区二区三 | 日韩大胆人体 | 美女露胸露尿口 | 欧美精品做受xxx性少妇 | 6996电视影片免费看 | 97超碰国产精品无码蜜芽 | 亚洲激情第一页 | 美痴女~美人上司北岛玲 | 黄色私人影院 | 亚洲综合欧美日韩 | 性欧美又大又长又硬 | 免费色片 | 久久精品三级视频 | 伊人自拍| 日韩视频免费观看高清完整版在线观看 | 久久久欧洲 | 日韩欧美三级在线观看 | 91视频合集| 香蕉视频二区 | 精品国产无码在线 | wwwwyoujizzcom| 三点尽露的大尺度国产 | 亚洲精品在线影院 | 精品国产一区二区三区久久久久久 | 激情开心网站 | 久久日韩精品 | 天天色视频 | 国产免费毛卡片 | 中文在线资源天堂 | 欧美精彩视频 | 久色99 | 久久人人澡 | 国产精品一线二线三线 | 日本91在线 | 国语对白做受按摩的注意事项 | 五月婷婷操 | 一曲二曲三曲在线观看中文字幕动漫 | 欧美女同在线 | 久久久久黄色片 | 亚洲在线第一页 | 日韩在线视 | 精品久久久久久久久久久久久久久 | 国产一区毛片 | 可以免费看黄的网站 | 日本人和亚洲人zjzjhd | 亚洲av无码一区二区二三区 | 日韩欧美在线看 | 七七色影院 | 少妇人妻真实偷人精品视频 | 国产精品网站入口 | 丁香久久婷婷 | 深夜小视频在线观看 | 懂色av一区二区在线播放 | 91av免费看 | 四虎影视网 | 夜夜导航| 日本成人精品视频 | 国产清纯在线 | 久久久久久一级片 | av中文在线观看 | 男女床上拍拍拍 | 欧美一级片在线视频 | 国产美女流白浆 | wwww黄色片| 97超碰福利 | 国产香蕉在线视频 | 青青国产在线视频 | 女人做爰全过程免费观看美女 | 欧美老女人性生活视频 | 久久亚洲中文字幕无码 | 理论在线视频 | 中文字幕人乱码中文字 | 丁香伊人 | 成年人福利网站 | 筱田优av | 中文字幕在线观看第一页 | 久久精品无码人妻 | 91超碰国产在线 | 中文字幕在线视频播放 | wwwxxx日本 | 日本一区二区在线看 | av黄色大片 | 黄色图片小说 | 中文在线字幕免费观看电 | 国产av无码专区亚洲av | 免费av观看网址 | 久久国产精品免费 |