Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell memory usage - expensive?

Tags:

powershell

I am new to powershell but has written up a few scripts running on a windows2003 server. It's definitely more powerful than cmd scripting (maybe due to me having a programming background). However, when I delve further, I noticed that:

  1. Each script launched will run under 1 powershell process, ie. you see a new powershell process for each script.
  2. the scripts I tested for memory are really simple, say, build a string or query an environment variable, then Start-Sleep for 60 sec, So nothing needy (as to memory usage). But each process takes around >30MB. Call me stingy, but as there are memory-intensive applications scheduled to run everyday, and if I need to schedule a few powershell scripts to run regularly and maybe some script running continuously as a service, I'd certainly try to keep memory consumption as low as possible. <-- This is because we recently experienced a large application failure due to lack of memory.

I have not touched on C# yet, but would anyone reckon that it sometimes may be better to write the task in C#?

Meanwhile, I've seen posts regarding memory leak in powershell. Am I right to think that the memory created by the script will be withing the process space of powershell, so that when the script terminates hence powershell terminates, the memory created get cleared?

like image 721
user1650004 Avatar asked Sep 05 '12 19:09

user1650004


1 Answers

My PowerShell.exe 2.0 by itself (not running a script) is ~30MB on XP. This shouldn't worry you much with the average memory per machine these days. Regarding memory leaks, there have been cases where people use 3rd party libraries that have memory leaks when objects arn't properly disposed of. To address those you have to manually invoke the garbage collectorusing [gc]::Collect(), but this is rare. Other times i've seen people use Get-Content to read a very large file and assign it to a variable before using it. This will take alot of memory as well. In that case you can use the pipeline to read the file portions at a time to reduce your memory footprint.

like image 114
Andy Arismendi Avatar answered Oct 09 '22 15:10

Andy Arismendi