Jump to content

Monitoring Folder - slow memory leak


Recommended Posts

I really don't know anything but memory leaks except seeing them in task manager lol

I've been running this simple script for months without issue.

Then tonight I made one change, I added a new function and AdlibRegister("AdlibFunc", 1500)

Now I can see it slowly increasing in memory useage in task manager.

So I assume I did something wrong when I wrote that function, any ideas?

Thanks,

Kenny

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.0
Author:         Kenneth P. Morrissey

Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
#include <ServiceControl.au3>
Global $PID

AdlibRegister("AdlibFunc", 1500)

Opt("TrayMenuMode", 1)
$AllwaySync = TrayCreateItem("Sync local files")
TrayCreateItem("")
$VMStart = TrayCreateItem("Start VMWare Background Services")
$VMStop = TrayCreateItem("Stop VMWare Background Services")
TrayCreateItem("")
$ExitOpt = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Switch $msg
  Case $ExitOpt
            Exit
        Case $VMStart
   TrayItemSetState($msg, 4)
            StartVMWare()
        Case $VMStop
   TrayItemSetState($msg, 4)
            KillVMWare()
        Case $AllwaySync
   TrayItemSetState($msg, 4)
            AllwaySync()
EndSwitch
Sleep(100)
WEnd

;====================
Func StartVMWare()
If Not ProcessExists("vmware-tray.exe") Then $PID = Run("C:\Program Files (x86)\VMware\VMware Workstation\vmware-tray.exe")
_StartService("", "ufad-ws60") ;VMware Agent Service
_StartService("", "VMAuthdService") ;VMware Authorization Service
_StartService("", "VMnetDHCP") ;VMware DHCP Service
_StartService("", "VMware NAT Service") ;VMware NAT Service
_StartService("", "VMUSBArbService") ;VMware USB Arbitration Service
$error = 0
If Not _ServiceRunning("", "ufad-ws60") Then MsgBox(0,"","1")
If Not _ServiceRunning("", "VMAuthdService") Then MsgBox(0,"","2")
If Not _ServiceRunning("", "VMnetDHCP") Then MsgBox(0,"","3")
If Not _ServiceRunning("", "VMware NAT Service") Then MsgBox(0,"","4")
If Not _ServiceRunning("", "VMUSBArbService") Then MsgBox(0,"","5")

If $error Then
  MsgBox(48, "VMWare Startup", "One or more VMWare services failed to start.")
Else
  MsgBox(0, "VMWare Startup", "VMWare services started succesfully")
EndIf
EndFunc
;====================
Func KillVMWare()
If ProcessExists("vmware-vmx.exe") Then
  If MsgBox(32+4, "VMWare Shutdown", "VMWare services should not be stopped while virtual machines are running." & @CR & "Continue?") = 7 Then Return
EndIf

ProcessClose($PID)
ProcessClose("vmware-tray.exe")
$error = 0
_StopService("", "ufad-ws60") ;VMware Agent Service
_StopService("", "VMAuthdService") ;VMware Authorization Service
_StopService("", "VMnetDHCP") ;VMware DHCP Service
_StopService("", "VMware NAT Service") ;VMware NAT Service
_StopService("", "VMUSBArbService") ;VMware USB Arbitration Service

If _ServiceRunning("", "ufad-ws60") Then MsgBox(0,"","1")
If _ServiceRunning("", "VMAuthdService") Then MsgBox(0,"","2")
If _ServiceRunning("", "VMnetDHCP") Then MsgBox(0,"","3")
If _ServiceRunning("", "VMware NAT Service") Then MsgBox(0,"","4")
If _ServiceRunning("", "VMUSBArbService") Then MsgBox(0,"","5")

If $error Then
  MsgBox(48, "VMWare Shutdown", "One or more VMWare services failed to stop.")
Else
  MsgBox(0, "VMWare Shutdown", "VMWare services shutdown succesfully")
