Memory leak?

C

Carlo

Hi,
I've a problem with a simple application and I don't know how to solve it.
Create a simple vb project, create a class with some member and one event,
add a timer with an interval of 10 ms. Every tick execute a "for" cycle for
many times (100 or even less), inside of it create an instance of the class
you have created and in the next line set it to "Nothing". Execute the
application and check the memory, you'll notice that memory grows more and
more. Now simply comment the line where the event is declared and memory
problems will disappears. How can be explained this behaviour? I've
converted the application in c# and the problem is not present. Starting the
application with a memory profiler a I've discovered that all memory is
occupied by "weak reference" object. This is a sample of the c# project:

public class TestClass
{
public string strName;
public bool bolCheck;
public delegate test_Handler (object sender,EventArgs args);
public xxx CheckMe;
}

.....in the tick part...
for (int i=0;i<100;i++)
{
TestClass objTestClass = new TestClass();
objTestClass = null;
}

In vb is the same, instead of declaring delegate i simple create an event:
Public Event CheckMe(sender as object, args as EventArgs)

The strange thing is that the same code written in c# works correctly, in
vb.net causes memory leak. Is there something connected with
the event keywork that i don't know?
Thank you very much for the answers.
Carlo
 
H

Herfried K. Wagner [MVP]

Carlo said:
I've a problem with a simple application and I don't know how to solve it.
Create a simple vb project, create a class with some member and one event,
add a timer with an interval of 10 ms. Every tick execute a "for" cycle
for many times (100 or even less), inside of it create an instance of the
class you have created and in the next line set it to "Nothing". Execute
the application and check the memory, you'll notice that memory grows more
and more. Now simply comment the line where the event is declared and
memory problems will disappears. How can be explained this behaviour? I've
converted the application in c# and the problem is not present. Starting
the application with a memory profiler a I've discovered that all memory
is occupied by "weak reference" object.

Do you have more details on where this 'WeakReference' instance comes into
play?

Note that memory cleanup in .NET is performed by a Garbage Collector which
will not remove objects from memory immediately after they cannot be reached
any more.
 
G

Guest

I don't think setting a class to nothing releases the class instance for
garbage collection.
 
M

Michel Posseth [MCP]

No it doesn`t , but it also shouldn`t hurt

The described problem doesn`t occur on my system by the way , so maybe it
would be nice if he posted his VB code
so we can test it

No magic here VB just creates the delegates for you in the background
 
C

Carlo

Resolved...i've looked the IL code, when i compile in debug (with
vb.net) the compiler add a line where creates a weak reference that
doesn't deallocate at runtime, instead, if you compile in release
(with the optimize flag enabled), in the IL code the weak reference is
no more created and there is not the memory leak. This problem is not
present if you use c#....so, in vb.net if you want profile memory,
compile always in "release".
Thank you
 

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