473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

reference not garbage collcted

Hello,
I have a piece of code in which some reference to the SQLadapter object
still exists after calling it, how can I get rid of it?
I tried disposing the object, closing it, no difference. It leaves just a
small object, but this application is supposed to run for months and then a
lot of small objects add up to dozens of Mb's.
Does anyone have an idea?
Thanks
Frank

The method is available in a class that is created once.
SqlConnection _SqlConnection= new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

SqlCommand _SqlCommand = new SqlCommand(StoredProcedure,_SqlConnection);

_SqlCommand.CommandType=CommandType.StoredProcedur e;

SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand);

DataSet _DataSet= new DataSet();

foreach (string name in Parameters.Keys)

{_SqlDataAdapter.SelectCommand.Parameters.Add("@" + name,
Parameters[name]);}

try{_SqlDataAdapter.Fill(_DataSet,StoredProcedure) ;}

catch (SqlException
e){{Console.WriteLine(StoredProcedure+e.Message+e. StackTrace);}}

finally{_SqlConnection.Close();}
Nov 17 '05 #1
7 1035
Hi Frank,

Well, both the SqlDataAdapter and SqlConnection should be disposed of, and your code doesn't do either.
Dispose the dataadapter first, then the connection. You can change connection.Close to connection.Dispose as dispose closes the connection as well.

You might benefit from 'using' the objects (works like try/finally) as Dispose will then be automatically called.
using(SqlConnection _SqlConnection = new SqlConnection(...))
{
SqlCommand _SqlCommand = new SqlCommand(StoredProcedure,_SqlConnection);
_SqlCommand.CommandType=CommandType.StoredProcedur e;

using(SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand))
{
DataSet _DataSet= new DataSet();

foreach (string name in Parameters.Keys)
{
_SqlDataAdapter.SelectCommand.Parameters.Add("@" + name, Parameters[name]);
}

try
{
_SqlDataAdapter.Fill(_DataSet,StoredProcedure);
}
catch (SqlException e)
{
Console.WriteLine(StoredProcedure+e.Message+e.Stac kTrace);
}
}
}

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
Thanks Morton for your fast answer. But, it does not solve my problem. I
used the code below for in a testapplication.
It leaves a 'weakreference' to (as far as I can tell) SqlDataAdapter .
Hope someone can shed some light.
Frank

using System;

using System.Data;

using System.Data.SqlClient;

namespace sqlAdapter

{public class Class2

{public Class2(){}

public void lees(){

string ConnectString="Server= localhost; User id=ssss;password=ssss;Connect
Timeout=100;";

DataSet _DataSet= new DataSet();

string StoredProcedure="S_dddd";

using (SqlConnection _SqlConnection= new SqlConnection(ConnectString))

{SqlCommand _SqlCommand = new SqlCommand(StoredProcedure,_SqlConnection);

_SqlCommand.CommandType=CommandType.StoredProcedur e;

using (SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand))

{try

{_SqlDataAdapter.Fill(_DataSet,StoredProcedure);}

catch (SqlException e)

{Console.WriteLine(StoredProcedure+e.Message+e.Sta ckTrace);}}}}}}

"Morten Wennevik" <Mo************@hotmail.com> schreef in bericht
news:op.sqlrjba5klbvpo@stone...
Hi Frank,

Well, both the SqlDataAdapter and SqlConnection should be disposed of, and
your code doesn't do either.
Dispose the dataadapter first, then the connection. You can change
connection.Close to connection.Dispose as dispose closes the connection as
well.

You might benefit from 'using' the objects (works like try/finally) as
Dispose will then be automatically called.
using(SqlConnection _SqlConnection = new SqlConnection(...))
{
SqlCommand _SqlCommand = new SqlCommand(StoredProcedure,_SqlConnection);
_SqlCommand.CommandType=CommandType.StoredProcedur e;

using(SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand))
{
DataSet _DataSet= new DataSet();

foreach (string name in Parameters.Keys)
{
_SqlDataAdapter.SelectCommand.Parameters.Add("@" + name,
Parameters[name]);
}

try
{
_SqlDataAdapter.Fill(_DataSet,StoredProcedure);
}
catch (SqlException e)
{
Console.WriteLine(StoredProcedure+e.Message+e.Stac kTrace);
}
}
}

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 17 '05 #3
Frank wrote:
I have a piece of code in which some reference to the SQLadapter object
still exists after calling it, how can I get rid of it?
Rewrite your code, of course.
I tried disposing the object, closing it, no difference. It leaves just a
small object, but this application is supposed to run for months and then a
lot of small objects add up to dozens of Mb's.


If the SqlAdapter variable is an object field, why? Why isn't it just
a local variable in a method, that will go out of scope when the
method returns?

If the SqlAdapter variable is a local in a method that will run for
months (unlikely, but not impossible) then just set it to null after
_SqlConnection.Close. The jitter is supposed to be smart enough to
notice when variables are out of scope, but explicitly nulling locals
doesn't waste many cycles.

