Form closes but doesn't free memory.

Is there a certain way you have to close a form to free memory up

I'M currently using formname.close but i never notice my memory go down. Any Ideas



Answer this question

Form closes but doesn't free memory.

  • Gonza981

    I agree with Spotty - go do some research - there's more than you'd care to know. Although .NET brings us many wonderful things (and Garbage Collection is one of them), it also requires us to take more care with objects. Because of the way the garbage collector 'collects' stuff, there's no way for our application to be signalled when the last reference to an object is released, except at some point in the future. Generally this isn't a problem, but could be an issue for things like database connections for example.

    One of the things you may be able to do is call the Dispose() method on all your objects (if they have them) for the form. This may tell the garbage collector to run; however, it depends upon the object. For your own objects, there's an IDisposable inerface that you can implement, which allows you to implement a Dispose() method correctly to free up any resources your object uses.

    Or, Memory is cheap; Windows is a bit on the clever side of memory managment; Don't worry about it.



  • Elizabeth Bradley

    Just as a point here - the form may remain in memory long after you have closed it. This is because .NET uses a process called the garbage collection process which will periodically come along and find all unreferable objects of which a form is one, in memory and reclaims the memory.

    So if you objects are no longer referencable in you code then eventually they will be recovered.

    When does this garbage collection process happen This is a interesting point in that it happens when the system is short on resources and needs to try and find some more - so it will run and clean up memory, It would also occur periodically although not really at pre-determined time periods.

    So why is this good, because under COM which used reference counting you could get memory leaks if something crashed and the memory not longer had a reference or you had some form of cyclic reference which would mean the memory would be formever held. The garbage collection process does not use reference counting and is searching for objects that are no longer referencable and will reclaim the memory.

    So it is probably not freed the memory as the process isnt short on memory and hence hasnt gone garbage collecting.

    If you've got loads of resources (memory) then the garbage collection process will probably run a lot less frequently as it doesnt run short as often. If you have lots of processes running - resources become more important and the process will run more frequently.

    There is loads of reference material, just do a web search on garbage collections + .NET


  • Form closes but doesn't free memory.