EndIf
EndFunc
;====================
Func AllwaySync()
Run(@ProgramFilesDir & "\Allway Sync Startup.exe")
If @error Then MsgBox(16,"AllwaySync", "Error launching AllwaySync")
EndFunc
;====================
Func AdlibFunc()
$Files = FileFindFirstFile(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\*.lnk")
If @error Then Return
While 1
  $File = FileFindNextFile($Files)
  If @error Then Return
  $Shortcut = FileGetShortcut(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\" & $File )
  If @error Then ContinueLoop
  If StringInStr($Shortcut[0], "D:\User Profile Folders\Test") Then FileDelete(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\" & $File)
  Sleep(250)
WEnd
EndFunc
;====================
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

On the last line of your AdlibFunc(), add the below line just before the EndFunc keyword.

FileClose($Files)

You were constantly creating file find handles thus the possible memory leak you observed. :idea:

Link to comment
Share on other sites

You need to free the filefind stuff by calling FileClose on the handle. Also make the $Files variable local. What is the Sleep(250) for?

I hope you don't get too many of the files your function deals with, or you'll have reentrancy issues!

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

I can see how that would do it lol.

But even with that it was still going up.

I found a UDF to dump the file list into an array that I could process, that stopped the memory leak.

And cut the CPU usage down so low task managercan't even bring itself to give me a 1 now lol

Thanks,

Kenny

#cs ----------------------------------------------------------------------------
AutoIt Version: 3.3.6.0
Author:         Kenneth P. Morrissey

Script Function:
Template AutoIt script.
#ce ----------------------------------------------------------------------------
#include <ServiceControl.au3>
#Include <File.au3>
Global $PID

AdlibRegister("AdlibFunc", 1500)

Opt("TrayMenuMode", 1)
$AllwaySync = TrayCreateItem("Sync local files")
TrayCreateItem("")
$VMStart = TrayCreateItem("Start VMWare Background Services")
$VMStop = TrayCreateItem("Stop VMWare Background Services")
TrayCreateItem("")
$ExitOpt = TrayCreateItem("Exit")

TraySetState()

While 1
    $msg = TrayGetMsg()
    Switch $msg
  Case $ExitOpt
   AdlibUnRegister()
            Exit
        Case $VMStart
   TrayItemSetState($msg, 4)
            StartVMWare()
        Case $VMStop
   TrayItemSetState($msg, 4)
            KillVMWare()
        Case $AllwaySync
   TrayItemSetState($msg, 4)
            AllwaySync()
EndSwitch
Sleep(100)
WEnd

;====================
Func StartVMWare()
If Not ProcessExists("vmware-tray.exe") Then $PID = Run("C:\Program Files (x86)\VMware\VMware Workstation\vmware-tray.exe")
_StartService("", "ufad-ws60") ;VMware Agent Service
_StartService("", "VMAuthdService") ;VMware Authorization Service
_StartService("", "VMnetDHCP") ;VMware DHCP Service
_StartService("", "VMware NAT Service") ;VMware NAT Service
_StartService("", "VMUSBArbService") ;VMware USB Arbitration Service
$error = 0
If Not _ServiceRunning("", "ufad-ws60") Then MsgBox(0,"","1")
If Not _ServiceRunning("", "VMAuthdService") Then MsgBox(0,"","2")
If Not _ServiceRunning("", "VMnetDHCP") Then MsgBox(0,"","3")
If Not _ServiceRunning("", "VMware NAT Service") Then MsgBox(0,"","4")
If Not _ServiceRunning("", "VMUSBArbService") Then MsgBox(0,"","5")

If $error Then
  MsgBox(48, "VMWare Startup", "One or more VMWare services failed to start.")
Else
  MsgBox(0, "VMWare Startup", "VMWare services started succesfully")
EndIf
EndFunc
;====================
Func KillVMWare()
If ProcessExists("vmware-vmx.exe") Then
  If MsgBox(32+4, "VMWare Shutdown", "VMWare services should not be stopped while virtual machines are running." & @CR & "Continue?") = 7 Then Return
EndIf

ProcessClose($PID)
ProcessClose("vmware-tray.exe")
$error = 0
_StopService("", "ufad-ws60") ;VMware Agent Service
_StopService("", "VMAuthdService") ;VMware Authorization Service
_StopService("", "VMnetDHCP") ;VMware DHCP Service
_StopService("", "VMware NAT Service") ;VMware NAT Service
_StopService("", "VMUSBArbService") ;VMware USB Arbitration Service

If _ServiceRunning("", "ufad-ws60") Then MsgBox(0,"","1")
If _ServiceRunning("", "VMAuthdService") Then MsgBox(0,"","2")
If _ServiceRunning("", "VMnetDHCP") Then MsgBox(0,"","3")
If _ServiceRunning("", "VMware NAT Service") Then MsgBox(0,"","4")
If _ServiceRunning("", "VMUSBArbService") Then MsgBox(0,"","5")

If $error Then
  MsgBox(48, "VMWare Shutdown", "One or more VMWare services failed to stop.")
Else
  MsgBox(0, "VMWare Shutdown", "VMWare services shutdown succesfully")
EndIf
EndFunc
;====================
Func AllwaySync()
Run(@ProgramFilesDir & "\Allway Sync Startup.exe")
If @error Then MsgBox(16,"AllwaySync", "Error launching AllwaySync")
EndFunc
;====================
Func AdlibFunc()
$Files = _FileListToArray(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\", "*.lnk", 1)
If Not $Files Then Return
For $i = 1 To $Files[0]
  $Shortcut = FileGetShortcut(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\" & $Files[$i] )
  If @error Then ContinueLoop
  If StringInStr($Shortcut[0], "D:\User Profile Folders\Personal Movies") Then FileDelete(@UserProfileDir & "\AppData\Roaming\Microsoft\Windows\Recent\" & $Files[$i])
  FileClose($Shortcut)
  Sleep(250)
Next
EndFunc
;====================
Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

You can use FileClose() to close handles from FileOpen() or FileFindFirstFile().

Using FileClose() on the variable containing the array returned from FileGetShortcut() is incorrect usage of FileClose().

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...