惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
Cloudbric
Cloudbric
The Register - Security
The Register - Security
小众软件
小众软件
PCI Perspectives
PCI Perspectives
G
Google Developers Blog
AI
AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
TaoSecurity Blog
TaoSecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
F
Full Disclosure
N
Netflix TechBlog - Medium
博客园_首页
Last Week in AI
Last Week in AI
A
Arctic Wolf
B
Blog RSS Feed
J
Java Code Geeks
C
Cybersecurity and Infrastructure Security Agency CISA
I
InfoQ
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
NISL@THU
NISL@THU
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Jina AI
Jina AI
有赞技术团队
有赞技术团队
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
P
Palo Alto Networks Blog
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
Vercel News
Vercel News
博客园 - 司徒正美
Webroot Blog
Webroot Blog
Hacker News: Ask HN
Hacker News: Ask HN
A
About on SuperTechFans

博客园 - 一个春天

用ASP.NET 2.0实现AJAX风格的Web开发 Web2.0时代,RSS你会用了吗? web2.0开源代码 Web 2.0 编程思想:16条法则 Web2.0简介 解读Web 2.0流行词 什么是RSS? Web 2.0学习指南 什么是Web 2.0 biztalk中调用.NET类 C#创建快捷方式 获得Windows中系统特殊文件夹的位置 ADO.NET中连接池状态的跟踪 NET Framework的安装 DataGrid常见问题 DataGrid列宽度设置 User Control事件无法触发? ASP.NET中动态创建DataGrid的模板列 关于ASP.NET程序的发布
Dispose模式
一个春天 · 2004-05-19 · via 博客园 - 一个春天

       早就听说.net有了”垃圾收集器”,有了这个东东,程序员可以不再关心内存回收的问题了。但是在Delphi中已经养成了顺手清理对象的习惯,所以到了真的用.net来开发的时候不是非常适应,不把自己创建的对象清除掉,心里总有点慌慌的。
   后来想想,“垃圾收集器”相当于清洁工,定期来打扫卫生。但是如果我在产生垃圾(比如喝水的纸杯)的同时顺手就清理掉,不是更舒服?效率更高?
天好好研究了一番,觉得这篇文章写得不错:似乎同时也验证了我的想法。
http://www.eqccd.com/ServiceSupport/view.asp?kind=13&id=182
里边这样说:
*1.程序员们啊,千万不要忘记调用Dispose()方法! (如果有的话 ^_^)
*2.万一忘记,不要着急...还有救!!! 因为还有"垃圾回收器"帮我们自动调用析构方法!

这篇文章似乎也是这个意思:
http://www.fawcette.com/china/XmlFile.aspx?ID=132&page=1 
这是其中的一段话,似乎是翻译的文章,读起来有点拗口:
        通过使用Dispose模式可以适当地释放非内存资源,比如数据库连接、Win32 interop组件和操作系统的句柄。你不要指望垃圾收集器能够立即将资源释放掉,因为垃圾收集器是由于管制堆(Managed Heap)的内存紧张时才触发的。你可以快速消耗掉例如数据库连接等少量资源,但会给程序的扩展性造成副面影响。在不必要的时候不能实现Dispose模式,因为它可能会增加系统开销,而这在很多情况下是可以避免的。

原来这个问题早就已经有人专门讨论过了:
http://ms.mblogger.cn/rustle/posts/2471.aspx
讨论的主角主要是@蝈蝈俊,@Meyer,@Rustle
顺便帮他们总结一下讨论的结果:不正确的还请几个大侠指点:
1.在类的Finalize方法不要调用Connection的Close方法
2.可以在类的Dispose或者Close方法中调用Connection的Close方法,及时把Connection放回缓冲池

联想:
如果类中创建了比如DataSet,DataCommand,DataAdapter,如果没有释放,也应该在类的Dispose或者Close方法中释放吧?