tags:

views:

50

answers:

3

I am working on VB.net multiuser application and when ever my any form open up memory rise and after some time out of memory exception is come.

I am working on VB.net 2008 and sqlserver 2005. I have use very much of shared object so second time it memory allocation is less, but how i can reduce it when form is close or not in use. can i use Garbage collector or Dispose and how i use this functionality ?

+1  A: 

using .net Garbage collection is an automatic process, getting rid of any unused objects in memory.

A lot of memory can be "lost" to the program while waiting for garbage collection.

Normally you should leave the Garbage Collector alone. It usually does a decent job of deciding when cleaning house will be worthwhile.

But you may wish to force its hand every now and then.

There is a very simple call to clear the garbage collection.

GC.Collect()

Hope this helps.

Although, you probably have some other issues to be getting out of memory exception.

Are you dealing with imagery by chance?

jimplode
It is rarely advisable to call GC.Collect.
Brian Rasmussen
Agreed. But sometimes it can be useful, like I stated it is prob some other issue.
jimplode
GC.Collect() doesn't work.
KuldipMCA
As stated you prob have another problem, as the garbage collection is usually pretty good at cleaning up. I would try doing what Brian Rasmussen has suggested.
jimplode
+3  A: 

Are you detaching all event handlers as needed? This is a common source of memory leaks in .NET applications.

You can troubleshoot this by using tools like ANTS Memory Profiler or if you prefer a free option WinDbg+SOS is very useful (but not as easy to use).

Brian Rasmussen
no i am not detaching event handlers.
KuldipMCA
That could be the problem then. It is not a problem if both subscriber and publisher have the same lifespan, but if the publisher stays all subscribers will stay as well unless you detach the handlers.
Brian Rasmussen
but i am not using add handlers in my project so i dont need to use remove handlers.
KuldipMCA
any thing else you are talking about ?
KuldipMCA
please give me any example of this.
KuldipMCA
@KuldipMCA: It is hard to tell from your question how your application is designed, but if you have a GUI its unlikely that you don't have any event handlers.
Brian Rasmussen
I have event handlers but should i need to explicitly remove all event handler?
KuldipMCA
Yes, you may need to. Please see my comment above.
Brian Rasmussen
Also, tools like ANTS Memory Profiler and WinDbg/SOS can tell you if you have any leaks.
Brian Rasmussen
A: 

Make sure you remove all reference to an object when you don't need it anymore. (or all reference to the parent object)

If one of your active forms still uses an object you don't need, the garbage collector will assume you still need it and won't remove it from memory.

Don't forget to call .Dispose() when needed.

There's some great tool out there that can give you a hint on where your memory problem is.

the_lotus