none
Get the size of an object?

    שאלה

  • Hello everyone!

    My question is quite simple: Is there a way to find out how large an object is? I really mean the amount of memory that is needed for a particular object/variable.

    My first try was to use the Measure-Object cmdlet but it doesn't seem to be able to do what I need...

    Thanks!!
    Regards
    Christian
    יום רביעי 29 פברואר 2012 14:46

כל התגובות

  • For value-type variables, you can calculate the size based on the type, and the number of bytes it uses.  But for objects, you only have a pointer to the object in your variable, and so the actual size in memory is unknown.  The reference variable will take (I think 4 Bytes on 32 bit systems?).

    You aren't meant to worry about memory.  Windows will tell you if you are running out of virtual memory.  You need to trust that Windows will manage the memory for optimal performance.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    יום רביעי 29 פברואר 2012 14:58
  • You can get a relative idea about how complex an object is (and consequently what kind of resources it takes to create it) by exporting it with export-clixml, and looking at the size of the exported object.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום רביעי 29 פברואר 2012 15:10
  • You can get a relative idea about how complex an object is (and consequently what kind of resources it takes to create it) by exporting it with export-clixml, and looking at the size of the exported object.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    I thought about suggesting that, then thought "Oh no, jrv will pounce on me for that, better not".  But now I realise am in the Powershell forum!

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    יום רביעי 29 פברואר 2012 15:18
  • I did qualify that by saying it will give you a "relative" idea. 

    Beyond that, I think it's instructive to go look a what a serialized version of the objects you're dealing with can be if you've never done it.  ("Well, look at that! I never knew that was in there.")


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • נערך על-ידי mjolinor יום רביעי 29 פברואר 2012 15:24
    יום רביעי 29 פברואר 2012 15:23
  • "Beyond that, I think it's instructive to go look a what a serialized version of the objects you're dealing with can be if you've never done it.  ("Well, look at that! I never knew that was in there.")"

    I have no idea what you're talking about.  I still don't know "it's" there.  What?


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    יום רביעי 29 פברואר 2012 15:44
  • I've done an export-clixml on an object and found properties buried two or three levels deep that I didn't realize were there. 

    It's a quick way to get a visual idea of what kind of properties a complex object has, and what the properties of those propertes are, on down to the fidelity limit of the export.

    It's a little different than just doing a get-member, because you're looking at a serialized version of a live object, with all the values filled in.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום רביעי 29 פברואר 2012 16:15
  • For value-type variables, you can calculate the size based on the type, and the number of bytes it uses.  But for objects, you only have a pointer to the object in your variable, and so the actual size in memory is unknown.  The reference variable will take (I think 4 Bytes on 32 bit systems?).

    You aren't meant to worry about memory.  Windows will tell you if you are running out of virtual memory.  You need to trust that Windows will manage the memory for optimal performance.


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    Don't forget that the variable name is stored somewhere as well, so each statement below should consume a different amount of memory:

        $a = 1.0
        $bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 1.0

    Also, if the code is in a function, that function occupies memory as well, so the assignment statement itself consumes yet more memory simply because it exists.

    All that said, the memory used by a powershell script is not an issue until it becomes one, so I agree that memory is not generally something we should worry about.


    Al Dunbar

    יום רביעי 29 פברואר 2012 17:05
  • memory is not generally something we should worry about

       

    That seems to be the general consensus, but it seems countintuitive then that best practice is to do early filtering whenever possible, and to use select-object to pare large objects down to just the properties you'll be using before you start processing.  Maybe I'm looking at it wrong, but it seems to me that "worry about memory" is exactly what that's all about.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום רביעי 29 פברואר 2012 17:20
  • I don’t know XML stuff that well, but could you create an XST (or some other
    parser type thing) that would make it friendly and viewable? or does the XML
    vary too much to make that possible?
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום רביעי 29 פברואר 2012 18:01
  • I worry about memory, not in the sense of I want to know the exact amount,
    but I don’t want to be wasteful either... I try to slim down whenever I
    can.. and I think that should be the practice..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום רביעי 29 פברואר 2012 18:02
  • memory is not generally something we should worry about

       

    That seems to be the general consensus, but it seems countintuitive then that best practice is to do early filtering whenever possible, and to use select-object to pare large objects down to just the properties you'll be using before you start processing.  Maybe I'm looking at it wrong, but it seems to me that "worry about memory" is exactly what that's all about.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    We needn't worry about the piddly amount of memory used by long, descriptive variable names, or extensive comments and comment-based help in functions we load into memory for the same reason we generally don't need to worry about how long it will take to execute each statement.

    But where there could be potential problems in both areas is where we have deeply nested loops that will be iterated many times. If you have an algorithm that takes a second to calculate something and you only ever use it outside of all loops, that's not a big deal. But when you need to run it uncounted times in a loop, then it becomes worthwhile finding a way to reduce the time it takes. The simplest method is to pre-execute as much as possible outside the loop.

    Same goes for the memory impact of blocking a pipeline, or not using one at all. But again, it depends somewhat on the size of each element as well as the number of iterations. In some cases you might be able to limit the aggregate volume of memory consumed using techniques similar to what I suggest in the above paragraph. But in the case of Powershell, we have another tool known to go a bit easier on memory - pipelining.

    This is not to say that it is something we should use whenever possible. If it seems more natural not to, you might find a number of cases where the result is not a problem. But when it becomes a problem, converting to a pipeline may be part of the answer.

    In summary, this is not about worrying about memory so much as avoiding having to.


    Al Dunbar

    יום רביעי 29 פברואר 2012 18:32
  • I worry about memory, not in the sense of I want to know the exact amount,
    but I don’t want to be wasteful either... I try to slim down whenever I
    can.. and I think that should be the practice..

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    On the surface, a useful practice. But there needs to be a balance between the cost (i.e. time) of developing a script, and the value of an incremental improvement.

    If you have a working script that takes 30 minutes to run, but could have been pared down to 22 minutes with an additional two weeks spent on optimizing, you might think it worth doing. Well, it might be, of course. But not necessarily.

    Suppose the purpose of the script was to get an answer to a problem before the competitor found it, and you were one week ahead of them. You spend the extra 2 weeks, and your company doesn't get the answer until a week after the competitor.

    Your time is valuable to your employer. The sooner you finish one project, the sooner you can get started on the next.

    Perfectionism is not as good a thing as its name makes it sound. It can also be considered a mental illness. I'm certainly not intending to insult you by saying this. It is just a lesson I have fought hard to learn over many years myself.


    Al Dunbar

    יום רביעי 29 פברואר 2012 18:41
  • Understood. That seems to be freely mixing the ideas of the memory used by the code itself with the memory used by the objects being handled, and the ideas of process efficiency with memory utilization.

     

    I don't think anybody writes code, particularly script, as efficiently as it can possibly be written with any consistency.  You have to learn what's worth the effort to optimize and concentrate on that.  I think part of knowing that is going to have to be knowing something about the objects your script is handling, and that seems to be what the OP is trying to do.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום רביעי 29 פברואר 2012 18:45
  • I completely agree, there should be a balance, I should say I worry about it
    in the sense that I try to be aware of wasting it. releasing vars/objs and
    slimming down data sets..
     
    that I find to be just good practice, not to the point where I measure it or
    spend 2 weeks optimizing it..
     
    I did a post on this
     
    basically some guidance on walking that line and how to think about it...
     
    too many people are way to far on one side or the other... too lax / too
    strict..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום רביעי 29 פברואר 2012 18:58
  • Agree completely.  I think knowing the nature of the objects you're dealing with will help you know when some particular piece of code might warrant a little addition thought/time spent, and I think the OPs question is entirely appropriate in that context.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום רביעי 29 פברואר 2012 19:09
  • Agree completely.

    If there was really a need for a scripted application to be written as efficiently as possible, one likely scenario is to switch from a scripting language to a compiled language.

    Fortunately for us, Powershell comes with so many benefits that simplify development that there is little need or benefit in going the compiler route.


    Al Dunbar

    יום רביעי 29 פברואר 2012 20:50
  • too many people are way to far on one side or the other... too lax / too strict..

    Of course, each one of us has our own opinion on where the middle ground is, and that is usually "exactly where I am"   ;-)


    Al Dunbar

    יום רביעי 29 פברואר 2012 20:54
  • lol
    my middle ground is being aware of each end, and being able to judge (per
    project) what is right... and what I mean by too far one way or the other is
    that there are people who don’t apply that thought to the project at hand
    and just always do too much or too little...
     
    the trick is to be aware of the level of the project and code
    appropriately...
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום חמישי 01 מרץ 2012 13:47
  • I think I've started to reply to this about 5 times but never quite gotten there... Are these classes or instances of objects you want to measure?
    יום חמישי 01 מרץ 2012 14:19
  • lol
    my middle ground is being aware of each end, and being able to judge (per
    project) what is right... and what I mean by too far one way or the other is
    that there are people who don’t apply that thought to the project at hand
    and just always do too much or too little...
    the trick is to be aware of the level of the project and code
    appropriately...

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Yes, that's obviously the main trick. But the trickiest part of it is coding "appropriately". That is another general concept that we all agree is important, even though none of us can define it precisely enough that we would have the same opinions in a specific case.

    Al Dunbar

    יום חמישי 01 מרץ 2012 21:51
  • You can get the amount of memory allocated bytes with GC's GetTotalMemory static method —before and after the object is created— and compare the difference. This will only give you an approximation to the actual size, given others objects may be created or disposed in the background, but it can give you a good idea.

    This sample code uses a custom prompt function to calculate the difference between currently and previously allocated memory bytes and writes the KB amounts in the window's title. Paste the function in a new PowerShell session, then run one line at a time and check the window's title.

    function prompt {
      [Int64]$__pam, [Int64]$global:__tam = $__tam, [GC]::GetTotalMemory($true)
      [Int64]$__diff = $__tam - $__pam
      if ($__pam -ne 0) {
        $Host.UI.RawUI.WindowTitle =
          'TAM {0:n1} KB | PAM {1:n1} KB | DIFF {2:n1} KB' -f ($__tam, $__pam, $__diff |
            ForEach-Object {$_ / 1kb})
      }
    }
    
    $bigArray1 = 1..1e6                                 # set a big array
    Clear-Variable bigArray1                            # clear the bigArray1 variable
    [Int[]]$bigArray2 = 1..1e6                          # set another big array, but constrain its type
    Clear-Variable bigArray2                            # clear the bigArray2 variable
    $hugeString = "Just some text`r`n" * 1e6            # set a huge string
    Clear-Variable hugeString                           # clear the hugeString variable
    Remove-Variable bigArray1, bigArray2, hugeString    # delete test variables
    

    You can get sophisticated with this procedure and use my technique to create tied/automatic variables and have automatic code —outside the prompt function— that you could enable through a global variable. This would write its output through Write-Host instead, that way your custom prompt will not be altered. Then, when done tracking memory allocation, just disable the automatic code.


    • נערך על-ידי zx38 יום חמישי 01 מרץ 2012 23:47
    • הוצע כתשובה על-ידי mjolinor יום שישי 02 מרץ 2012 01:34
    יום חמישי 01 מרץ 2012 23:24
  • Abbreviations used in the window's title:

    TAM = Total allocated memory

    PAM = Previously allocated memory

    DIFF = Difference (approximate size of object)

    Negative values represent deallocated memory.

    Did you notice the difference when declaring the big array as an Integer array?

    יום חמישי 01 מרץ 2012 23:43
  • Very nice.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 01:33
  • Thanks, I appreciate that.

    יום שישי 02 מרץ 2012 02:03
  • Well deserved, IMHO.  You doing OK?  Saw some posts that you've not been well.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 02:28
  • I'm still hanging in there, doing better but not as well as I would like ...I have to learn. Thanks for asking.

    Here is the other version, in case anyone is interested. It works by setting a breakpoint on variable $NestedPromptLevel (this variable is read everytime you hit Enter to execute a command). In the breakpoint's Action block the code checks the value of (global) $TrackMemoryAllocation, if set to $true it calculates the memory being allocated or deallocated. It can get annoying, to disable the automatic code, set $TrackMemoryAllocation to $false:

    # break everytime $NestedPromptLevel is read, every time you hit Enter
    Set-PSBreakpoint -Variable NestedPromptLevel -Mode Read -Action {
      if ($TrackMemoryAllocation) {
        [Int64]$__pam, [Int64]$global:__tam = $__tam, [GC]::GetTotalMemory($true)
        [Int64]$__diff = if ($__pam) {$__tam - $__pam} else {0}
        Write-Host TAM ($__tam / 1kb).ToString('N1') KB -Fore Black -Back Yellow -NoNewline
        Write-Host '  ' -Back DarkGray -NoNewline
        Write-Host PAM ($__pam / 1kb).ToString('N1') KB -Fore Black -Back Green -NoNewline
        Write-Host '  ' -Back DarkGray -NoNewline
        Write-Host DIFF ($__diff / 1kb).ToString('N1') KB -Fore White -Back Blue
      }
    }
    
    # enable memory allocation tracking
    [Bool]$TrackMemoryAllocation = $true
    
    $bigArray1 = 1..1e6                              # set another big array
    Clear-Variable bigArray1                         # clear the bigArray1 variable
    [Int[]]$bigArray2 = 1..1e6                       # set a type constraied big Int32 array
    Clear-Variable bigArray2                         # clear the bigArray2 variable
    $bigArray3 = New-Object Int[] 1e6                # set a big Int32 array with zeros
    Clear-Variable bigArray3                         # clear the bigArray3 variable
    $hugeString = "Just some text`r`n" * 1e6         # set a huge string
    Clear-Variable hugeString                        # clear the hugeString variable
    
    # disable memory allocation tracking
    $TrackMemoryAllocation = $false
    
    יום שישי 02 מרץ 2012 03:15
  • One more thing, I used Clear-Variable to demonstrate memory deallocation; Remove-Variable is better if you do not need the variable around, because it also deletes it from the PS drive.

    יום שישי 02 מרץ 2012 03:23
  • Thanks. I'll have a look at that tomorrow. Need to get home and get to bed, but I want to keep reading..........

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 03:25
  • yup, it’s a moving target, and I don’t expect a definition or rules to it,
    I'd just like to know that people understand the concept and apply it...
    because they'll then at least be in the area..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 14:19
  • very nice... I was looking around in that direction... Im not sure how I
    missed that method! but good catch and nicely done!
     
    hope someone blogs that, if not I will :)
     
    maybe even wiki that under the performance one we've talked about making..
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 14:21
  • I have to admit to never having explored the garbage collector, and it appears I need to do that. 

     

    I'm starting to feel like I'm getting close to a stack overflow.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 14:39
  • I have to admit to never having explored the garbage collector, and it appears I need to do that. 

     

    I'm starting to feel like I'm getting close to a stack overflow.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Perhaps it's just a buffer overflow gone awry.  Take two reboots and call me in the morning.
    יום שישי 02 מרץ 2012 14:46
  • So, how would you put this into a function/filter to parse pipelined data?  If that's really an option.
    יום שישי 02 מרץ 2012 14:50
  •  Take two reboots and call me in the morning.

     

    Did that yesterday.  Do not install V3 Beta on top of CTP2.  Remove CTP2 first.  DAMHIKT.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 14:53
  • like a measure-command type deal, call it measure-memory? it can be done...
    not too hard really
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 15:03
  • lol RTFM!
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 15:05
  • That sounded like a volunteering comment.
    יום שישי 02 מרץ 2012 15:05
  • Thanks Justin, I appreciate it.

    By all means, blog about it.

    יום שישי 02 מרץ 2012 15:06
  • lol since Leteo offered me the blog post, and I already have an idea on how
    to do the cmd, maybe I'll do that...
     
    damn it, not going to get any work done today... :(
     
    might start the perf wiki too... include 'how to test' and show this for
    monitoring memory... hmmmmm too many thoughts not enough time...
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 15:08
  • I started putting together some thoughts on testing, but don't have it organized yet.  That testing we did on filter vs where-object seems like a good demo.  I think I've also figured out why there were references to functions acting like blocking cmdlets, and they can but don't always.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 15:13
  • well then, you carry on with that and I'll work out the memory bit. I might
    throw up the wiki and give it a structure for us to populate.
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 15:17
  • Yes, the principle is the same for pipeline. In the process block:

    1. Store allocated memory before setting the target object
    2. Store allocated memory after setting the target object
    3. Subtract currently from previously allocated memory
    4. Display or log the data

    In the end block:

    1. Display or log the data

    Or,:

    In the begin block:

    1. Store allocated memory

    In the end block:

    1. Store allocated memory
    2. Subtract currently from previously allocated memory
    3. Display or log the data
    • נערך על-ידי zx38 יום שישי 02 מרץ 2012 15:27
    יום שישי 02 מרץ 2012 15:18
  • If you are in a suggestable mood there Mr. mjolinor, perhaps a high level (this is how to think about the problem) along with the actual example would be greatly appreciated.

    Measure-Memory mjolinor | Out-Wiki

    יום שישי 02 מרץ 2012 15:21
  • The problem is, it's all in hash tables, and there's no telling what order any of it would come out in.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 15:28
  • The title says it all:

    http://learningpcs.blogspot.com/2011/10/powershell-sorting-and-enumerating.html

    EDIT: Did say it all.  Sorting and enumerating hashtables.  Grr blogger.
    • נערך על-ידי Will Steele יום שישי 02 מרץ 2012 15:36 extra comment
    יום שישי 02 מרץ 2012 15:32
  • Yeah, but there's no .getenumerator() or sort in that pipeline you set up.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 15:44
  • It was a start. Have an ending? : )
    יום שישי 02 מרץ 2012 15:46
  • Let's see.  How about if we do a get-enumerator, create an array of  psobjects from that, then pipe it into a function to collect those into $input, use .movenext() and .current to step through those and create a variable from the key name, set it to the value, then use get-variable to get the variable names, which will automatically sort them, and then use IEX on each one to output the value.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 15:56
  • hmm so....
     
    $before = [gc]::GetTotalMemory($true)
    $str = "hello world"
    $after = = [gc]::GetTotalMemory($true)
     
    ($after - $before)/1kb
     
    says 55KB for me, can that be right?
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 21:02
  • I don't know what is being measured.  For me:
     
    C:> $before = [gc]::GetTotalMemory($true)
    C:> $str = "hello world"
    C:> $after = [gc]::GetTotalMemory($true)
    C:> ($after - $before)/1kb
    -0.28125
     
     
    יום שישי 02 מרץ 2012 21:09
  • so you'd be better off to do something like this I think...
     
    $before = Get-Process -id $pid | select
    workingset,pagedmemorysize,privatememorysize,virtualmemorysize
    $a = 1..1e6
    $after = Get-Process -id $pid | select
    workingset,pagedmemorysize,privatememorysize,virtualmemorysize
    $delta = "" | select @{n="working";e={($after.workingset -
    $before.workingset)/1kb}},@{n="paged";e={($after.pagedmemorysize -
    $before.pagedmemorysize)/1kb}},@{n="private";e={($after.privatememorysize -
    $before.memorysize)/1kb}},@{n="virtual";e={($after.virtualmemorysize -
    $before.virtualmemorysize)/1kb}}
    $delta
     but even still, memory is too unstable for this to work right...
     
    "Retrieves the number of bytes currently thought to be allocated. A
    parameter indicates whether this method can wait a short interval before
    returning, to allow the system to collect garbage and finalize objects."
     
    That’s right, THOUGHT to be allocated...
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 21:39
  • Wouldn't that only tell you how much memory is being used by the last object that came through the pipeline?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 21:49
  • you cant measure the memory a pipe takes, unless you look at the Peak values
    of the process.. which would only work if it doesn’t peak on loading...
    maybe there is a way to reset the peak?
     
    ohh well, back to sq1
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 21:56
  • Store allocated memory to a hash table in the process block for each object that goes through, then check that in the end block?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 22:07
  • yeah but I think we still have the same problem. unless you are dealing with
    huge data sets, memory is too volatile to measure it like this
     
     

    Justin Rich
    http://jrich523.wordpress.com
    PowerShell V3 Guide (Technet)
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    יום שישי 02 מרץ 2012 22:14
  • If it's a matter of doing it to come up with potential perfmance metrics, I think you'd have to do it the same way you do it for processor time.  You can't just take a single instance sample and trust that. You need to run it through enough iterations to get a good average.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    יום שישי 02 מרץ 2012 22:17
  • In other languages there is a way to "equivalance" two objects, so that you can view them either as
    what they naturally are to the application, or as a byte array for internal analysis.
     
    The actual implementation allocates them so that they begin at the same address of a contiguously
    allocated block of memory.   PowerShell may not have that same, simple, allocation strategy for
    data objects.
      - Larry
     >
     >
    On 3/2/2012 3:56 PM, jrich wrote:
    > ohh well, back to sq1
     
     
    יום שישי 02 מרץ 2012 23:29
  • I wrote a function to assist looking at the XML representation of a PowerShell object.
     Function show-xml
    {
     #
     # display XML of an object using a Windows app notepad.exe session for convenience.
     # example:
     #   $o = get-process powershell; show-xml $o
     # written by Larry Weiss, lfw@airmail.net
     #
       $fso = New-Object -com scripting.filesystemobject
       $filename = $fso.GetTempName()
       $tempfile = Join-Path $env:temp $filename
        $args[0] | export-clixml $tempfile
        notepad.exe $tempfile       # open the tempfile in Notepad
       Start-Sleep 5               # sleep to give the editor a chance to open the file
       if (Test-Path $tempfile)
        {
         Remove-Item $tempfile     # delete file if it still exists (it will remain be open in Notepad)
        }
    }
       - Larry
     >
     >
     
    On 2/29/2012 9:10 AM, mjolinor wrote:
    > You can get a relative idea about how complex an object is (and consequently what kind of resources
    > it takes to create it) by exporting it with export-clixml, and looking at the size of the exported
    > object.
    >
     
     
    שבת 03 מרץ 2012 18:54
  • I've thought that it actually is
     "worrying about the computational overhead needed to populate memory"
      - Larry
     >
     >
    On 2/29/2012 11:20 AM, mjolinor wrote:
    > *memory is not generally something we should worry about*
    >
    > That seems to be the general consensus, but it seems countintuitive then that best practice is to
    > do early filtering whenever possible, and to use select-object to pare large objects down to just
    > the properties you'll be using before you start processing. Maybe I'm looking at it wrong, but it
    > seems to me that "worry about memory" is exactly what that's all about.
    >
     
    שבת 03 מרץ 2012 18:59