reference not garbage collcted

F

Frank

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.StoredProcedure;

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();}
 
M

Morten Wennevik

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.StoredProcedure;

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.StackTrace);
}
}
}
 
F

Frank

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.StoredProcedure;

using (SqlDataAdapter _SqlDataAdapter = new SqlDataAdapter(_SqlCommand))

{try

{_SqlDataAdapter.Fill(_DataSet,StoredProcedure);}

catch (SqlException e)

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

Jon Shemitz

Frank said:
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.
 
G

Guest

GC is nondeterministic, when you are done using it, it's not collected right
away. for more information, you can google for it.
 
F

Frank

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
 
J

Jon Shemitz

Frank said:
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.
 
F

Frank

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.StoredProcedure;
using (SqlDataAdapter _SqlDataAdapter = new
SqlDataAdapter(_SqlCommand))
{
try
{
_SqlDataAdapter.Fill(_DataSet,StoredProcedure);
}
catch (SqlException e)
{
Console.WriteLine(StoredProcedure+e.Message+e.StackTrace);
}}}}}}}}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top