473,386 Members | 1,958 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,386 software developers and data experts.

Memory leak?

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
Mar 19 '07 #1
4 5394
"Carlo" <ca******@portalis.itschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Mar 19 '07 #2
I don't think setting a class to nothing releases the class instance for
garbage collection.
--
Dennis in Houston
"Carlo" wrote:
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
Mar 22 '07 #3

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
>Is there something connected with
the event keywork that i don't know?
No magic here VB just creates the delegates for you in the background
"Dennis" <De****@discussions.microsoft.comschreef in bericht
news:C3**********************************@microsof t.com...
>I don't think setting a class to nothing releases the class instance for
garbage collection.
--
Dennis in Houston
"Carlo" wrote:
>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

Mar 22 '07 #4
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

Mar 23 '07 #5

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

Similar topics

8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.