Question Out of Memory exception

elie

New member
Joined
Dec 13, 2011
Messages
4
Programming Experience
1-3
Hi,
I have 2 forms frm_main and frm_search. I have an issue regarding the memory management (outOfMemory exception)

From a main form (frm_main) I am opening a new form through a button click event handler. Below is the code for this procedure

Private Sub BtnOpen_click (...) handles btnOpen.click

dim frm_mysearch as new frm_search
try
with frm_mysearch
.mdiparent = me
.show()
catch ex as exception
finally
frm_mysearch = Nothing
end with
end try
end sub

When I close the frm_mysearch form, through frm_mysearch.dispose() and frm_mysearch = nothing, I always realize that the process'
memory usage(seen in Task manager) never decreases; so opening the form multiple times leads to an outOfMemory exception.
I have tried calling the garbage collector after disposing the form, but the problem persisted.

Your help is appreciated.

Regards
 
Do you actually get OutOfMemoryException, or just worried that it might happen? If it actually happens you have to carefully examine all objects used in code to see if any disposable object is left without disposing it.

About closing form, for MDI form is automatically disposed on close, unless it is in hidden state at closing time, only then you need to explicitly call Dispose before releasing last reference.

Disposing only means freeing unmananged resources and that memory is free for deallocation once last reference to object is released, not that deallocations need happen at that time. Automatic memory management may keep allocations for later use for performance reasons, but that will never cause OutOfMemoryException.
 
Hi JohnH,
First I wish to thank you for your reply.
Second, regarding the form; I implemented an algorithm that opens and disposes instances of frm_mysearch for many times.
During the process, I realized that the memory was growing constantly, until it reached 1.5G and only then, the OutOfmemoryexception occured.
So I think that memory management is not deallocating memory. I am sure that all references are released (actually I am using a profiler to track memory leaks).
During my search I found similar problems that were related to memory fragmentation, and on some posts ( C# forums) people were talking about "Unsubscribing forms" from event handlers, so I was wondering if this could be a solution.
 
people were talking about "Unsubscribing forms" from event handlers
Only if you use AddHandler you must use RemoveHandler before releasing the object. WithEvents handlers are released when the object variable points to is dereferenced from that variable.
actually I am using a profiler to track memory leaks
Then you should be seeing what kind of objects are taking up the memory.
So I think that memory management is not deallocating memory
It will always deallocate memory if that memory is requested by system and can be released.
I am sure that all references are released
It is not sufficient to release a variable that references a disposable object. If a class implements IDisposable, and as such has a Dispose method, you must always call that method before releasing the object.
 
Back
Top