none
Release memory used by a List Of (x) after I have finished with it RRS feed

  • Question

  • Hi

    I have a tool which runs as a windows service and basically processes event log information

    Whilst it does its thing every few hours it stores data in a List Of (x) e.g.

     Dim EventViewerResultsCollection As New List(Of EventRecord)

    Now when it has finished I want to be able to release the memory my service is using

    I tried just clearing the collection after using it e.g.

     EventViewerResultsCollection.Clear()

    But looking in task manager my service is still using the 600mb of RAM and doesn't release it

    If I stop and start service then usage goes down to 3.5mb again


    Darren Rose

    Monday, February 12, 2018 6:18 PM

All replies

  • Darren,

    This code takes around (number of EventRecords * 8 + 8 bytes.) 

     Dim EventViewerResultsCollection As New List(Of EventRecord)

    It is only a reference to the in the framework in build list and for every eventrecord another reference. 

    However, it can be that with this something else is locked in memory, which you have to release first. 


    Success Cor


    Monday, February 12, 2018 6:56 PM
  • Okay, that is the only thing in my code which "stores" a lot of information - so can't think of anything else that would need releasing

    Darren Rose

    Monday, February 12, 2018 6:58 PM
  • I believe you are going against the GC (garbage collector). Have you tried iterating (for/next) the list and calling dispose on each object.

    Signature

    <HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort := True)>
    Public MustInherit Class EventRecord
    	Implements IDisposable


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 12, 2018 6:59 PM
    Moderator
  • I believe you are going against the GC (garbage collector). Have you tried iterating (for/next) the list and calling dispose on each object.

    Hi Karen

    No I haven't tried doing that as it can contain a lot of items so I just thought clearing the whole list in one go was better route

    Should I not be doing this?

    How long was the GC normally take to free up the memory used once I have cleared the list?


    Darren Rose

    Monday, February 12, 2018 7:04 PM
  • Hi Darren,

    There are a lot of triggers for memory to be cleaned as per the following document.

    https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals

    Now there are times when memory can't be released (but should not be the case here but had to mention it) if a pointer is lost (rare case in vb.net)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, February 12, 2018 7:41 PM
    Moderator
  • Okay thank you , very helpful

    It seems if I leave it for a while then service is releasing the used memory, so suspect it is when GC next runs or something, as was showing 610mb when posting, but now gone down to 59mb which is better

    Thought I needed to do it manually or force it to do it, but reading link I see that is not a good idea, and best left for it to sort itself


    Darren Rose

    Monday, February 12, 2018 8:10 PM
  • Daren,

    Keep in mind it are mostly system admins who are watching taskmanager and resource viewers. 

    The computer is most of the time not busy with not in use memory. It takes time you know. It becomes only a problem if are reported errors because of to few memory. Otherwise it is only slowing down the computer with not real sense full processing. 


    Success Cor

    Monday, February 12, 2018 8:50 PM
  • Okay make sense

    Probably me just over checking things as didn't want to role my app out to client if using too much memory or not releasing it - but seems if I just wait it sorts itself out anyway


    Darren Rose

    Monday, February 12, 2018 8:55 PM
  • Hi,

    I suppose if the service is stopped, its resource would be released from memory automatically.
    If you release its resource from memory by code, how using "Dispose" is, before stopping?
    EventViewerResultsCollection.Dispose

    Regards,

    Ashidacchi

    Tuesday, February 13, 2018 2:04 AM
  • Hi,

    I suppose if the service is stopped, its resource would be released from memory automatically.
    If you release its resource from memory by code, how using "Dispose" is, before stopping?
    EventViewerResultsCollection.Dispose

    Regards,

    Ashidacchi


    Not relevant as Dispose is not a member of a List Of collection

    Darren Rose

    Tuesday, February 13, 2018 1:55 PM