Jump to content

how to know there is a memory leak [solved] ( I think )


Recommended Posts

How do I know there is a memory leak ?, is it "Private Bytes" or "Working Set" too ?
I have some clues but no definitive knowing. I have no experience looking at these.

Also, If I can use AutoIt to self-check within the code would be most helpful. 

Thanks

Edited by argumentum
[solved]

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

4 minutes ago, BigDaddyO said:

I posted something for this a while ago.

It gives me some errors executing

Process Name = (MicSwitcher)
    PoolNonpagedBytes = (15808), PoolPagedBytes = (246160), PageFileBytes = (7131136), PrivateBytes = (7131136), VirtualBytes = (129437696)
Initial Function Completed
"C:\Users\Tester\Downloads\SciTE Customization GUI -- Source\New AutoIt v3 Script.au3" (17) : ==> Subscript used on non-accessible variable.:
If $aMemDiff[1] > 300 or $aMemDiff[2] > 300 or $aMemDiff[3] > 300 Then
If $aMemDiff^ ERROR


I'm looking at 

#include <WinAPIProc.au3>
Local $n, $aData = _WinAPI_GetProcessMemoryInfo(@AutoItPID) ; <-- add your PID
For $n = 0 To 9
    $aData[$n] = ByteSuffix($aData[$n])
Next
ConsoleWrite('Number of page faults: ' & $aData[0] & @CRLF)
ConsoleWrite('Peak working set size: ' & $aData[1] & ' ' & @CRLF)
ConsoleWrite('Current working set size: ' & $aData[2] & ' ' & @CRLF)
ConsoleWrite('Peak paged pool usage: ' & $aData[3] & ' ' & @CRLF)
ConsoleWrite('Current paged pool usage: ' & $aData[4] & ' ' & @CRLF)
ConsoleWrite('Peak nonpaged pool usage: ' & $aData[5] & ' ' & @CRLF)
ConsoleWrite('Current nonpaged pool usage: ' & $aData[6] & ' ' & @CRLF)
ConsoleWrite('Current space allocated for the pagefile: ' & ByteSuffix($aData[7]) & ' ' & @CRLF)
ConsoleWrite('Peak space allocated for the pagefile: ' & ByteSuffix($aData[8]) & ' ' & @CRLF)
ConsoleWrite('Current private space: ' & $aData[9] & ' ' & @CRLF)

Func ByteSuffix($iBytes)
    Local $iIndex = 0, $aArray = [' bytes', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB']
    While $iBytes > 1023
        $iIndex += 1
        $iBytes /= 1024
    WEnd
    Return Round($iBytes, 3) & $aArray[$iIndex]
EndFunc   ;==>ByteSuffix

or do I need the WMI services ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

@BigDaddyO, how do you like this 

ConsoleWrite(MemLeakTooMuch() & @CRLF)
Sleep(1000) ; call on AdlibRegister() or other means
; Ex: AdlibRegister("OnMemLeakRestart", 3600000) ; once an hour
ConsoleWrite(MemLeakTooMuch() & @CRLF)

Func OnMemLeakRestart() ; 
    If MemLeakTooMuch() Then
        If _WinAPI_GetIdleTime() > 600000 Then ; over 10 min.
            ShellExecute(@ScriptFullPath)
            Exit 0
        EndIf
    EndIf
EndFunc

Func MemLeakTooMuch($iPid = 0, $iMutliplier = 4) ; in case this is for some other PID, but only one  =/
    Local Static $aProcessMemoryInfo = 99
    Local $ret = False, $n, $aTemp
    If $aProcessMemoryInfo = 99 Then
        $aTemp = _WinAPI_GetProcessMemoryInfo($iPid)
        If Not IsArray($aTemp) Then Return SetError(1, 0, $ret)
        $aProcessMemoryInfo = $aTemp
    Else
        $aTemp = _WinAPI_GetProcessMemoryInfo($iPid)
        If Not IsArray($aTemp) Then Return SetError(1, 0, $ret)
        For $n = 1 To UBound($aTemp) -1
            If $aTemp[$n] > $aProcessMemoryInfo[$n] * $iMutliplier Then $ret = True
        Next
    EndIf
    Return $ret
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

You are checking if any type of memory exceeds the multiplier.  I never looked at the details of every memory object, just those 3 I listed above so i'm not sure how valid that is.

Are you checking for memory leak on your own app, or a 3rd party app?  if it's your own, you could add that old mem clean up function that's been around for years.  

edit:  The _ReduceMemory() function i've used for years seems to have been replaced by _WinAPI_EmptyWorkingSet() now.

Edited by BigDaddyO
Link to comment
Share on other sites

25 minutes ago, BigDaddyO said:

if it's your own, you could add that old mem clean up function

It's been found that the function is actually counterproductive as it flushes to disk, memory leaks too. Just don't use it. Search the forum to dig down exactly why.

28 minutes ago, BigDaddyO said:

Are you checking for memory leak on your own app

Yes. Is the Mic. switcher I put together. It misbehaves a lot. So, instead of using the "_IMMNotificationClient_*" of the "bulk" file, I'm looking into _WinAPI_RegNotifyChangeKeyValue() but still need to use the _EnumerateAudioDevices() from the bulk file, and it too has some leaks, so I'm gonna use the above posted OnMemLeakRestart() to solve the problem.

38 minutes ago, BigDaddyO said:

You are checking if any type of memory exceeds the multiplier

Well, I figure that it should not use more than twice it's original usage ( of less than 20 mb in my case ), if any of these values grows to 4 times ( usually the paged mem. ), might as well restart the script when the user is not using the PC ( say, after 10 min. of idle time ) 

I'm a baby programmer. I mean, I could not find a ptr* if I was pointed to it <joke><but true>

So I'm sharing to learn the most I can, from others with more experience on the art of not crashing the script, or the hole PC for that matter.  :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...