Tuesday, July 25, 2006

Clearing memory used by a process

Firstly declare this somewhere in the code

#Region "Stuff for Closing the Processes"
'# to close all the resources
Private Declare Auto Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal procHandle As IntPtr, ByVal min As Int32, ByVal max As Int32) As Boolean

Public Sub SetProcessWorkingSetSize()
Try
Dim Mem As Process
Mem = Process.GetCurrentProcess()
SetProcessWorkingSetSize(Mem.Handle, -1, -1)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
#End Region


Then call it like this from where you need to in the code

If (Environment.OSVersion.Platform = PlatformID.Win32NT) Then
SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1)
End If

No comments: