PoshCode Logo PowerShell Code Repository

Trim Working Set for PID by SunnyChakraborty 4 years ago (modification of post by SunnyChakraborty view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/3653"></script>download | new post

Trim working set for a PID. ***********WARNING***********
!!!! Do not use in production environment before thoroughly testing and understanding the script. !!!!
Do not use this to Trim Working Set for SQL Databases, there will be data-loss.
Do not use this to Trim Working Set database for msExchange store.exe
I havent tried this on IIS w3wp.exe processes.
More NEGATIVE effects here – http://support.microsoft.com/kb/2001745

  1. ## Trim Working set
  2.  
  3. Function TrimWorkingSet {
  4. param([int] $procid)
  5. <#.NOTES
  6. AUTHOR: Sunny Chakraborty(sunnyc7@gmail.com)
  7. WEBSITE: http://tekout.wordpress.com
  8. CREATED: 9/20/2012
  9. This starts the Evil Monkey series of scripts.
  10.  
  11. .DESCRIPTION
  12. MSDN - http://msdn.microsoft.com/en-us/library/windows/desktop/ms686234(v=vs.85).aspx
  13. Trim's working set to minimum levels (-1, -1)
  14. You can give a max / min values by modifying the signature.
  15.  
  16. .WARNING
  17. ***********READ CAREFULLY***********
  18. !!!! Do not use in production environment before thoroughly testing and understanding the script. !!!!
  19.  
  20. Do not use this to Trim Working Set for SQL Databases, there will be data-loss.
  21. Do not use this to Trim Working Set database for msExchange store.exe
  22. I havent tried this on IIS w3wp.exe processes.
  23. More NEGATIVE effects here - http://support.microsoft.com/kb/2001745
  24.  
  25. I have primarily used this to trim browser WorkingSet data. Tested on firefox / iexplore / chrome. It works with no tab crashing.
  26. Trimming Working Set data, doesnt mean that browser Working Set values wont climb to their previous numbers.
  27. I have seen some IE windows go back to similar working set numbers.
  28.  
  29. However, this script is really useful for a stuck browser and freezing screeing situations.
  30. ***********READ CAREFULLY***********
  31.  
  32. .EXAMPLE
  33. TrimWorkingSet(1920)
  34. #>
  35.  
  36. $sig = @"
  37. [DllImport("kernel32.dll")]
  38. public static extern bool SetProcessWorkingSetSize( IntPtr proc, int min, int max );
  39. "@
  40.  
  41. $apptotrim = (get-process -Id $procid).Handle
  42. Add-Type -MemberDefinition $sig -Namespace User32 -Name Util -UsingNamespace System.Text -PassThru
  43. [User32.Util]::SetProcessWorkingSetSize($apptotrim,-1,-1)
  44. }
  45. TrimWorkingSet(5960)

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me