Jump to content

Memory usage is through the roof.


Recommended Posts

Hi, I'm completly new to programming / AutoIt, so this is probably one of those mistakes you guys will laugh at.

I'm trying to make a window with 3 input boxes and 1 submit button, when the submit button is pressed, the data in the input boxes will be saved to a txt file.

The window shall remain open, and each time the button is pressed it should add the input to a new line in the file.

The "program" works as intended, only problem is, it uses so much memory, looks like it is rising with around 6mb pr second or so.

#include <GUIConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=g:\ai\autoit3\scite\forms\test2.kxf
$Form1_1 = GUICreate("Test_Form_1", 958, 321, 192, 133, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
GUISetBkColor(0xC0DCC0)
;The 3 Inputboxes
$inputbox1 = GUICtrlCreateInput("Inputbox1", 536, 50, 400, 21)
$inputbox2 = GUICtrlCreateInput("Inputbox2", 536, 120, 400, 21)
$inputbox3 = GUICtrlCreateInput("Inputbox3", 536, 190, 400, 21)
;The 2 txt fields relating to inputbox1
$InputBox1_Label1 = GUICtrlCreateLabel("Inputbox1 text field to the left:", 80, 50, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000080)
$InputBox1_Label2 = GUICtrlCreateLabel("inputbox1 text field above", 536, 20, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000080)
;The 2 txt fields relating to inputbox2
$inputbox2_Label1 = GUICtrlCreateLabel("inputbox2 text field to the left:", 80, 120, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 4, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
$inputbox2_Label2 = GUICtrlCreateLabel("inputbox2 text field above", 537, 90, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
;The 2 txt fields relating to inputbox3
$inputbox3_Label1 = GUICtrlCreateLabel("inputbox3 text field to the left:", 80, 190, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 4, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$inputbox3_Label2 = GUICtrlCreateLabel("inputbox3 text field above", 536, 160, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
;Submit button
$Button1 = GUICtrlCreateButton("Submit", 48, 248, 161, 49)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x3A6EA5)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

local const $sPath = ("F:\Test\"&@MON)
local const $sFilnavn = (@UserName&".txt")
local const $sFilPath = ($sPath&"\"&$sFilnavn)
DirCreate($sPath)

While 1
   Local $hFileOpen = FileOpen($sFilPath, $FO_APPEND)
      If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the file.")
           EndIf
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $button1
       $inputcontents1=GUICtrlRead($inputbox1)
       $inputcontents2=GUICtrlRead($inputbox2)
       $inputcontents3=GUICtrlRead($inputbox3)
       FileWrite($hFileOpen,"input1 "&$inputcontents1&@CRLF)
       Filewrite($hFileOpen,"input2 "&$inputcontents2&@CRLF)
       Filewrite($hFileOpen,"input3 "&$inputcontents3&@CRLF)
       GUICtrlSetData($inputbox1,"1")
       GUICtrlSetData($inputbox2,"2")
       GUICtrlSetData($inputbox3,"3")
          Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

 

Link to comment
Share on other sites

Every FileOpen needs a corresponding FileClose.

If not the handle stays alive. And because you openend the file inside the loop again and again the number of opened handles increases.

So move the FileOpen before the Loop (seems the better option for this scenario) or add a FileClose inside (at the end) the loop. 

Edited by AspirinJunkie
Link to comment
Share on other sites

Declare $hFileOpen outside of the while loop, It should look like this:

#include <GUIConstants.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

#Region ### START Koda GUI section ### Form=g:\ai\autoit3\scite\forms\test2.kxf
$Form1_1 = GUICreate("Test_Form_1", 958, 321, 192, 133, BitOR($GUI_SS_DEFAULT_GUI,$WS_MAXIMIZEBOX,$WS_SIZEBOX,$WS_THICKFRAME,$WS_TABSTOP))
GUISetBkColor(0xC0DCC0)
;The 3 Inputboxes
$inputbox1 = GUICtrlCreateInput("Inputbox1", 536, 50, 400, 21)
$inputbox2 = GUICtrlCreateInput("Inputbox2", 536, 120, 400, 21)
$inputbox3 = GUICtrlCreateInput("Inputbox3", 536, 190, 400, 21)
;The 2 txt fields relating to inputbox1
$InputBox1_Label1 = GUICtrlCreateLabel("Inputbox1 text field to the left:", 80, 50, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000080)
$InputBox1_Label2 = GUICtrlCreateLabel("inputbox1 text field above", 536, 20, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x000080)
;The 2 txt fields relating to inputbox2
$inputbox2_Label1 = GUICtrlCreateLabel("inputbox2 text field to the left:", 80, 120, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 4, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
$inputbox2_Label2 = GUICtrlCreateLabel("inputbox2 text field above", 537, 90, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0x800080)
;The 2 txt fields relating to inputbox3
$inputbox3_Label1 = GUICtrlCreateLabel("inputbox3 text field to the left:", 80, 190, 448, 30, BitOR($SS_RIGHT,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 4, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
$inputbox3_Label2 = GUICtrlCreateLabel("inputbox3 text field above", 536, 160, 400, 30, BitOR($SS_CENTER,$SS_CENTERIMAGE))
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFF0000)
;Submit button
$Button1 = GUICtrlCreateButton("Submit", 48, 248, 161, 49)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0x3A6EA5)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

local const $sPath = ("F:\Test\"&@MON)
local const $sFilnavn = (@UserName&".txt")
local const $sFilPath = ($sPath&"\"&$sFilnavn)
DirCreate($sPath)

Local $hFileOpen = FileOpen($sFilPath, $FO_APPEND)
If $hFileOpen = -1 Then
    ; Your code here
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $button1
       $inputcontents1=GUICtrlRead($inputbox1)
       $inputcontents2=GUICtrlRead($inputbox2)
       $inputcontents3=GUICtrlRead($inputbox3)
       FileWrite($hFileOpen,"input1 "&$inputcontents1&@CRLF)
       Filewrite($hFileOpen,"input2 "&$inputcontents2&@CRLF)
       Filewrite($hFileOpen,"input3 "&$inputcontents3&@CRLF)
       GUICtrlSetData($inputbox1,"1")
       GUICtrlSetData($inputbox2,"2")
       GUICtrlSetData($inputbox3,"3")
          Case $GUI_EVENT_CLOSE
            FileClose($hFileOpen)
            Exit

    EndSwitch
WEnd

TD :)

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@TheDcoder, always good practice to clear up your own resources.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

FileClose is missing as well as FileDelete.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

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