[DllImport("psapi")]
public static extern bool EmptyWorkingSet(IntPtr hProcess);
Public Declare Function EmptyWorkingSet Lib "psapi.dll" (hwProc As IntPtr) As Int32
None.
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.
Please add some!
public static bool TrimWSByProcess(Process process)
{
bool _result = false; //assume failed
try
{
if (process != null)
{ //only continue if we have a process
long _workingSet = process.WorkingSet64;
//trim working set
_result = PsapiHelper.EmptyWorkingSet(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;
}
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