If the DataSet maintains a reference to the SqlAdapter, extract your
data from it, and forget the DataSet. Ditto all the other objects you
create in your snippet.

--

www.midnightbeach.com
Nov 17 '05 #4
GC is nondeterministic, when you are done using it, it's not collected right
away. for more information, you can google for it.

"Frank" wrote:
Hello,
I have a piece of code in which some reference to the SQLadapter object
still exists after calling it, how can I get rid of it?
I tried disposing the object, closing it, no difference. It leaves just a
small object, but this application is supposed to run for months and then a
lot of small objects add up to dozens of Mb's.
Does anyone have an idea?
Thanks
Frank

The method is available in a class that is created once.
SqlConnection _SqlConnection= new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

SqlCommand _SqlCommand = new SqlCommand(StoredProcedure,_SqlConnection);

_SqlCommand.CommandType=CommandType.StoredProcedur e;

SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand);

DataSet _DataSet= new DataSet();

foreach (string name in Parameters.Keys)

{_SqlDataAdapter.SelectCommand.Parameters.Add("@" + name,
Parameters[name]);}

try{_SqlDataAdapter.Fill(_DataSet,StoredProcedure) ;}

catch (SqlException
e){{Console.WriteLine(StoredProcedure+e.Message+e. StackTrace);}}

finally{_SqlConnection.Close();}

Nov 17 '05 #5
Jon,
very nice, but if you studied my example u will see that the sqladapter
variable is local. And it is still not disposed.
Regards
Frank
"Jon Shemitz" <jo*@midnightbeach.com> schreef in bericht
news:42***************@midnightbeach.com...
Frank wrote:
I have a piece of code in which some reference to the SQLadapter object
still exists after calling it, how can I get rid of it?


Rewrite your code, of course.
I tried disposing the object, closing it, no difference. It leaves just a
small object, but this application is supposed to run for months and then
a
lot of small objects add up to dozens of Mb's.


If the SqlAdapter variable is an object field, why? Why isn't it just
a local variable in a method, that will go out of scope when the
method returns?

If the SqlAdapter variable is a local in a method that will run for
months (unlikely, but not impossible) then just set it to null after
_SqlConnection.Close. The jitter is supposed to be smart enough to
notice when variables are out of scope, but explicitly nulling locals
doesn't waste many cycles.

If the DataSet maintains a reference to the SqlAdapter, extract your
data from it, and forget the DataSet. Ditto all the other objects you
create in your snippet.

--

www.midnightbeach.com

Nov 17 '05 #6
Frank wrote:
very nice, but if you studied my example u will see that the sqladapter
variable is local. And it is still not disposed.


Perhaps if you'd posted the method, I'd have seen that. You posted a
bunch of statements.
If the DataSet maintains a reference to the SqlAdapter, extract your
data from it, and forget the DataSet. Ditto all the other objects you
create in your snippet.


--

www.midnightbeach.com
Nov 17 '05 #7
Anything to find a solution (consoleapp):

using System;
using System.Data;
using System.Data.SqlClient;

namespace sqlAdapter
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
string StoredProcedure="S_choicelistItem";
while (true)
{
System.Threading.Thread.Sleep(300);
string ConnectString="Server= localhost; User
id=uniserver;password=428301pvdd;Connect Timeout=100;";
DataSet _DataSet= new DataSet();
using (SqlConnection _SqlConnection= new SqlConnection(ConnectString))
{
using (SqlCommand _SqlCommand = new
SqlCommand(StoredProcedure,_SqlConnection))
{
_SqlCommand.CommandType=CommandType.StoredProcedur e;
using (SqlDataAdapter _SqlDataAdapter = new
SqlDataAdapter(_SqlCommand))
{
try
{
_SqlDataAdapter.Fill(_DataSet,StoredProcedure);
}
catch (SqlException e)
{
Console.WriteLine(StoredProcedure+e.Message+e.Stac kTrace);
}}}}}}}}

"Jon Shemitz" <jo*@midnightbeach.com> schreef in bericht
news:42***************@midnightbeach.com...
Frank wrote:
very nice, but if you studied my example u will see that the sqladapter
variable is local. And it is still not disposed.


Perhaps if you'd posted the method, I'd have seen that. You posted a
bunch of statements.
> If the DataSet maintains a reference to the SqlAdapter, extract your
> data from it, and forget the DataSet. Ditto all the other objects you
> create in your snippet.


--

www.midnightbeach.com

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John | last post by:
Hi. I'm a newbie to Java. I have a question regarding references and lists. Suppose I have the class: public class Junk { List myList = new ArrayList(); static void justATest(DumbObject...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. It says "A garbarage...
7
by: Fred Hedges | last post by:
I'm wondering if a tool exists that will integrate with the VS 2005 debugger, such that when I set a breakpoint I can inspect a reference and see how many other references exist to that object. ...
12
by: Sam Kong | last post by:
Hi, JavaScript hides its memory structure. I know that numbers, booleans, null and undefined are value types (value is directed saved in a variable). I want to know: - How JavaScript...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.