Results 1 to 5 of 5

Thread: Memory leak in events?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    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

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Memory leak in events?

    THis looks like VB6.

    The problem looks to be 'ee = nothing' should be 'set ee = nothing'

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Memory leak in events?

    Quote Originally Posted by SchwabenProgger View Post
    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.

    Quote Originally Posted by Bill Crawley View Post
    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.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Memory leak in events?

    In that case....Just force the garbage collection to take place, though this will give additional overhead.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Posts
    11

    Re: Memory leak in events?

    Quote Originally Posted by SchwabenProgger View Post
    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
  •  



Click Here to Expand Forum to Full Width