user1650004
user1650004

Reputation: 131

Powershell memory usage - expensive?

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?

Upvotes: 5

Views: 5157

Answers (3)

johny why
johny why

Reputation: 2201

Powershell requires (much) more resources (RAM) than cmd so if all you need is something quick and simple, it makes more sense to use cmd.

CMD uses native Win32 calls and Powershell uses the .Net framework. Powershell takes longer to load, and can consume a lot more RAM than CMD.

"I monitored a Powershell session executing Get-ChildItem. It grew to 2.5GB (all of it private memory) after a few minutes and was no way nearly finished. CMD “dir /o-d” with a small scrollback buffer finished in about 2 minutes, and never took more than 300MB of memory."

https://qr.ae/pGmwoe

Upvotes: 1

latkin
latkin

Reputation: 16792

1 - Yes, a new process is created. The same is true when running a cmd script, vb script, or C# compiled executable.

2 - Loading the powershell host and runtime will take some non-trivial amount of memory, which will vary from system to system and version to version. It will generally be a heavier-weight process than a cmd shell or a dedicated C# exe. For those MB, you are getting the rich runtime and library support that makes Powershell so powerful.

General comments:

  • The OS allocates memory per-process. Once a process terminates, all of its memory is reclaimed. This is the general design of any modern OS, and is not specific to Powershell or even Windows.
  • If your team is running business-critical applications on hardware such that a handful of 30MB processes can cause a catastrophic failure, you have bigger problems. Opening a browser and going to Facebook will eat more memory than that.
  • In the time it takes you to figure out some arcane batch script solution, you could probably create a better solution in Powershell, and your company could afford new dedicated hardware with the savings in billable hours :-)
  • You should use the tool which is most appropriate for the job. Powershell is often the right tool, but not always. It's great for automating administrative tasks in a Windows environment (file processing, working with AD, scheduled tasks, setting permissions, etc, etc). It's less great for high-performance, heavily algorithmic tasks, or for complex coding against raw .NET APIs. For these tasks, C# would make more sense.
  • Powershell has huge backing/support from Microsoft (and a big user community!), and it's been made very clear that it is the preferred scripting environment for Windows going forward. All new server-side tech for Windows has powershell support. If you are working in admin/IT, it would be a wise investment to build up some skills in Powershell. I would never discourage someone from learning C#, but if your role is more IT than dev then Powershell will be the right tool much more often, and your colleagues are more likely to also understand it.

Upvotes: 5

Andy Arismendi
Andy Arismendi

Reputation: 52609

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.

Upvotes: 5

Related Questions