Jump to content

Code Optimization (specifically memory usage)


Recommended Posts

I would like to start by saying that I have no training in any sort of coding i taught myself bat files then several years ago I was attempting to do something more complex with a bat that's how I found Autoit. that being said please don't make to much fun of me if this code is just ridiculous. I believe the "program" is using to much memory for what it is so I decided to create a forum account to ask about it. Thanks in advance!

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile_x64=NL.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Res_Fileversion=1.3.0.3
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Add_Constants=n
#AutoIt3Wrapper_Run_Tidy=y
#AutoIt3Wrapper_Tidy_Stop_OnError=n
#AutoIt3Wrapper_Run_Au3Stripper=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
Opt("TrayAutoPause", 0)
If FileExists("C:\NetLog") = 0 Then
    DirCreate("C:\Netlog")
EndIf
While 1
    $b = "NA"
    $a = Ping("1.1.1.1")
    FileOpen("C:\NetLog\Net " & @MON & "-" & @MDAY & "-" & @YEAR & ".txt", 1)
    If $a = 0 Then
        $b = Ping("8.8.8.8")
        If $b = 0 Then
            FileWriteLine("C:\NetLog\Net " & @MON & "-" & @MDAY & "-" & @YEAR & ".txt", "FAILED " & @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC)
        EndIf
    Else
        FileWriteLine("C:\NetLog\Net " & @MON & "-" & @MDAY & "-" & @YEAR & ".txt", "MS: A=" & $a & " B=" & $b & " " & @MON & "-" & @MDAY & "-" & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC)
    EndIf
    FileClose("C:\NetLog\Net " & @MON & "-" & @MDAY & "-" & @YEAR & ".txt")
    Sleep(60000)
WEnd

 

Link to comment
Share on other sites

1 minute ago, mitchell12388 said:

Autoit just doesn't like me... LOL!

nope again.  But if you want us to give you a better advice, I suggest you should provide a full detail of your system config.  How did you compile it... etc

Link to comment
Share on other sites

Link to comment
Share on other sites

The reason your memory keeps rising is because you are creating a new file handle with each iteration through your loop.  FileOpen returns a handle to your file.   That handle is used by the FileClose.  Your FileClose was failing because it expected, as its parameter, the handle created by the FileOpen, not a file name.  If you were going to use FileOpen, then your FileWriteLine functions should have been using the file handle, not the file name.  I know that it can be a little confusing when you are first starting out.

Your script is only writing to the file once every 60 seconds.   So you can just let the FileWriteLine open and close the file automatically by supplying the file name instead of a handle (like you were already doing).   Change your script to something like this and see if the memory keeps creeping up or not.  Also, I modified the logic to only create the date and time values once per iteration.  I left the rest of the logic alone.

 

Opt("TrayAutoPause", 0)

If FileExists("C:\NetLog\") = 0 Then
    DirCreate("C:\Netlog")
EndIf

While 1
    $dt  = @MON & "-" & @MDAY & "-" & @YEAR
    $hms = @HOUR & ":" & @MIN & ":" & @SEC
    $b   = "NA"
    $a   = Ping("1.1.1.1")
    
    If $a = 0 Then
        $b = Ping("8.8.8.8")
        If $b = 0 Then
            FileWriteLine("C:\NetLog\Net " & $dt & ".txt", "FAILED " & $dt & " " & $hms)
        EndIf
    Else
        FileWriteLine("C:\NetLog\Net " & $dt & ".txt", "MS: A=" & $a & " B=" & $b & " " & $dt & " " & $hms)
    EndIf
    
    Sleep(60000)
WEnd

 

If you wanted to use the FileOpen way of doing things, then it would look something like this:

Opt("TrayAutoPause", 0)

If FileExists("C:\NetLog") = 0 Then
    DirCreate("C:\Netlog")
EndIf
While 1
    $dt  = @MON & "-" & @MDAY & "-" & @YEAR
    $hms = @HOUR & ":" & @MIN & ":" & @SEC
    $b   = "NA"
    $a   = Ping("1.1.1.1")

    $hFile = FileOpen("C:\NetLog\Net " & $dt & ".txt", 1)
    If $a = 0 Then
        $b = Ping("8.8.8.8")
        If $b = 0 Then
            FileWriteLine($hFile, "FAILED " & $dt & " " & $hms)
        EndIf
    Else
        FileWriteLine($hFile, "MS: A=" & $a & " B=" & $b & " " & $dt & " " & $hms)
    EndIf
    FileClose($hFile)

    Sleep(60000)
WEnd

 

Edited by TheXman
Link to comment
Share on other sites

1 hour ago, TheXman said:

Your FileClose was failing because it expected, as its parameter, the handle created by the FileOpen, not a file name.

Sorry I meant to quote this in my last post... 

also thanks That first script you posted looks much cleaner 

1 hour ago, TheXman said:

Opt("TrayAutoPause", 0) If FileExists("C:\NetLog\") = 0 Then     DirCreate("C:\Netlog") EndIf While 1     $dt  = @MON & "-" & @MDAY & "-" & @YEAR     $hms = @HOUR & ":" & @MIN & ":" & @SEC     $b   = "NA"     $a   = Ping("1.1.1.1")         If $a = 0 Then         $b = Ping("8.8.8.8")         If $b = 0 Then             FileWriteLine("C:\NetLog\Net " & $dt & ".txt", "FAILED " & $dt & " " & $hms)         EndIf     Else         FileWriteLine("C:\NetLog\Net " & $dt & ".txt", "MS: A=" & $a & " B=" & $b & " " & $dt & " " & $hms)     EndIf         Sleep(60000) WEnd

I'll have to give it a try it's not copyright?? ^_^

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...