Memory leak

  • Thread starter Thread starter mat
  • Start date Start date
M

mat

Hello.

I am having major problems with memory leaking in my c# app.

Here's my problem:

I have an array of objects that need to be re-created every time new
information
is to be shown in them. So for each element in the array I create
(new) a new object. The problem is the 'old' contents of the array is
still in memory.
How can I get rid of the old object before creating the new one?
The class does not contain anything that contains a Dispose function
so I cannot call that.

Can anyone help?

thanks,

Mat
 
mat said:
I am having major problems with memory leaking in my c# app.

Please be more specific... give us some data which you collected with
perfmon... (",NET Memory | Commited bytes" and "Process | Private bytes")
I have an array of objects that need to be re-created every time new
information
is to be shown in them. So for each element in the array I create
(new) a new object. The problem is the 'old' contents of the array is
still in memory.

If it is not referenced anymore, then this is no problem.
But you have to make sure that the old object is not referenced from some
other "rooted" object.
How can I get rid of the old object before creating the new one?

You can´t. For this .NET uses a garbage collection.
The class does not contain anything that contains a Dispose function
so I cannot call that.

Dispose does not free the object itself...


You can force a garbage collection with

GC.Collect();
GC.WaitForPendingFinilizers();
GC.Collect();



--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
As long as your objects don't use external resources to make sure that the
objects will be picked up by garbage collector you need to release the
references. For example if you got an ArrayList with objects, you call the
Clear, or if your array is something else then iterate through elements in
array an make them null like the code below:

foreach(object obj in yourArray)
{
obj = null;
}

hope this helps

Fitim Skenderi
 
mat said:
I am having major problems with memory leaking in my c# app.

Here's my problem:

I have an array of objects that need to be re-created every time new
information
is to be shown in them. So for each element in the array I create
(new) a new object. The problem is the 'old' contents of the array is
still in memory.
How can I get rid of the old object before creating the new one?
The class does not contain anything that contains a Dispose function
so I cannot call that.

Can anyone help?

Chances are you still have a reference to the old array somewhere. What
exactly do you do with the array?
 
Fitim Skenderi said:
As long as your objects don't use external resources to make sure that the
objects will be picked up by garbage collector you need to release the
references. For example if you got an ArrayList with objects, you call the
Clear, or if your array is something else then iterate through elements in
array an make them null like the code below:

foreach(object obj in yourArray)
{
obj = null;
}

a) That won't work because the variable in foreach is readonly
b) It wouldn't work anyway because it would only set the local variable
c) It's unlikely to help anyway

It sounds like the array itself should be collectable. If it's not,
then the above would only reduce the memory leak, not eliminate it.

If it is, then something else has references to the objects referenced
in the ArrayList, so clearing the ArrayList won't do any good.
 
Jon said:
a) That won't work because the variable in foreach is readonly
b) It wouldn't work anyway because it would only set the local
variable c) It's unlikely to help anyway

replace the above code with this:

for(int i=0; i<yourArray.Length; i++)
{
yourArray = null; // or just reference some other stuff
}

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp


Do you need daily reports from your server ?
http://sourceforge.net/projects/srvreport/
 
Thanks for the prompt replies.

I am not re-newing the array itself only individual elements within.

more details:

The contents of the array are object to go into another class i made.
They contain all that is needed to draw a themselves with some text
inside. Brushes and pens are passed by reference, so they only need to
be changed in one place in order to affect all object. Some cells
contain an arraylist, which contains a number of options for that cell.
Nothing in any of the cells implemets IDispose.

I realise something is still referencing the objects. What methods can I
use to figure out what is still referencing them?

thanks again,
mat
 
Jochen Kalmbach said:
a) That won't work because the variable in foreach is readonly
b) It wouldn't work anyway because it would only set the local
variable c) It's unlikely to help anyway

replace the above code with this:

for(int i=0; i<yourArray.Length; i++)
{
yourArray = null; // or just reference some other stuff
}


And then only point c) applies - but that *does* still apply, for the
reasons you snipped.

In fact, I'd use Array.Clear (yourArray, 0, yourArray.Length) to do the
above, but there we go.
 
mat holton said:
I am not re-newing the array itself only individual elements within.

more details:

The contents of the array are object to go into another class i made.
They contain all that is needed to draw a themselves with some text
inside. Brushes and pens are passed by reference, so they only need to
be changed in one place in order to affect all object.

You need to be *very* careful about what you mean there. See
http://www.pobox.com/~skeet/csharp/parameters.html for what "pass by
reference" really means. That could be part of the problem - it's not
entirely clear given your description, I'm afraid.
Some cells
contain an arraylist, which contains a number of options for that cell.
Nothing in any of the cells implemets IDispose.
I realise something is still referencing the objects. What methods can I
use to figure out what is still referencing them?

That's hard to say without a bit more of a feel of your code, I'm
afraid.
 
more details of my code:

1. One control, containing one 'grid' class, which contains many 'cell'
object in an array.
2. The 'grid' draws onto the control by getting passed a Graphics
object.
3. Each cell is drawn in using this same Graphics object. 4. Each cell
has it's own reference to a brush and pen for drawing. (by reference, i
mean it is set from _one_ new'd object - new is only mentioned once..in
the parent grid).
5. Each cell has a reference to the 'parent' grid.
6. Strings are used to display data in each cell (could this cause a
memory leak)
7.1 Upon, re-populating the grid I null each cell in the grid's array.
7.2 Each cell is new'd with it's new data.

That's all that I think is significant.
I've a demo version of a scitech tool for profiling and it seems that
each 'cell' is only referenced by the parent 'grid' - so what else can
be causing the problem?

Thanks, once again,

mat
 
mat holton said:
more details of my code:

1. One control, containing one 'grid' class, which contains many 'cell'
object in an array.
2. The 'grid' draws onto the control by getting passed a Graphics
object.
3. Each cell is drawn in using this same Graphics object. 4. Each cell
has it's own reference to a brush and pen for drawing. (by reference, i
mean it is set from _one_ new'd object - new is only mentioned once..in
the parent grid).
5. Each cell has a reference to the 'parent' grid.
6. Strings are used to display data in each cell (could this cause a
memory leak)
7.1 Upon, re-populating the grid I null each cell in the grid's array.
7.2 Each cell is new'd with it's new data.

That's all that I think is significant.
I've a demo version of a scitech tool for profiling and it seems that
each 'cell' is only referenced by the parent 'grid' - so what else can
be causing the problem?

Have you constructed a short but complete example which demonstrates
the problem? If so, could you post it?

See http://www.pobox.com/~skeet/csharp/complete.html
 
myClass ob;

Elsewhere, preferably in a function, instantiate so that objects are
given reference only when required.

ob=new myClass();

Or, better still, declare the objects within the 'using' directive

using (ob=new myClass(i))
{
// 'i' could be a static variable.
}

At the end of the above block, the objects will not exist.


Please be more specific with your question.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top