Jump to content

Best practice for ending script - or - Why am I leaving memory hanging?


Recommended Posts

I have written a script that runs as a scheduled task(batch job) to daily zip up db backups and transaction logs. After the first month I noticed the server memory in use when idle went has gone from 1 GB to 11 GB. A reboot brought the memory back to 1 GB, but after a week we were up to 4 GB. This server is a simple offsite storage device, backups get copied to it by processes on the db server. The only non-OS application running on this server is my zip process.

So here is my question: Is the way I have written my zipping/removal process leaving memory hanging so the OS thinks that memory is still allocated to a running process? And as a follow up - What are the best practices to end a program?

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#region ;Includes, Definitions, etc. ===============================================================
#include <File.au3>
#include <Date.au3>
#include <7Zip.au3>
#include <Array.au3>
#include <Affinity.au3>
#include <EventLog.au3>
#include "RecFileListToArray.au3"
Global $pid
Global $aFolders
Global $runLoop
Global $hEventLog
Dim $aData[4] = [0, 0, 0, 0]
#endregion ;Includes, Definitions, etc. ============================================================
#region ;Opens ==============================================================================
$runLoop = FileExists($CmdLine[1])
If $runLoop = 0 Then
$hEventLog = _EventLog__Open("", "CmdLineZip")
_EventLog__Report($hEventLog, 1, 0, 2, "Administrator", "Folder does not exist: " & $CmdLine[1], $aData)
_EventLog__Close($hEventLog)
EndIf
If ($CmdLine[4] > 0) Then
$pid = _WinAPI_GetCurrentProcessID()
ProcessSetAffinity($pid, $CmdLine[4])
EndIf
;~ _ArrayDisplay($CmdLine)
#endregion ;Opens ===========================================================================
#region ;Main ===============================================================================
While $runLoop = 1
If $CmdLine[3] = "1" Then
$aFileList = _RecFileListToArray($CmdLine[1], $CmdLine[2], 1, 1, 1, 2)
;~ MsgBox(0, "Recursive File Listing ", "Error: " & @error & " - " & " Extended: " & @extended & @CRLF)
Else
$aFileList = _RecFileListToArray($CmdLine[1], $CmdLine[2], 1, 0, 1, 2)
;~ MsgBox(0, "Single Folder File Listing ", "Error: " & @error & " - " & " Extended: " & @extended & @CRLF)
EndIf
If @error = 0 Then
For $j = 1 To $aFileList[0]
$runLoop = _GoZip($aFileList[$j])
If $runLoop < 1 Then ExitLoop
Next
$runLoop = 0
Else
$runLoop = 0
EndIf
WEnd
Exit
#endregion ;Main ============================================================================
#region ; Functions ===========================================================================
;~ _GoZip - Uses 7Zip to compress file --------------------------------------------------------------------------------------------------------------
Func _GoZip($inName)
$vZippedName = $inName & '.7z'
$retResult = _7ZipAdd(0, $vZippedName, $inName, 0, 9)
$hEventLog = _EventLog__Open("", "CmdLineZip")
If $retResult = 0 Then
_EventLog__Report($hEventLog, 1, 0, 2, "Administrator", "Failure Zipping: " & $inName, $aData)
Return 0
Else
$retResult = FileDelete($inName)
If $retResult = 0 Then
_EventLog__Report($hEventLog, 1, 0, 2, "Administrator", "Failure Removing: " & $inName, $aData)
Return 0
Else
_EventLog__Report($hEventLog, 0, 0, 2, "Administrator", "Successful Zip and Removal: " & $inName, $aData)
Return 1
EndIf
EndIf
_EventLog__Close($hEventLog)
EndFunc ;==>_GoZip
#endregion ; Functions ========================================================================
Link to comment
Share on other sites

Your _EventLog__Close($hEventLog) in the function _GoZip is never being called, the function returns in the If/Endif above it, so that line isn't ever activated. Might be some of the problem.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

BrewMan is right. The only condition under which the event log gets close is at the top, if no files exist. The _GoZip has a Return for every possible condition so the final _EventLog__Close never gets called.

Link to comment
Share on other sites

Your _EventLog__Close($hEventLog) in the function _GoZip is never being called, the function returns in the If/Endif above it, so that line isn't ever activated. Might be some of the problem.

Thank you gentlemen.

I miss having code reviews. Downsizing sucks.

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