2

I have this script to trap the IP's on a network to compare to historic packet captures as part of a larger problem solving exercise.

function qp($comp)
    {
    $p = ping $comp -n 1 -w 2 -4
    IF($? -eq $true){$out = $p[1].split("[")[1].split("]")[0]}
    else{$out = $False}
    return $out
    }

$comps = Get-Content C:\PacketCapture\comps.txt

DO
    {
    foreach($comp in $comps)
        {
        ECHO "$(qp $comp);$comp" >>"C:\PacketCapture\IP_$(Get-date -format HHmm-ddMMyy).txt"
        }
    Start-sleep 3600
    }
until($null -eq "WANG")

I set this off initially a fortnight ago, at the middle of last week the terminal it was running on was grinding to a halt as the memory use for the power shell process was nearly at 2GB.

I stopped and restarted it and again we were at 1.2GB RAM use this morning.

Whilst not particularly critical, I've modified this to run once then stop/start itself, I'm interested to know which element is causing the memory leak and how I would identify that in the future.

10
  • 1
    I could see it being worse but have you tried using Test-Connnection -Quiet? It would remove the need for string manipulation although that should finish rather quickly. Also what PowerShell version are you running this with. Commented Oct 5, 2015 at 12:07
  • 1
    First, how large is comps.txt? Second, why even use ping.exe when there's Test-Connection cmdlet? Finally, your do..until is funny but while ($true) { ... } is more common. Commented Oct 5, 2015 at 12:11
  • 2
    I agree with the others about using Test-Connection. That said, what if you sprinkled in a [GC]::Collect(), maybe in the look after the Start-Sleep? Just curious if that helps. Commented Oct 5, 2015 at 12:35
  • 1
    Try VMMap from sysinternals. It allows you to view the memory usage pattern of a process over time. When at 1.2GB ram, you could take a memory dump using procdump (or procexp or taskmanager -> right click -> create dumpfile) and analyze it with WinDbg. Commented Oct 5, 2015 at 14:35
  • 1
    fwiw - I didn't notice any memory drain/leak when running this on my computer with the Start-Sleep removed. Commented Oct 5, 2015 at 14:36

2 Answers 2

2

You could try invoking garbage collection manually. I think the do/until loop is a good place for it:

DO
    {
    foreach($comp in $comps)
        {
        ECHO "$(qp $comp);$comp" >>"C:\PacketCapture\IP_$(Get-date -format HHmm-ddMMyy).txt"
        }
    Start-sleep 3600
    [GC]::Collect()
    }
until($null -eq "WANG")
Sign up to request clarification or add additional context in comments.

Comments

-1

Try introducing garbage collection into your code with [System.GC]::Collect()

Source : https://dmitrysotnikov.wordpress.com/2012/02/24/freeing-up-memory-in-powershell-using-garbage-collector

8 Comments

You should actually include the code from the link... its one line. This is a poor answer without it.
It is the right answer but what happens when the link dies? This is a link-only answer right now which is discouraged at SO. My comment is not poor. It is an attempt at constructive criticism.
But users wouldn't have to bother finding anything if you wrote the 15 characters in the answer and kept the link for context is my point.
Jimbo thanks for the answer, Matt is technically correct. Stack Overflow prefer you include the crux of your answer here and not just via a link. However @Matt, Stack Overflow is collaborative, feel free to edit Jimbo's answer to include the pertinent details.
@Patrick I am aware of that yes. I was taking the teach a man to fish approach.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.