托管代码中的内存泄漏 [英] Memory Leak in managed Code

查看:41
本文介绍了托管代码中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将创建内存泄漏!!!

使用System;

使用System.Diagnostics;

使用System.Data;

使用System.Data.SqlClient;

名称空间MemoryLeak

{

公共类RGCSqlConnection:System.IDisposable < br $>
{

SqlConnection m_connSQL;

public RGCSqlConnection(string dbName,string serverName)

{

string ServerName = serverName;

string DBName = dbName;

string m_strConn =" Connection Timeout = 45; Integrated Security = SSPI; Persist

安全信息=假;初始目录=" + dbName +" ;; Data Source =" +

serverName;

m_connSQL = new SqlConnection(m_strConn);

m_connSQL.Open();

}

#region IDisposable会员


public void Dispose()

{

m_connSQL.Dispose( );

}


#endregion

}


公共类TestClass

{

int [] a = new int [100];

~TestClass()

{

}

[STAThread]

static void Main()

{

RGCSqlConnection conn = new RGCSqlConnection(" Northwind"," localhost");

conn.Dispose();


TestClass t;

do

{

t = new TestClass();

}

while(true);

}

}


}

The following code will create memory leaks!!!
using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
namespace MemoryLeak
{
public class RGCSqlConnection:System.IDisposable
{
SqlConnection m_connSQL;
public RGCSqlConnection(string dbName, string serverName)
{
string ServerName=serverName;
string DBName=dbName;
string m_strConn="Connection Timeout=45;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=" + dbName + ";Data Source=" +
serverName;
m_connSQL= new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable Members

public void Dispose()
{
m_connSQL.Dispose();
}

#endregion
}

public class TestClass
{
int [] a=new int [100];
~TestClass()
{
}
[STAThread]
static void Main()
{
RGCSqlConnection conn = new RGCSqlConnection("Northwind","localhost");
conn.Dispose();

TestClass t;
do
{
t=new TestClass();
}
while (true);
}
}

}

推荐答案

James写道:
以下代码将创建内存泄漏!!!

使用System;
使用Sy stem.Diagnostics;
使用System.Data;
使用System.Data.SqlClient;

名称空间MemoryLeak
公共类RGCSqlConnection:System.IDisposable
{sqlConnection m_connSQL;
公共RGCSqlConnection(字符串dbName,字符串serverName)
{>字符串ServerName = serverName;
字符串DBName = dbName;
string m_strConn =" Connection Timeout = 45; Integrated Security = SSPI; Persist
Security Info = False; Initial Catalog =" + dbName +" ;; Data Source =" +
serverName;
m_connSQL = new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable成员

public void Dispose()
{
m_connSQL.Dispose();
}




公共课TestClass
{
int [] a = new int [100];
~TestClass()
{
}

[STAThread]
static void Main()
{RGCSqlConnection conn = new RGCSqlConnection(" Northwind"," localhost");
conn.Dispose();

TestClass t;

{
t = new TestClass();
}
while(true);
}
}

}
The following code will create memory leaks!!!
using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
namespace MemoryLeak
{
public class RGCSqlConnection:System.IDisposable
{
SqlConnection m_connSQL;
public RGCSqlConnection(string dbName, string serverName)
{
string ServerName=serverName;
string DBName=dbName;
string m_strConn="Connection Timeout=45;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=" + dbName + ";Data Source=" +
serverName;
m_connSQL= new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable Members

public void Dispose()
{
m_connSQL.Dispose();
}

#endregion
}

public class TestClass
{
int [] a=new int [100];
~TestClass()
{
}
[STAThread]
static void Main()
{
RGCSqlConnection conn = new RGCSqlConnection("Northwind","localhost");
conn.Dispose();

TestClass t;
do
{
t=new TestClass();
}
while (true);
}
}

}




我不认为你对内存泄漏是什么有所了解。

调用dispose只会标记来自垃圾收集的对象,

对象将被处理掉,但不会马上处理掉。要测试内存泄漏的
,运行你的应用程序,然后关闭它,看看是否有资源返回堆中。


-

Rob Schieber



I don''t think that you have an understanding of what a memory leak is.
Calling dispose will just mark the object from Garbage Collection, the
objects will eventially get disposed of, but not right away. To test
for a memory leak, run your app, then close it and see if resources are
returned to the heap.

--
Rob Schieber


我理解你所说的话。

请运行代码并查看是否记忆力上升了。我谈到的内存泄漏问题

发生在while循环中


-James


" Rob SCHIEBER"写道:
I understand what you said.
Please run the code and see if the memory goes up. The memory leak problem
that I talked about happens in the while loop

-James

"Rob Schieber" wrote:
James写道:
以下代码会造成内存泄漏!!!

使用系统;
使用System.Diagnostics;
使用System.Data;
使用System.Data.SqlClient;

名称空间MemoryLeak
公共类RGCSqlConnection:System。 IDisposable
{sqlConnection m_connSQL;
公共RGCSqlConnection(字符串dbName,字符串serverName)
{>字符串ServerName = serverName;
字符串DBName = dbName;
string m_strConn =" Connection Timeout = 45; Integrated Security = SSPI; Persist
Security Info = False; Initial Catalog =" + dbName +" ;; Data Source =" +
serverName;
m_connSQL = new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable成员

public void Dispose()
{
m_connSQL.Dispose();
}




公共课TestClass
{
int [] a = new int [100];
~TestClass()
{
}

[STAThread]
static void Main()
{RGCSqlConnection conn = new RGCSqlConnection(" Northwind"," localhost");
conn.Dispose();

TestClass t;

{
t = new TestClass();
}
while(true);
}
}

}
The following code will create memory leaks!!!
using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
namespace MemoryLeak
{
public class RGCSqlConnection:System.IDisposable
{
SqlConnection m_connSQL;
public RGCSqlConnection(string dbName, string serverName)
{
string ServerName=serverName;
string DBName=dbName;
string m_strConn="Connection Timeout=45;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=" + dbName + ";Data Source=" +
serverName;
m_connSQL= new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable Members

public void Dispose()
{
m_connSQL.Dispose();
}

#endregion
}

public class TestClass
{
int [] a=new int [100];
~TestClass()
{
}
[STAThread]
static void Main()
{
RGCSqlConnection conn = new RGCSqlConnection("Northwind","localhost");
conn.Dispose();

TestClass t;
do
{
t=new TestClass();
}
while (true);
}
}

}



我不认为你对内存泄漏是什么有所了解。
调用dispose只会标记来自垃圾收集的对象,
对象将被处理掉,但不会立即被处理掉。要测试内存泄漏,请运行您的应用程序,然后关闭它,看看是否有资源返回堆中。

-
Rob Schieber



I don''t think that you have an understanding of what a memory leak is.
Calling dispose will just mark the object from Garbage Collection, the
objects will eventially get disposed of, but not right away. To test
for a memory leak, run your app, then close it and see if resources are
returned to the heap.

--
Rob Schieber





" James" < JA *** @ discussions.microsoft.com>在消息中写道

news:58 ********************************** @ microsof t.com ...

"James" <Ja***@discussions.microsoft.com> wrote in message
news:58**********************************@microsof t.com...
以下代码将创建内存泄漏!!!

使用System;
使用System.Diagnostics;
使用System .Data;
使用System.Data.SqlClient;

名称空间MemoryLeak
公共类RGCSqlConnection:System.IDisposable
{S / S> SqlConnection m_connSQL;
public RGCSqlConnection(string dbName,string serverName)
{/>字符串ServerName = serverName;
string DBName = dbName;
string m_strConn =" Connection Timeout = 45;综合安全= SSPI;坚持
安全信息=假;初始目录=" + dbName +" ;; Data Source =" +
serverName;
m_connSQL = new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable成员

public void Dispose()
{
m_connSQL.Dispose();
}




公共课TestClass
{
int [] a = new int [100];
~TestClass()
{
}

[STAThread]
static void Main()
{RGCSqlConnection conn = new RGCSqlConnection(" Northwind"," localhost");
conn.Dispose();

TestClass t;

{
t = new TestClass();
}
while(true);
}
}

}
The following code will create memory leaks!!!
using System;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
namespace MemoryLeak
{
public class RGCSqlConnection:System.IDisposable
{
SqlConnection m_connSQL;
public RGCSqlConnection(string dbName, string serverName)
{
string ServerName=serverName;
string DBName=dbName;
string m_strConn="Connection Timeout=45;Integrated Security=SSPI;Persist
Security Info=False;Initial Catalog=" + dbName + ";Data Source=" +
serverName;
m_connSQL= new SqlConnection(m_strConn);
m_connSQL.Open();
}
#region IDisposable Members

public void Dispose()
{
m_connSQL.Dispose();
}

#endregion
}

public class TestClass
{
int [] a=new int [100];
~TestClass()
{
}
[STAThread]
static void Main()
{
RGCSqlConnection conn = new RGCSqlConnection("Northwind","localhost");
conn.Dispose();

TestClass t;
do
{
t=new TestClass();
}
while (true);
}
}

}




不,它不会,是什么让你这么想?

注意你不应该实现IDisposable,你的TestClass不应该

有一个析构函数(Finalize)。


Willy。



No it won''t, what makes you think that?
Note that you should not implement IDisposable and your TestClass should not
have a destructor (Finalize).

Willy.


这篇关于托管代码中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