Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()
很简单,不翻译了。
If you are using COM components on your .NET code, you might be already aware of the Marshal.ReleaseComObject and Marshal.FinalReleaseComObject, which are used to release the managed reference to Runtime Callable Wrapper (RCW) of the COM object.
If both are used to release COM objects from memory, and you are unsure which one to use, then this post will help you to learn the differences between the calls
.NET Framework is always a better choice managing your .NET objects. Once you are done, and the object that you are referring to goes out of scope, Garbage Collector automatically handles it to remove from memory. But that's not the same for unmanaged COM objects. Even though the scope of the instance variable goes out of it, but the reference still stays in memory as an RCW (Runtime Callable Wrapper).
Every time a COM interface pointer enters into the Common Language Runtime (CLR), it wraps it inside an RCW. Then the RCW keeps a reference count which is then incremented every time a COM interface pointer is mapped to it.
To release that memory instance, Marshal.ReleaseComObject method is used which enables you to force the RCW and decrements the reference count. When the reference count reaches to zero, the runtime releases all its references on that unmanaged COM object. Attempting to access that object afterwards, throws you an Exception.
Marshal.FinalReleaseComObject also does the same, but with a little difference. When you call Marshal.ReleaseComObject method, it releases one RCW reference and decrements the count by one. To remove all the managed references to a COM object, you need to call the Marshal.ReleaseComObject, the following way, in a loop until it returns '0' (zero) to free up the COM object:
while (Marshal.ReleaseComObject(obj) != 0) ;
But in case of Marshal.FinalReleaseComObject, you don't have to loop in to release all the associated references to RCW. Because, the method itself performs the same internally. If you check the original implementation of the System.Runtime.InteropServices.Marshal class, you will notice the following:
public static int FinalReleaseComObject (object o)
{
while (ReleaseComObject (o) != 0) ;
return 0;
}
So, after reading this, which one will you prefer? It's definitely the FinalReleaseComObject as it internally loops through the RCW to release all the instance references, until the count becomes '0' (zero). Isn't it? Call the ReleaseComObject only if it is absolutely required to perform on a single reference.
To learn further about the above two methods, checkout the MSDN reference: Marshal.ReleaseComObject and Marshal.FinalReleaseComObject. I hope that the above post was clear and easy to understand. Don't forget to read my other posts/articles published on this blog.
其实许多人建议不要使用Marshal这个类,因为可能会发生异常情况。但如果一定要用,看完了上面的洋文,你知道该用哪一个了么?
从这里抄来的:https://www.kunal-chowdhury.com/2017/08/marshal-release-com-object.html
Marshal.ReleaseComObject() vs. Marshal.FinalReleaseComObject()的更多相关文章
- C#双面打印解决方法(打印word\excel\图片)
最近需要按顺序打印word.excel.图片,其中有的需要单面打印,有的双面.网上查了很多方法.主要集中在几个方式解决 1.word的print和excel的printout里设置单双面 2.prin ...
- .net开发Ae释放com对象的问题
本文转载自: http://www.cnblogs.com/yhlx125/archive/2011/11/22/2258543.html#2269154我的博文 http://www.cnblogs ...
- 毫秒级的时间处理上G的图片(生成缩略图)
测试环境: 测试图片(30M): 测试计时方法: Stopwatch sw1 = new Stopwatch(); sw1.Start(); //TODO...... sw1.Stop(); stri ...
- 关闭EXCEL进程
//导入Windows类库,可以获得进程ID [DllImport("User32.dll", CharSet = CharSet.Auto)] pub ...
- c#.net Excel中的数据导入到SQL数据库中
/// <summary> /// 从Excel 导入学生 /// </summary> /// <param name=&qu ...
- 向Word模板中填充数据
现在有这样的需求,给Word文档的指定位置填充上特定数据,例如我们有一个终端,用来打印员工的薪资证明,对于一个公司来说,他的薪资证明模板是固定的,变化的地方是员工姓名,部门,职位等.我们只需要将这些指 ...
- DataTable导出到Excel(.NET 4.0)
最近在论坛里又看到很多关于DataTable(DataSet)导入Excel的帖子,我也温故知新一下,用VS2010重新整理了一个Sample.这个问题简化一下就是内存数据到文件,也就是遍历赋值,只不 ...
- PDF/WORD/EXCEL 图片预览
一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...
- PDF转图片 C# with Adobe API
PDF转图片大概有十几种方式,褒贬不一,我就详细给大家说一下我认为效率最高的方式,使用Adobe官方的SDK 安装acrobat reader 9.0以上即可,勾选如下组件.
随机推荐
- Elasticsearch安装中文分词插件ik
Elasticsearch默认提供的分词器,会把每一个汉字分开,而不是我们想要的依据关键词来分词.比如: curl -XPOST "http://localhost:9200/userinf ...
- Codeforces Round #367 (Div. 2) 套题
吐槽:只能说是上分好场,可惜没打,唉 A:Beru-taxi (水题,取最小值) #include <cstdio> #include <cstring> #include & ...
- 【POJ 1958】 Strange Towers of Hanoi
[题目链接] http://poj.org/problem?id=1958 [算法] 先考虑三个塔的情况,g[i]表示在三塔情况下的移动步数,则g[i] = g[i-1] * 2 + 1 再考虑四个塔 ...
- [Swift通天遁地]三、手势与图表-(1)监听屏幕上触摸事件的各种状态
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Spring中常用的注解,你知道几个呢?
今天给大家分享下Spring中一般常用的注解都有哪些.可能很多人做了很长是了但有些还是不知道一些注解,不过没有关系,你接着往下看. Spring部分 1.声明bean的注解 @Component 组件 ...
- redis+php实现秒杀
使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是) 先将商品库存如队列 1 2 3 4 5 6 7 ...
- 【BZOJ2595_洛谷4294】[WC2008]游览计划(斯坦纳树_状压DP)
上个月写的题qwq--突然想写篇博客 题目: 洛谷4294 分析: 斯坦纳树模板题. 简单来说,斯坦纳树问题就是给定一张有边权(或点权)的无向图,要求选若干条边使图中一些选定的点连通(可以经过其他点) ...
- hdu2029
http://acm.hdu.edu.cn/showproblem.php?pid=2029 #include<stdio.h> #include<string.h> #inc ...
- vue-cli 打包优化
1. 优化打包体积 先上2个图 (上图A是优化前的各个js大小对比视图,下图B是优化后,还未完全优化完成的,不过也可以看得出来对比) 图A是3个压缩文件,包括部分图片和使用的所有js,体积都偏大 图B ...
- jq-文本框只能输入数字
<input type="text" onKeyUp="value=value.replace(/\D/g,'')" /> onKeyUp: 当输 ...