ManualIT Posted January 31, 2019 Share Posted January 31, 2019 Guys, i need help on creating a script that restarts a program once it starts using more than 1GB of memory. No idea how to start on the script, i don't know which functions i should use for process memory reading. My mind is in total blank at the moment, so i need a kick start Link to comment Share on other sites More sharing options...
Nine Posted January 31, 2019 Share Posted January 31, 2019 look at ProcessGetStats () function ManualIT 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
argumentum Posted January 31, 2019 Share Posted January 31, 2019 https://www.autoitscript.com/forum/topic/197259-memory-leak-script-stopper-or-not/?tab=comments#comment-1414754 ManualIT 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
ManualIT Posted February 2, 2019 Author Share Posted February 2, 2019 (edited) On 1/31/2019 at 8:24 PM, argumentum said: https://www.autoitscript.com/forum/topic/197259-memory-leak-script-stopper-or-not/?tab=comments#comment-1414754 Thank you so much but this is too advanced for me On 1/31/2019 at 7:48 PM, Nine said: look at ProcessGetStats () function Thanks! I've searched about this function and came up with this script so far for testing: #include <MsgBoxConstants.au3> While 1 $memory = ProcessGetStats("mspaint.exe") Sleep(1000) call ("check") ConsoleWrite($memory[0]/1024&@CRLF) WEnd Func check() If $memory > 30000 Then MsgBox($MB_SYSTEMMODAL, "Alert!", "Restart program") Exit EndIf EndFunc It reads and writes the correct used memory on the console, however it doesn't detect when it goes over 30000 kilobytes Edited February 2, 2019 by ManualIT Link to comment Share on other sites More sharing options...
TheXman Posted February 2, 2019 Share Posted February 2, 2019 (edited) @ManualIT The ProcessGetStats() function returns an array. In your check() function, you are referencing $memory (the array) when you should be referencing $memory[0] / 1024. The script below does basically the same thing as what your script was doing, with the correction that I pointed out and a some remarks to help make it clear. Also note the @error checking. Coding defensively, in order to catch and handle errors when they occur, will save you a lot of headaches when your scripts get longer and more complex. If you start the script without MSPaint running, you will get an error message. Your original script would not have recognized that mspaint wasn't running and just continued on. #include <Constants.au3> Global $aMemStats Global $nMemKB While 1 ;Get process' memory stats $aMemStats = ProcessGetStats("mspaint.exe") If @error Then MsgBox($MB_ICONERROR, "ERROR", "Unable to get process stats. Check to make sure mspaint is running.") Exit EndIf ;Convert bytes to kilobytes $nMemKB = $aMemStats[0] / 1024 ;If threshhold exceeded If $nMemKB > 30000 Then ;Display message and exit MsgBox($MB_ICONWARNING, "WARNING", "MSPaint memory limit exceeded!" & @CRLF & "Working set = " & $nMemKB) Exit EndIf ;Wait a second Sleep(1000) WEnd Edited February 2, 2019 by TheXman ManualIT 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
ManualIT Posted February 2, 2019 Author Share Posted February 2, 2019 (edited) @TheXman Excellent, i learned so much from this, thank you so much! Edited February 2, 2019 by ManualIT Link to comment Share on other sites More sharing options...
TheXman Posted February 2, 2019 Share Posted February 2, 2019 You're welcome. CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
caramen Posted February 3, 2019 Share Posted February 3, 2019 I am Trying to complet this with adding the addition of multiple process with same name. My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
caramen Posted February 3, 2019 Share Posted February 3, 2019 (edited) @TheXman This should do the trick for me. Why it s not same as my tskmgr ? expandcollapse popup#include <Constants.au3> #include <Array.au3> Global $aMemStats , $aMemStats2 = 0 Global $nMemKB While 1 ;Get process' memory stats $ProcessName = "Firefox.exe" $ProcessList = ProcessList ($ProcessName) If @error Then MsgBox($MB_ICONERROR, "ERROR", "Unable to get process stats. Check to make sure mspaint is running.") Exit EndIf If IsArray ($ProcessList) Then _ArrayDisplay ($ProcessList) $iRaws = UBound ( $ProcessList ) For $i = 0 To $iRaws -1 $aMemStatsProcessID = ProcessGetStats($ProcessList[$i][1]) $aMemStatsProcessName = ProcessGetStats($ProcessList[$i][0]) $aMemStats1 = ProcessGetStats($ProcessList[$i][1]) $aMemStats2 = $aMemStats2+$aMemStats1[1] MsgBox(0,"$aMemStats2",$aMemStats2/ 1024 / 1024 ) Next EndIf ;Convert bytes to Mo $nMemKB = $aMemStats2 / 1024 / 1024 MsgBox($MB_ICONWARNING, " Mo",$nMemKB) ;If threshhold exceeded $Limite = 250 If $nMemKB > $Limite Then ;Display message and exit MsgBox($MB_ICONWARNING, "WARNING", "Firefox memory limit (/"&$Limite&" Mo) exceeded!" & @CRLF & "Working set = " & $nMemKB &"/"&$Limite&" Mo" ) Exit EndIf ;Wait a second Sleep(1000) WEnd Edited February 3, 2019 by caramen My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
caramen Posted February 3, 2019 Share Posted February 3, 2019 (edited) Hmmmmmmmmm close expandcollapse popup#include <Constants.au3> #include <Array.au3> Global $aMemStats1 = 0 , $aMemStats2 = 0 Global $nMemKB While 1 $aMemStats1 = 0 $aMemStats2 = 0 ;Get process' memory stats $ProcessName = "" If $ProcessName = "" Then $ProcessList = ProcessList () If @error Then MsgBox($MB_ICONERROR, "ERROR", "Unable to get process stats. Check to make sure "&$ProcessName&" is running.") Exit EndIf Else $ProcessList = ProcessList ($ProcessName) If @error Then MsgBox($MB_ICONERROR, "ERROR", "Unable to get process stats. Check to make sure "&$ProcessName&" is running.") Exit EndIf EndIf If IsArray ($ProcessList) Then _ArrayDisplay ($ProcessList) $iRaws = UBound ( $ProcessList ) For $i = 1 To $iRaws -1 $aMemStatsProcessID = ProcessGetStats($ProcessList[$i][1]) $aMemStatsProcessName = ProcessGetStats($ProcessList[$i][0]) $aMemStats1 = ProcessGetStats($ProcessList[$i][1]) If IsArray ($aMemStats1) Then $Message = $aMemStats1[1] $ProcessName = $ProcessList[$i][0] ;MsgBox($MB_ICONWARNING,"Addition", $Message&" "&$ProcessName&" PID:" &$ProcessList[$i][1] ) $Message &=' + ' $Message &= $aMemStats2 $Message &= ' = ' $Message &= $aMemStats1[1]+$aMemStats2 ;MsgBox(0,"Addition", $Message ) $aMemStats2 = $aMemStats2+$aMemStats1[1] ;MsgBox(0,"$aMemStats2",$aMemStats2) EndIf Next EndIf ;Convert bytes to Mo $nMemKB = $aMemStats2 / 8388608 $nMemKB = Round($nMemKB ,0) ;$nMemKB = $aMemStats2 / 1048576 $Message = $aMemStats2&'b / 8388608b(=1Mo) ='&$nMemKB&' Mo' MsgBox($MB_ICONWARNING, "b / Mo",$Message) ;If threshhold exceeded $Limite = 4300 If $nMemKB > $Limite Then ;Display message and exit MsgBox($MB_ICONWARNING, "WARNING", "Firefox memory limit (/"&$Limite&" Mo) exceeded!" & @CRLF & "Working set = " & $nMemKB &"/"&$Limite&" Mo" ) Exit EndIf ;Wait a second Sleep(1000) WEnd Edited February 3, 2019 by caramen My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
TheXman Posted February 3, 2019 Share Posted February 3, 2019 (edited) 4 hours ago, caramen said: Why it s not same as my tskmgr ? I have to assume that you are asking why doesn't your calculated "working set" memory, in megabytes, match what's displayed in your task manager. If that's true, what is the heading title of the memory column that you are looking at in the task manager? Is it "Working Set (memory)"? If it is not, then change it to "Working Set (memory)" and see if it matches. Edited February 3, 2019 by TheXman CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
caramen Posted February 3, 2019 Share Posted February 3, 2019 My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
TheXman Posted February 3, 2019 Share Posted February 3, 2019 (edited) @caramen expandcollapse popup#include <Constants.au3> #include <Array.au3> Const $PROCESS_NAME = "firefox.exe" Const $MB_LIMIT = 250 Global $aMemStats , $aProcessList Global $nMemKB, $nMemMB While 1 ;Get process list $aProcessList = ProcessList ($PROCESS_NAME) If Not IsArray($aProcessList) Or $aProcessList[0][0] = 0 Then MsgBox($MB_ICONERROR, "ERROR", StringFormat('Unable to get process list for "%s".', $PROCESS_NAME)) Exit EndIf ;Calculate total kb of processes $nMemKB = 0 For $i = 1 To $aProcessList[0][0] $aMemStats = ProcessGetStats($aProcessList[$i][1]) $nMemKB += ($aMemStats[0] / 1024) Next ;Convert bytes kb to mb $nMemMB = $nMemKB / 1024 ConsoleWrite(StringFormat("Total KB = %i", $nMemKB) & @CRLF) ConsoleWrite(StringFormat("Total MB = %i", $nMemMB) & @CRLF) ;If threshhold exceeded If $nMemMB > $MB_LIMIT Then ;Display message and exit MsgBox($MB_ICONWARNING, "WARNING", _ StringFormat("Firefox memory limit exceeded.\nActual MB = %i\nLimit MB = %i", $nMemMB, $MB_LIMIT) _ ) Exit EndIf ;Wait a second Sleep(1000) WEnd Edited February 3, 2019 by TheXman Fixed a typo caramen 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "Any fool can know. The point is to understand." -Albert Einstein "If you think you're a big fish, it's probably because you only swim in small ponds." ~TheXman Link to comment Share on other sites More sharing options...
caramen Posted February 3, 2019 Share Posted February 3, 2019 nice My video tutorials : ( In construction ) || My Discord : https://discord.gg/S9AnwHw How to Ask Help || UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote Spoiler Water's UDFs:Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now