1

I am working on VB.NET multiuser application and whenever any of my forms opens up - memory rises, and after some time OutOfMemoryException is thrown.

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

5 Answers 5

3

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).

Sign up to request clarification or add additional context in comments.

8 Comments

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.
but i am not using add handlers in my project so i dont need to use remove handlers.
any thing else you are talking about ?
please give me any example of this.
@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.
|
2

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?

3 Comments

It is rarely advisable to call GC.Collect.
Agreed. But sometimes it can be useful, like I stated it is prob some other issue.
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.
2

Try this code for RAM MEMORY released :

First declare this function :

Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal dwMinimumWorkingSetSize As Int32, ByVal dwMaximumWorkingSetSize As Int32) As Int32

And this is the usage of it :

Friend Sub ReleaseMemory()
    Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
        If Environment.OSVersion.Platform = PlatformID.Win32NT Then
            SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1)
        End If
    Catch ex As Exception
        LogError(ex.ToString())
    End Try
End Sub

Comments

0

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.

Comments

-6

The best :

Public Class FreeMemory Private Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" ( _ ByVal process As IntPtr, _ ByVal minimumWorkingSetSize As Integer, _ ByVal maximumWorkingSetSize As Integer) As Integer

Public Shared Sub FlushMemory()
    GC.Collect()
    GC.WaitForPendingFinalizers()
    If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
        SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
    End If
End Sub

End Class

1 Comment

So you posted an almost duplicate answer (only difference is you removed a catch) but with only slightly better code formatting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.