473,554 Members | 2,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,EventArg s 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 5423
"Carlo" <ca******@porta lis.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,EventArg s 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****@discuss ions.microsoft. comschreef in bericht
news:C3******** *************** ***********@mic rosoft.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,EventArg s 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
3395
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4779
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 easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to...
4
6069
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"; scope.Path.Path=@"\\pc\root\cimv2";
20
8050
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 matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
23
4520
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
8526
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 each object within my JS app to help locate my problem? Thanks
7
6921
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 the status of the machines. Upon we follow the .NET object lifetime recommendations the application is constantly consuming more memory! The problem...
3
5297
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". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
7
15677
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 lot of memory but I still had to use it. 1. After using DLLImport and seeing the memory leak I tried to load and
22
9316
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 memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few...
0
8039
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7887
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5152
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3545
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2015
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.