Windows Vista Forums

Garbage Collection and PowerShell
  1. #1


    SQL Guru Guest

    Garbage Collection and PowerShell

    I've noticed that Powershell likes to eat LOTS of memory. I think it has to
    do with .net and garbage collection. I've done a test with wmi and then
    removing the variable. It seems that memory will continually increase
    without ever going to a stable level. I thought it would increase until
    garbage collection runs, but it never seems to stabilize. My thought is
    that the heap gets fragmented and therefore can't release all of the freed
    memory.

    I've found something that seems to have stabilized it, but I think there may
    be some drawbacks. I've added "[GC]::Collect()" to my prompt function to
    force garbage collection. Does anyone know what type of issues I may see
    from this? Or, is there another way to make it free up the memory more
    efficiently?



    Test Case 1: Keeps using memory.
    $a = get-wmiobject Win32_NTLogEvent ; remove-variable a;
    [math]::floor((get-process powershell).PrivateMemorySize/1MB)

    Test Case 2: Memory stabilizes after a few executions (3-4).
    [GC]::Collect() has been added.
    $a = gwmi Win32_NTLogEvent ; remove-variable a; [GC]::Collect();
    [math]::floor((get-process powershell).PrivateMemorySize/1MB)

    Thanks,

    SQL Guru


      My System SpecsSystem Spec

  2. #2


    Marcel J. Ortiz [MSFT] Guest

    Re: Garbage Collection and PowerShell

    What you are seeing is just that the Garbage collector doesn't run unless it
    has to (memory pressure). There's quite a few articles around explaining
    how it works if youre interested. Anyway I don't think you'll see any
    issues from the [GC]::Collect() in your prompt function (other than some
    performance degradation from running the garbage collector all the time) but
    you really shouldn't need it.

    "SQL Guru" <myjunk_abcd at hotmail dot com> wrote in message
    news:elBqkXpWHHA.496@TK2MSFTNGP06.phx.gbl...
    > I've noticed that Powershell likes to eat LOTS of memory. I think it has
    > to
    > do with .net and garbage collection. I've done a test with wmi and then
    > removing the variable. It seems that memory will continually increase
    > without ever going to a stable level. I thought it would increase until
    > garbage collection runs, but it never seems to stabilize. My thought is
    > that the heap gets fragmented and therefore can't release all of the freed
    > memory.
    >
    > I've found something that seems to have stabilized it, but I think there
    > may
    > be some drawbacks. I've added "[GC]::Collect()" to my prompt function to
    > force garbage collection. Does anyone know what type of issues I may see
    > from this? Or, is there another way to make it free up the memory more
    > efficiently?
    >
    > Test Case 1: Keeps using memory.
    > $a = get-wmiobject Win32_NTLogEvent ; remove-variable a;
    > [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >
    > Test Case 2: Memory stabilizes after a few executions (3-4).
    > [GC]::Collect() has been added.
    > $a = gwmi Win32_NTLogEvent ; remove-variable a; [GC]::Collect();
    > [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >
    > Thanks,
    >
    > SQL Guru
    >



      My System SpecsSystem Spec

  3. #3


    Thomas Tomiczek Guest

    Re: Garbage Collection and PowerShell

    That is exactly what happens.

    To quote the original poster:

    > I thought it would increase until
    > garbage collection runs, but it never seems to stabilize.


    And that happens. The GC just does not run. Why SHOULD it? The memory is not
    needed :-) GC costs time.

    Thomas

    "Marcel J. Ortiz [MSFT]" <mosoto@online.microsoft.com> wrote in message
    news:7EC4F61A-B635-4233-B353-B626CF671E60@microsoft.com...
    > What you are seeing is just that the Garbage collector doesn't run unless
    > it has to (memory pressure). There's quite a few articles around
    > explaining how it works if youre interested. Anyway I don't think you'll
    > see any issues from the [GC]::Collect() in your prompt function (other
    > than some performance degradation from running the garbage collector all
    > the time) but you really shouldn't need it.
    >
    > "SQL Guru" <myjunk_abcd at hotmail dot com> wrote in message
    > news:elBqkXpWHHA.496@TK2MSFTNGP06.phx.gbl...
    >> I've noticed that Powershell likes to eat LOTS of memory. I think it has
    >> to
    >> do with .net and garbage collection. I've done a test with wmi and then
    >> removing the variable. It seems that memory will continually increase
    >> without ever going to a stable level. I thought it would increase until
    >> garbage collection runs, but it never seems to stabilize. My thought is
    >> that the heap gets fragmented and therefore can't release all of the
    >> freed
    >> memory.
    >>
    >> I've found something that seems to have stabilized it, but I think there
    >> may
    >> be some drawbacks. I've added "[GC]::Collect()" to my prompt function to
    >> force garbage collection. Does anyone know what type of issues I may see
    >> from this? Or, is there another way to make it free up the memory more
    >> efficiently?
    >>
    >> Test Case 1: Keeps using memory.
    >> $a = get-wmiobject Win32_NTLogEvent ; remove-variable a;
    >> [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >>
    >> Test Case 2: Memory stabilizes after a few executions (3-4).
    >> [GC]::Collect() has been added.
    >> $a = gwmi Win32_NTLogEvent ; remove-variable a; [GC]::Collect();
    >> [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >>
    >> Thanks,
    >>
    >> SQL Guru
    >>

    >



      My System SpecsSystem Spec

  4. #4


    SQL Guru Guest

    Re: Garbage Collection and PowerShell

    I have 1 GB of physical memory in my system. If I have 1.8 GB committed,
    memory usage is growing, my system is dirt slow from swapping, and garbage
    collection still isn't needed; then I have no clue what would trigger it on
    its own. The cost of garbage collection is almost nothing compared to the
    cost of it not freeing memory. I don't know why this happens, but it does
    and I've seen it on other peoples systems as well. I understand the idea of
    garbage collection not running immediately and I agree that it normally
    shouldn't. I just believe it should be a little more consistent/aggressive
    with freeing memory.

    SQL_Guru

    "Thomas Tomiczek" <t.tomiczek@nettecture.com> wrote in message
    news:54108F9F-6AB0-4867-92AC-F9B3E33F5172@microsoft.com...
    That is exactly what happens.

    To quote the original poster:

    > I thought it would increase until
    > garbage collection runs, but it never seems to stabilize.


    And that happens. The GC just does not run. Why SHOULD it? The memory is not
    needed :-) GC costs time.

    Thomas

    "Marcel J. Ortiz [MSFT]" <mosoto@online.microsoft.com> wrote in message
    news:7EC4F61A-B635-4233-B353-B626CF671E60@microsoft.com...
    > What you are seeing is just that the Garbage collector doesn't run unless
    > it has to (memory pressure). There's quite a few articles around
    > explaining how it works if youre interested. Anyway I don't think you'll
    > see any issues from the [GC]::Collect() in your prompt function (other
    > than some performance degradation from running the garbage collector all
    > the time) but you really shouldn't need it.
    >
    > "SQL Guru" <myjunk_abcd at hotmail dot com> wrote in message
    > news:elBqkXpWHHA.496@TK2MSFTNGP06.phx.gbl...
    >> I've noticed that Powershell likes to eat LOTS of memory. I think it has
    >> to
    >> do with .net and garbage collection. I've done a test with wmi and then
    >> removing the variable. It seems that memory will continually increase
    >> without ever going to a stable level. I thought it would increase until
    >> garbage collection runs, but it never seems to stabilize. My thought is
    >> that the heap gets fragmented and therefore can't release all of the
    >> freed
    >> memory.
    >>
    >> I've found something that seems to have stabilized it, but I think there
    >> may
    >> be some drawbacks. I've added "[GC]::Collect()" to my prompt function to
    >> force garbage collection. Does anyone know what type of issues I may see
    >> from this? Or, is there another way to make it free up the memory more
    >> efficiently?
    >>
    >> Test Case 1: Keeps using memory.
    >> $a = get-wmiobject Win32_NTLogEvent ; remove-variable a;
    >> [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >>
    >> Test Case 2: Memory stabilizes after a few executions (3-4).
    >> [GC]::Collect() has been added.
    >> $a = gwmi Win32_NTLogEvent ; remove-variable a; [GC]::Collect();
    >> [math]::floor((get-process powershell).PrivateMemorySize/1MB)
    >>
    >> Thanks,
    >>
    >> SQL Guru
    >>

    >



      My System SpecsSystem Spec

  5. #5


    klumsy@xtra.co.nz Guest

    Re: Garbage Collection and PowerShell

    to do be so extreme you could have your prompt only call GC every X
    number or times, or evey Y # of minutes

    a situations when it is good to call the GC is if you are youiung a
    com object , and you get rid of it, but you want to ensure it goes
    away. I.e you make a com object of excel.. and that process is created
    then you do $xl.quit
    then you remove the variable so their is no reference count.. but
    wrong.. there still is a reference count because the dotnet variable
    is waiting for GC.. then to get rid of that excel process you should
    call the garbage collection.


      My System SpecsSystem Spec

Garbage Collection and PowerShell problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Powershell Pipe to Foreach to get indiviudal elements in a string collection. William Holmes PowerShell 0 01 Sep 2009
Using Garbage Collection after import SteveB .NET General 1 20 Oct 2008
garbage bin ccc38941 Vista General 2 25 Jul 2008
DEP Garbage Richard Vista security 3 17 Nov 2007
Garbage collection and disposing resources JN1974 WinFX General 1 17 Apr 2007