Hi,
I have a C# program that in principle should only use a constant amount of
memory to execute (with periodic garbage collection) and in fact does so when
executed in Mono under either Windows or Linux. However, with Microsoft's
own .NET execution environment, the program rapidly takes up more and more
memory until it dies with an out-of-memory exception.
I post an example code below, in the hope that someone with more knowledge
of C#/.NET could enlighten me on some possible causes of such leaky behavior.
Thanks.
------------------------------------------------------------------------------------------------
using System;
using System.Data.Sql Client;
using System.Security .Cryptography;
/*
* Prerequisites:
* - On (local), need to have DB named "test_db" with table named "gotera",
* or adjust connection and query strings to match own settings.
* - The table can have any schema but needs to have sufficient number of
* rows (say, 1000000) for the program to run for more than a few seconds.
*
* No leaky behavior is observed if:
* - The [STAThread] tag is removed;
* - The MD5 construction is removed; or
* - The enumeration of DB table rows is replaced with a simple loop
* such as "for (int i=0; i<100000; ++i) { ... }".
*/
class Leaky
{
[STAThread]
static int Main() {
long n_RowCnt = 0;
SqlConnection conn = new
SqlConnection(@ "server=(local) ;database=test_ db;trusted_conn ection=yes");
try {
conn.Open();
SqlCommand cmd = new SqlCommand("sel ect * from gotera", conn);
SqlDataReader reader = cmd.ExecuteRead er();
while (reader.Read()) {
++n_RowCnt;
MD5CryptoServic eProvider md5 = new MD5CryptoServic eProvider();
}
}
catch (Exception ex) {
System.Console. WriteLine(ex.Me ssage);
}
finally {
conn.Close();
}
System.Console. WriteLine("Coun ted {0} rows.", n_RowCnt);
return 0;
}
}
I have a C# program that in principle should only use a constant amount of
memory to execute (with periodic garbage collection) and in fact does so when
executed in Mono under either Windows or Linux. However, with Microsoft's
own .NET execution environment, the program rapidly takes up more and more
memory until it dies with an out-of-memory exception.
I post an example code below, in the hope that someone with more knowledge
of C#/.NET could enlighten me on some possible causes of such leaky behavior.
Thanks.
------------------------------------------------------------------------------------------------
using System;
using System.Data.Sql Client;
using System.Security .Cryptography;
/*
* Prerequisites:
* - On (local), need to have DB named "test_db" with table named "gotera",
* or adjust connection and query strings to match own settings.
* - The table can have any schema but needs to have sufficient number of
* rows (say, 1000000) for the program to run for more than a few seconds.
*
* No leaky behavior is observed if:
* - The [STAThread] tag is removed;
* - The MD5 construction is removed; or
* - The enumeration of DB table rows is replaced with a simple loop
* such as "for (int i=0; i<100000; ++i) { ... }".
*/
class Leaky
{
[STAThread]
static int Main() {
long n_RowCnt = 0;
SqlConnection conn = new
SqlConnection(@ "server=(local) ;database=test_ db;trusted_conn ection=yes");
try {
conn.Open();
SqlCommand cmd = new SqlCommand("sel ect * from gotera", conn);
SqlDataReader reader = cmd.ExecuteRead er();
while (reader.Read()) {
++n_RowCnt;
MD5CryptoServic eProvider md5 = new MD5CryptoServic eProvider();
}
}
catch (Exception ex) {
System.Console. WriteLine(ex.Me ssage);
}
finally {
conn.Close();
}
System.Console. WriteLine("Coun ted {0} rows.", n_RowCnt);
return 0;
}
}
Comment