Archive for January 18th, 2008

Measure vs Count vs $i++

Suppose you need to count the number of certain objects (e.g. users in your domain) and you know that the number (and object size) is big, what’s the best way of doing that?

The first one that pops to mind is: just get the collection and check the Count property:

(Get-QADUser -SizeLimit 0).Count

The problem with that is that you are getting (and keeping) all the objects until the enumeration is over, and only check the count after that. This means that in large environments you can easily run out of memory. In my domain (which is not huge at all), this command uses 153 MB of memory. And if I go for a bigger collection by retrieving all AD objects (running Get-QADObject) I run out of memory before the command finishes.

So, let’s look for alternatives. Why don’t we just pipe the output to the Measure-Object cmdlet which by default simply counts the number:

Get-QADUser -SizeLimit 0 | Measure-Object

The cmdlet will be getting the objects through the pipeline and thus will not need to keep them all, so it should use less memory, right?

Wrong. It looks like due to lack of optimization Measure-Object is actually keeping the whole collection of real objects instead of just counting them and letting them go. Thus, it produces the exact same hit on your system and uses awful amounts of memory (hope that this gets fixed in v2! anyone from the PowerShell team reading this?)

So the right way of counting objects is actually to do that yourself by simply incrementing a counter variable:

Get-QADUser -SizeLimit 0 | ForEach-Object {$count++}

On my system this used only 27 MB instead of 153 MB used by Count and Measure-Object – a 6 time reduction!

And, the ratio is going to be even bigger if the number of object being counted grows.

Summary: standard solutions ain’t necessarily the best ones. In this particular case, manual $count++ works more optimally than standard Measure-Object. PowerShell provides multiple ways to solve the same tasks and the performance/resource-intensiveness differences between them can be huge.

For other optimization tips see also: Optimizing PowerShell Performance and Memory Consumption

Tags: , , , , , , ,


My Recent Tweets


The posts on this blog are provided “as is” with no warranties and confer no rights. The opinions expressed on this site are mine and mine alone, and do not necessarily represent those of my employer - WSO2 or anyone else for that matter. All trademarks acknowledged.

© 2007-2014 Dmitry Sotnikov

January 2008

%d bloggers like this: