pythonpowershellmemory-leakskill-process

How can I avoid a memory leak when force killing PowerShell?


I'm making a Python script that creates shortcuts using PowerShell. I've taken advantage of the fact that if a network path isn't available it will take a long time to create the shortcut. I force kill the process if it takes too long, and this results in a bunch of shortcuts of available computers. Here is what's inside the loop

temp = "powershell; $WshShell = New-Object -ComObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut(\"\"\"%cd%\\" + list[i][:list[i].find("\\")] + ".lnk\"\"\"); $Shortcut.TargetPath = \"\"\"\\" + list[i][list[i].find("\\"):-1] + "\"\"\"; $Shortcut.Save()"
subprocess.Popen(temp, shell=True)

This runs through a list that is like "COMPUTER1/insert ip here"

I then have another script that uses tasklist to find PowerShell processes and kills them after around 2 seconds. This works, but leads to a memory leak. I think it's a result of killing the PowerShell and the resources aren't released from RAM.

Is there a way to release that memory or avoid the leak altogether?


Solution

  • If you run this in a loop, then yes.. you are bound to run into memory problems, since you keep creating COM objects, but never release them from memory. After Shortcut.Save() you need to make sure the Windows Garbage Collector will remove them from memory using:

    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($shortcut)
    $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WshShell)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
    

    Force killing scripts when a network path isn't available is also a bad idea.. Why not test a network path BEFORE trying to make a shortcut ? That's what Powershell cmdlets Test-NetConnection and Test-Path are for.

    B.T.W. Writing all that code in one long line is not only ugly, it also doesn't help in finding any mistakes you may have put in there.. Best write code in multiple lines with proper indentation.