Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than psapi, prefix the name with the module name and a period.
EmptyWorkingSet (psapi)
.
C# Signature:
[DllImport("psapi")]
public static extern bool EmptyWorkingSet(IntPtr hProcess);
VB Signature:
Public Declare Function EmptyWorkingSet Lib "psapi.dll" (hwProc As IntPtr) As Int32
System.Diagnostics.Process.MaxWorkingSet and System.Diagnostics.Process.MinWorkingSet are similar but are used to set thresholds rather than just free whatever can be freed.
C# Signature:
[DllImport("psapi")]
public static extern bool EmptyWorkingSet(long hProcess);
VB Signature:
Public Declare Function EmptyWorkingSet Lib "psapi.dll" (hwProc As IntPtr) As Int32
User-Defined Types:
None.
Notes:
Alternative Managed API:
System.Diagnostics.Process.MaxWorkingSet and System.Diagnostics.Process.MinWorkingSet are similar but are used to set thresholds rather than just free whatever can be freed.
//trim working set
_result = PsapiHelper.EmptyWorkingSet(process.Handle);
try
{
if (process != null)
{ //only continue if we have a process
long _workingSet = process.WorkingSet64;
//trim working set
_result = PsapiHelper.EmptyWorkingSet((long)process.Handle);
//check result
if (_result)
{ //sucessfully trimed
}
else
{ //failed to trim
}
}
else
{ //no process passed in
}
}
catch (Exception ex)
{ //exception occured. Do some error handling
_result = false;
throw; //throw the error in this case.
}
return _result;
}
Sample Code:
Module Module1
Sub Main()
BackAgain:
Dim MemEater$() = New String(-1){}
ReDim MemEater(&O141231232)
Dim Proc As Process = Process.GetCurrentProcess()
Print("{0:N0} Mb", ((Proc.WorkingSet64 \ 1024) \ 1024))
Call EmptyWorkingSet(Process.GetCurrentProcess().Handle)
Print("{0:N0} Mb", ((Proc.WorkingSet64 \ 1024) \ 1024)) : Print("")
Console.Write("Back ? ""Y"" or ""No""")
If Console.ReadLine().ToLower = "y" Then GoTo BackAgain Else Exit Sub
End Sub
Sub Print(format$, ParamArray args As Object)
Console.WriteLine(format, args)
End Sub
End Module
EmptyWorkingSet wherever possible moves an applications pages from RAM to the page file, therefore freeing physical RAM. Good for giving you a bit more memory for the programs you need when Windows doesn't free up memory from something that has been sitting idle for hours.
10/19/2015 1:06:05 AM - -97.87.142.55
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).