|
-
Oct 18th, 2010, 09:19 PM
#1
Thread Starter
New Member
Memory leak in events?
Hi guys,
I just found, that the following sample exposes a memory leak...or I am not understanding what is happening..
Code:
Public Class schrunzEvents
Public Event schrunz()
End Class
'put this in a simple button eventhandler and watch your process memory in the taskmanager:
For i As Integer = 0 To 1000000
Dim ee As New schrunzEvents
ee = Nothing
Next
When watching the process in perfmon you find that garbage collection happens..
Any ideas are appreciated!
Sam
-
Oct 29th, 2010, 08:08 AM
#2
Fanatic Member
Re: Memory leak in events?
THis looks like VB6.
The problem looks to be 'ee = nothing' should be 'set ee = nothing'
-
Oct 29th, 2010, 08:27 AM
#3
Re: Memory leak in events?
 Originally Posted by SchwabenProgger
Hi guys,
I just found, that the following sample exposes a memory leak...or I am not understanding what is happening..
Code:
Public Class schrunzEvents
Public Event schrunz()
End Class
'put this in a simple button eventhandler and watch your process memory in the taskmanager:
For i As Integer = 0 To 1000000
Dim ee As New schrunzEvents
ee = Nothing
Next
When watching the process in perfmon you find that garbage collection happens..
Any ideas are appreciated!
Sam
The garbage collector doesn't run all the time, which is why the memory isn't actually freed right away. Setting the variable equal to Nothing simply removes the pointer (so no one can use it anymore) but the entire thing still exists in memory until the GC comes along (at random times) and physically removes it from memory.
It's also well known that the windows task manager doesn't display the memory usage for .Net apps correctly anyways, so you're looking at wrong usage numbers.
 Originally Posted by Bill Crawley
THis looks like VB6.
The problem looks to be 'ee = nothing' should be 'set ee = nothing'
It could be either .Net or vb6, but since he's talking about garbage collection, it'd be .Net
The set keyword only exists in vb1 through vb6 for legacy coders but isn't actually needed outside of Basic/QBasic (DOS) programming.
-
Oct 29th, 2010, 09:07 AM
#4
Fanatic Member
Re: Memory leak in events?
In that case....Just force the garbage collection to take place, though this will give additional overhead.
-
Oct 31st, 2010, 09:49 PM
#5
Thread Starter
New Member
Re: Memory leak in events?
 Originally Posted by SchwabenProgger
When watching the process in perfmon you find that garbage collection happens..
Did anyone of you actually run the test code?
When I remove the event, and replace it with a string variable -> everything is fine.
Doing the same thing in C# doesn't affect the memory at all.
Crazy!? I'm using Studio 2008..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|