- 浏览: 41605 次
- 性别:
- 来自: 河北唐山
最近访客 更多访客>>
文章分类
最新评论
-
fangguojun:
在插入新记录是提到的把所有左值和右值大于5的节点加上2,这里的 ...
改进前序遍历树—–关于无限分类的问题 -
li6151770:
listeners:{
change:{
} ...
Ext实现页面表单Enter全键盘导航
越來越搞不懂,除了該不該Dispose?如果用Dispose,還需要先Close嗎?
還有Dispose會不會影響Connection Pool呢?
哪來那麼多麻煩呀,真受不了 ~__~
寫了測試程式,嘗試從結果來找到一些解答,用10個thread給它connection開開關關1000次,我想特別是針對第3點,如果Dispose會影響Pool ,那Dispose會降低很多效能才是,畢竟開關是很花資源的。
得出來的數字如下:
無 Dispose 且打開 Pooling: 00:00:01.0625000
用 Using 且打開 Pooling: 00:00:01.1718750
有 Dispose 且打開 Pooling: 00:00:01.6562500
有 Dispose 且關掉 Pooling: 00:00:43.5937500
用 Using 且關掉 Pooling: 00:00:46.3906250
無 Dispose 且關掉 Pooling: 00:00:48.0312500
心得:
1、ㄟ,有沒有 Pooling還差真多,ado.net 內建啟動算是良善之舉。
2、Dispose 似乎不會影響Pooling,雖然他比較慢一點,但也表示他有在做事吧(乖乖資源回收?),如果能像己前寫dos程式的時代,可以查對記憶體的影響就好了。
3、using 會比較快耶,是因為少做close的動作嗎?
附上測試用的code,下次哪個無聊的夜晚,再來繼續改進
1 | namespace testApp { |
2 | public partial class Form1 : Form { |
3 | public Form1() { |
4 | InitializeComponent(); |
5 | } |
6 | System.Object lockThis = new System.Object(); |
7 | |
8 | public int count = 0; |
9 | DateTime beginTime; |
10 | DateTime endTime; |
11 | int threadCount = 10; |
12 | int testTimePerThread = 1000; |
13 | int testCount; |
14 | string connStr = "server=localhost;database=testdb;uid=sa;pwd="; |
15 | string connStrWithoutPool = "server=localhost;database=testdb;uid=sa;pwd=;pooling=false"; |
16 | private void btnNoDispose_Click(object sender, EventArgs e) { |
17 | testCount = threadCount * testTimePerThread; |
18 | ArrayList threadList = null; |
19 | // test dispose and pool = true |
20 | count = 0; |
21 | threadList = new ArrayList(); |
22 | for (int i = 0; i < threadCount; i++) { |
23 | Thread thread = new Thread(new ThreadStart(DisposePoolingTrueCallback)); |
24 | threadList.Add(thread); |
25 | } |
26 | beginTime = DateTime.Now; |
27 | foreach (Thread thread in threadList) { |
28 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
29 | thread.Start(); |
30 | } |
31 | } |
32 | while (count < testCount) { |
33 | } |
34 | // test no dispose and pool = true |
35 | count = 0; |
36 | threadList = new ArrayList(); |
37 | for (int i = 0; i < threadCount; i++) { |
38 | Thread thread = new Thread(new ThreadStart(NoDisposePoolingTrueCallback)); |
39 | threadList.Add(thread); |
40 | } |
41 | beginTime = DateTime.Now; |
42 | foreach (Thread thread in threadList) { |
43 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
44 | thread.Start(); |
45 | } |
46 | } |
47 | while (count < testCount) { |
48 | } |
49 | // test dispose and pool = false |
50 | count = 0; |
51 | threadList = new ArrayList(); |
52 | for (int i = 0; i < threadCount; i++) { |
53 | Thread thread = new Thread(new ThreadStart(DisposePoolingFalseCallback)); |
54 | threadList.Add(thread); |
55 | } |
56 | beginTime = DateTime.Now; |
57 | foreach (Thread thread in threadList) { |
58 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
59 | thread.Start(); |
60 | } |
61 | } |
62 | while (count < testCount) { |
63 | } |
64 | // test no dispose and pool = false |
65 | count = 0; |
66 | threadList = new ArrayList(); |
67 | for (int i = 0; i < threadCount; i++) { |
68 | Thread thread = new Thread(new ThreadStart(NoDisposePoolingFalseCallback)); |
69 | threadList.Add(thread); |
70 | } |
71 | beginTime = DateTime.Now; |
72 | foreach (Thread thread in threadList) { |
73 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
74 | thread.Start(); |
75 | } |
76 | } |
77 | while (count < testCount) { |
78 | } |
79 | // using and pool = true |
80 | count = 0; |
81 | threadList = new ArrayList(); |
82 | for (int i = 0; i < threadCount; i++) { |
83 | Thread thread = new Thread(new ThreadStart(UsingPoolingTrueCallback)); |
84 | threadList.Add(thread); |
85 | } |
86 | beginTime = DateTime.Now; |
87 | foreach (Thread thread in threadList) { |
88 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
89 | thread.Start(); |
90 | } |
91 | } |
92 | while (count < testCount) { |
93 | } |
94 | // using and pool = false |
95 | count = 0; |
96 | threadList = new ArrayList(); |
97 | for (int i = 0; i < threadCount; i++) { |
98 | Thread thread = new Thread(new ThreadStart(UsingPoolingFalseCallback)); |
99 | threadList.Add(thread); |
100 | } |
101 | beginTime = DateTime.Now; |
102 | foreach (Thread thread in threadList) { |
103 | if (thread.ThreadState != System.Threading.ThreadState.Running) { |
104 | thread.Start(); |
105 | } |
106 | } |
107 | while (count < testCount) { |
108 | } |
109 | } |
110 | |
111 | /// <summary> |
112 | /// 有 Dispose 且打開 Pooling |
113 | /// </summary> |
114 | private void DisposePoolingTrueCallback() { |
115 | for (int i = 0; i < testTimePerThread; i++) { |
116 | SqlConnection conn = new SqlConnection(); |
117 | conn.ConnectionString = connStr; |
118 | try { |
119 | conn.Open(); |
120 | } finally { |
121 | conn.Close(); |
122 | conn.Dispose(); |
123 | } |
124 | lock (lockThis) { |
125 | count++; |
126 | if (count == testCount) { |
127 | endTime = System.DateTime.Now; |
128 | Debug.Write("有 Dispose 且打開 Pooling: " + (endTime - beginTime) + "\n"); |
129 | } |
130 | } |
131 | } |
132 | } |
133 | |
134 | /// <summary> |
135 | /// 無 Dispose 且打開 Pooling |
136 | /// </summary> |
137 | private void NoDisposePoolingTrueCallback() { |
138 | for (int i = 0; i < testTimePerThread; i++) { |
139 | SqlConnection conn = new SqlConnection(); |
140 | conn.ConnectionString = connStr; |
141 | try { |
142 | conn.Open(); |
143 | } finally { |
144 | conn.Close(); |
145 | } |
146 | lock (lockThis) { |
147 | count++; |
148 | if (count == testCount) { |
149 | endTime = System.DateTime.Now; |
150 | Debug.Write("無 Dispose 且打開 Pooling: " + (endTime - beginTime) + "\n"); |
151 | } |
152 | } |
153 | } |
154 | } |
155 | |
156 | /// <summary> |
157 | /// 有 Dispose 且關掉 Pooling |
158 | /// </summary> |
159 | private void DisposePoolingFalseCallback() { |
160 | for (int i = 0; i < testTimePerThread; i++) { |
161 | SqlConnection conn = new SqlConnection(); |
162 | conn.ConnectionString = connStrWithoutPool; |
163 | try { |
164 | conn.Open(); |
165 | } finally { |
166 | conn.Close(); |
167 | conn.Dispose(); |
168 | } |
169 | lock (lockThis) { |
170 | count++; |
171 | if (count == testCount) { |
172 | endTime = System.DateTime.Now; |
173 | //Console.Write("Bingo"); |
174 | Debug.Write("有 Dispose 且關掉 Pooling: " + (endTime - beginTime) + "\n"); |
175 | } |
176 | } |
177 | } |
178 | } |
179 | |
180 | /// <summary> |
181 | /// 無 Dispose 且關掉 Pooling |
182 | /// </summary> |
183 | private void NoDisposePoolingFalseCallback() { |
184 | for (int i = 0; i < testTimePerThread; i++) { |
185 | SqlConnection conn = new SqlConnection(); |
186 | conn.ConnectionString = connStrWithoutPool; |
187 | try { |
188 | conn.Open(); |
189 | } finally { |
190 | conn.Close(); |
191 | } |
192 | lock (lockThis) { |
193 | count++; |
194 | if (count == testCount) { |
195 | endTime = System.DateTime.Now; |
196 | //Console.Write("Bingo"); |
197 | Debug.Write("無 Dispose 且關掉 Pooling: " + (endTime - beginTime) + "\n"); |
198 | } |
199 | } |
200 | } |
201 | } |
202 | |
203 | /// <summary> |
204 | /// 用 Using 且關掉 Pooling 且打開 Pooling |
205 | /// </summary> |
206 | private void UsingPoolingTrueCallback() { |
207 | for (int i = 0; i < testTimePerThread; i++) { |
208 | using (SqlConnection conn = new SqlConnection()) { |
209 | conn.ConnectionString = connStr; |
210 | conn.Open(); |
211 | } |
212 | |
213 | lock (lockThis) { |
214 | count++; |
215 | if (count == testCount) { |
216 | endTime = System.DateTime.Now; |
217 | //Console.Write("Bingo"); |
218 | Debug.Write("用 Using 且打開 Pooling: " + (endTime - beginTime) + "\n"); |
219 | } |
220 | } |
221 | } |
222 | } |
223 | /// <summary> |
224 | /// 用 Using 且關掉 Pooling 且關掉 Pooling |
225 | /// </summary> |
226 | private void UsingPoolingFalseCallback() { |
227 | for (int i = 0; i < testTimePerThread; i++) { |
228 | using (SqlConnection conn = new SqlConnection()) { |
229 | conn.ConnectionString = connStrWithoutPool; |
230 | conn.Open(); |
231 | } |
232 | |
233 | lock (lockThis) { |
234 | count++; |
235 | if (count == testCount) { |
236 | endTime = System.DateTime.Now; |
237 | //Console.Write("Bingo"); |
238 | Debug.Write("用 Using 且關掉 Pooling: " + (endTime - beginTime) + "\n"); |
239 | } |
240 | } |
241 | } |
242 | } |
243 | } |
244 | } |
发表评论
-
在Web.Config里面保存Access数据库的连接----带server.mappath
2009-04-21 06:31 1069在Web.Config里面保存Access数据库的连接---- ... -
揭秘ASP.NET 2.0的Eval方法
2009-03-05 05:38 1676实际上Eval方法是TemplateControl的,而S ... -
使用Json.Net 1.3.1的第一个演示
2009-03-05 05:20 3325首先,要明白什么是JSON,引用json.org的一段话: ... -
Json.net简单用法
2009-03-05 05:18 1804Json.Net这个程序集可以帮我们很好的实现对象到json的 ... -
.NET环境下生成JSON的类库 - JSON.NET
2009-03-05 05:15 3029网站以后的例子都会以asp.net为主,生成JSON数据也 ... -
你必须知道的C#的25个基础概念
2009-02-27 14:02 10671.静态变量和非静态变量的区别? 静态变量:静态变量使用 st ... -
SharpNuke简明教程
2009-02-04 04:46 889序言 SharpNuke包括三个 ... -
国外的开源的CMS系统(ASP.Net_c#)
2009-02-04 04:41 23861、LudicoLudico是C#编写的居 ... -
C#开源资源大汇总
2009-02-04 04:39 1286一、AOP框架 E ...
相关推荐
只用dispose是不能关闭connection的,两者不是一回事,只用close也不能释放它所占的内存。 conn.dispose() 是销毁连接,彻底关闭。 您可能感兴趣的文章:C#基础:Dispose()、Close()、Finalize()的区别详解探讨C#中...
(这里用using或许更好)当我们开发C#代码的时候,经常碰到一个问题,有些class提供Close(),有些class提供Dispose(),那么Dispose和Close到底有什么区别? 在这里,要明确一下C#程序(或者说.NET)中的资源。简单的...
首先,Dispose和Close基本上应该是一样的。Close是为了那些不熟悉Dispose的开发者设计的。因为基本上所有的developer都知道Close是干吗的(特别是对于那些有C++背景的developer)。
.net内存回收与Dispose﹐Close﹐Finalize方法一. net的对象使用一般分为三种情况﹕1.创建对象2.使用对象3.释放对象二.创建对象1.创建对象实际分为两个步骤﹕变量类型宣告和初始化对象2.变量类型宣告(declare),如﹕ ...
connection.Close(); connection.Open(); } return connection; } } public static int ExecuteCommand(string safeSql) { SqlCommand cmd = new SqlCommand(safeSql, Connection); int result = cmd....
C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose模式的实现 C#中标准Dispose...
本文详细对比了C#中析构函数、Dispose和Close方法的区别,三者都是释放资源,本文介绍了他们各自的使用方法和使用场景,希望对大家有所帮助。
delphi dispose释放结构体指针内存泄露解决方案 内存管理 delphi内存泄漏解决方案
c#的dispose用法,相当于c++的析构函数
c#使用Close方法和Dispose方法关闭SQL数据库后再次使用Open方法连接数据库的一个测试
实现标准的Dispose模式.txt
using (SQLiteConnection connection = CreateConnection1()) { DataSet ds = new DataSet(); try { connection.Open(); SQLiteDataAdapter command = new SQLiteDataAdapter(strSQL, connection); command....
using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; ...
图片批处理工具,大小替换,非常不错,jpg ,gif ,png 可以加水印
extension method to dispose content
using (OracleConnection con = new OracleConnection(OracleConnectionStr)) { //创建数据适配器,将查询语句及连接字符串两个参数传进数据适配器 con.Open(); OracleCommand cmd = con.CreateCommand(); cmd...
triple_dispose.py