日韩性视频-久久久蜜桃-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的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 女人久久 | 一本色道久久hezyo无码 | av中文资源在线 | sm乳奴虐乳调教bdsm | 美女免费视频网站 | 精品国产一区二 | 天堂成人在线视频 | 成人黄色大片 | 亚洲视频入口 | 国产精品亚洲天堂 | 狠狠狠狠狠狠干 | 中文亚洲欧美 | 日韩欧美国产三级 | 9999在线视频| 久久99久久99精品中文字幕 | 在线观看亚洲大片短视频 | 成人在线免费视频播放 | 色网址在线 | 午夜激情视频网 | 手机在线观看av片 | 国产成人精品视频 | 熟妇人妻久久中文字幕 | 欧美极品少妇xxxxⅹ猛交 | 亚洲黄色第一页 | 动漫毛片 | 国产精品久久久免费观看 | 伊人9999 | 五月婷婷天堂 | 激情五月婷婷在线 | 美女在线观看www | 淫品色影院 | 亚洲成人精品一区 | 日本精品在线播放 | 激情片网站 | 69视频免费在线观看 | 久久av高潮av | 丰满少妇一区二区三区 | 91午夜视频在线观看 | 午夜天堂在线 | 国产成人精品无码免费看夜聊软件 | 天天噜夜夜噜 | 一本大道久久久久精品嫩草 | 超碰在线免费公开 | 999伊人| 欧美视频观看 | 午夜婷婷色 | 少妇被躁爽到高潮无码人狍大战 | 综合久久av | 欧美日韩视频免费观看 | 看黄色一级大片 | 日本黄色片一级 | 色呦呦免费视频 | 中文字幕视频在线播放 | 久热色 | 色多多视频污 | 国产乱人对白 | 99热99re6国产在线播放 | 永久免费看片在线观看 | 欧美人与性囗牲恔配 | 亚洲一区二区在线免费观看 | 日本在线三级 | 色噜噜色综合 | 精品一区二区在线视频 | 男女偷爱性视频刺激 | 成人深夜在线 | 国产精品亲子伦对白 | 免费黄色欧美 | 啪啪免费视频网站 | 日本草逼视频 | 日本久久久网站 | 伊人色区 | 李丽珍毛片 | 午夜视频欧美 | 国产伦精品一区二区三区视频1 | 成人一级视频在线观看 | 99久久精品一区二区成人 | 亚洲精品一区二区在线 | 老司机精品福利视频 | 刘玥91精选国产在线观看 | 欧美日韩精品一区二区三区视频播放 | 中文字幕首页 | 最近中文字幕在线免费观看 | 欧美 亚洲 视频 | 国产大片一区二区 | 日日爽天天 | 伊人狼人影院 | 青青草视频免费看 | 91精品国产91久久久久福利 | 日本韩国三级 | 精品一区二区三区毛片 | 精品国产乱码久久久久久牛牛 | 亚洲精品视频国产 | 开心六月婷婷 | 一级全黄裸体免费观看视频 | 亚洲国产小视频 | 天天网综合 | 国产中文字幕二区 | 欧美日韩中文字幕一区二区三区 | 精品区一区二区 |