3

Memory Picture

There seems to be a leak in my install of PS v4.
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.17400

I run the following, memory quickly grows unbounded:

while($true) {
    New-Object System.Object
}

If I break out of the loop and call [System.GC]::Collect() excess memory is immediately freed. The weird part is that I can run this in a slightly older PSv4 version I have lying around (CLR build 34014, buildversion build 15394), and it runs fine.

Is there a setting in PS that toggles garbage collection?

Heath Carroll
  • 323
  • 2
  • 12
  • 1
    If garbage collection cleans it up then it isn't a memory leak. It may be a poorly tuned gc or a stopped/off gc but it, by definition, isn't a leak. – Etan Reisner Jun 18 '15 at 14:45
  • OK. Then how do you tune/stop/start garbage collection in PowerShell? If you can't and the default behavior is to not clean up resources until you manually tell it to, then that is a leak. That's like saying using malloc doesn't result in memory leaks since you can free it anytime you want. – Heath Carroll Jun 18 '15 at 17:06
  • 1
    Have you tried killing the loop and just waiting? Or running some other commands while waiting? The gc might not do anything until pushed to avoid impacting performance, etc. (I don't know anything about the powershell gc specifically.) – Etan Reisner Jun 18 '15 at 17:12
  • 1
    No, it isn't, and yes that isn't a leak. A leak is memory that is *lost* and unrecoverable. Anything else is "used". It may be used **very badly** but if you can release it it isn't leaked. Sure, failing utterly to ever release the memory (even though technically you could) could, productively, be called a leak as well but this isn't even that necessarily. – Etan Reisner Jun 18 '15 at 17:14
  • Yes. Killing the loop stops the memory going up, but the excess hangs around. I don't know how long, but any future memory use just keeps adding to it after you run it. I've waited about 10 minutes doing other things with it, and it only goes back down if you manually call Collect(). You're right though, it really does seem like gc is just turned off somehow. – Heath Carroll Jun 18 '15 at 17:19
  • Does powershell eventually gc itself in the loop? Does it if you add a small sleep/delay in the loop? A number of things I'm reading online seem to indicate that powershell doesn't really run gc on its own much until memory pressure is really serious (or the script exits). – Etan Reisner Jun 18 '15 at 17:31
  • I actually ran into this using sleep 1, it just takes longer to notice the memory spike. I've let it run up to > 1 GB, which only takes about 5 minutes. And I'm seeing a lot concerning best practices concerning PS and gc. I would just drop this entirely if it didn't work in older versions. I've verified the GC works (without being told to) in PS v2 and a year old version of PS v4. – Heath Carroll Jun 18 '15 at 17:43
  • 1
    I submitted it as a bug to Microsoft (https://connect.microsoft.com/PowerShell/feedbackdetail/view/1446857/native-memory-leak-in-ps-4-0-30319-34209). I guess they'll let us know for certain whether this is a bug or a "feature" :) – Heath Carroll Jun 18 '15 at 17:45
  • Greater than 1GB on a machine with 4GB or 6GB available isn't anywhere near a lot and could really just be powershell saying "eh, that's not enough to worry about". Older versions of powershell may have optimized the object creation away or re-used a temporary or something and the difference may have nothing to do with the gc. – Etan Reisner Jun 18 '15 at 17:45
  • Challenge accepted! (This may take a while on my 8GB ram machine.) – Heath Carroll Jun 18 '15 at 17:48
  • Can't reproduce. I'm running `while($true) { New-Object System.Object; Start-Sleep -m 5 }` in my test VM with 2 GB RAM and memory consumption is stable. – Ansgar Wiechers Jun 18 '15 at 18:33
  • @Ansgar Check your PS version. It only seems to happen for me in version 4.0.30319.34209. Older versions work fine. – Heath Carroll Jun 18 '15 at 18:43
  • CLRVersion 4.0.30319.34209, BuildVersion 6.3.9600.16406 – Ansgar Wiechers Jun 18 '15 at 18:51
  • So I started it then got impatient after about ten minutes. I loaded up a bunch of stuff to bring my steady ram usage up to about 6 GB and kept watching it. It gets to about 2.5 GB (and ~94% system memory total usage), but then it seems to garbage collect. I let it run another 10 minutes and memory usage spiked back up as if it never gc'd. Then I started paging. Added a screenshot. @Ansgar my BuildVersion is newer. See the question. – Heath Carroll Jun 18 '15 at 18:53
  • I see a similar issue in PowerShell 3. http://stackoverflow.com/q/31620763/138757 – Phil Jul 24 '15 at 22:51

0 Answers0