jftuga Posted July 10, 2005 Share Posted July 10, 2005 (edited) I really love AutoIt, but always thought that the scripts took up too much memory. One day I stumbled across empty.exe from the 2003 resource kit, which essentially frees unused memory from a process. I had this discussion and this one with Valik and w0uter which resulted in the UDF below. Basically, it uses all of w0uter's code The results are spectacular -- usually a 50% reduction in memory with no noticable performance degradation. Memory usage, as reported by Task Manager, does grow over time, but not by that much.I have attached the code as well as pasted it.Enjoy!-JohnEDIT - DON'T USE THIS CODE, SCROLL DOWN AND USE THE SHORTENED VERSION.expandcollapse popup#cs _ReduceMemory UDF by John Taylor (jftuga) Jul-9-2005 This function, _ReduceMemory(), can reduce the amount of memory that an AutoIt compiled .exe uses by ~50% Essentially written by several other UDFs coded by w0uter :-) Usage: call _ReduceMemory() from your script. If it is a GUI app, call it just before entering the event loop See also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/emptyworkingset.asp #ce Global Const $PROCESS_ALL_ACCESS = 0x1f0fff ; from w0uter ; http://www.autoitscript.com/forum/index.php?showtopic=12651 Func _MemOpen($i_dwDesiredAccess, $i_bInheritHandle, $i_dwProcessId) Dim $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', $i_dwDesiredAccess, 'int', $i_bInheritHandle, 'int', $i_dwProcessId) If @error Then SetError(1) Return 0 EndIf Return $ai_Handle[0] EndFunc;==> _MemOpen() ; from w0uter ; http://www.autoitscript.com/forum/index.php?showtopic=13383 Func _GetPID() Dim $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId') return $ai_GetCurrentProcessId[0] EndFunc ; from w0uter ; http://www.autoitscript.com/forum/index.php?showtopic=13392 Func _ReduceMemory() Dim $Handle = _MemOpen($PROCESS_ALL_ACCESS, False, _GetPID()) return DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $Handle) EndFuncReduceMemory.au3 Edited July 10, 2005 by jftuga Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
w0uter Posted July 10, 2005 Share Posted July 10, 2005 just make it into 1 functions My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
MSLx Fanboy Posted July 10, 2005 Share Posted July 10, 2005 (edited) I like it, and to make w0uter a happy person, I took the liberty to git-r-don (I live about 30 minutes from Sanford, FL, home of Larry the Cable Guy ) Global Const $PROCESS_ALL_ACCESS = 0x1f0fff Func _ReduceMemory() Dim $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId') Dim $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', $PROCESS_ALL_ACCESS, 'int', False, 'int', $ai_GetCurrentProcessId[0]) If @error Then SetError(1) Return 0 EndIf return DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle) EndFunc That should work...I'll go and test it in a second... Edit: Is return DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle) supposed to return a blank string? Edited July 10, 2005 by MSLx Fanboy Writing AutoIt scripts since _DateAdd("d", -2, _NowCalcDate()) Link to comment Share on other sites More sharing options...
jftuga Posted July 10, 2005 Author Share Posted July 10, 2005 (edited) I like it, and to make w0uter a happy person, I took the liberty to git-r-don (I live about 30 minutes from Sanford, FL, home of Larry the Cable Guy )Many Blue Collar TV episodes are filmed here in Athens GA, although I have not been able to go to a taping, which are free.-John Edited July 10, 2005 by jftuga Admin_Popup, show computer info or launch shellRemote Manager, facilitates connecting to RDP / VNCProc_Watch, reprioritize cpu intensive processesUDF: _ini_to_dict, transforms ini file entries into variablesUDF: monitor_resolutions, returns resolutions of multiple monitorsReport Computer Problem, for your IT help deskProfile Fixer, fixes a 'missing' AD user profile Link to comment Share on other sites More sharing options...
MSLx Fanboy Posted July 10, 2005 Share Posted July 10, 2005 Some of their t.v. shows aren't that great, but their individual stand-ups are good! Writing AutoIt scripts since _DateAdd("d", -2, _NowCalcDate()) Link to comment Share on other sites More sharing options...
FuryCell Posted July 10, 2005 Share Posted July 10, 2005 (edited) Looks like a cool UDF. I will try it when I get back home from visting family in vermont as I only have my windows 98 laptop and it has no way of seeing memory usage in Task Manager (actually it does not evan have task manager.) Will have to try it on my XP computer. Edited July 10, 2005 by SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code. Link to comment Share on other sites More sharing options...
w0uter Posted July 10, 2005 Share Posted July 10, 2005 (edited) return DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle) should become Local $ai_return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) return $ai_return[0] also use local instead of Dim. total would be like: Func _ReduceMemory() Local $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId') Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ai_GetCurrentProcessId[0]) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) Return $ai_Return[0] EndFunc Edited July 10, 2005 by w0uter My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
MSLx Fanboy Posted July 10, 2005 Share Posted July 10, 2005 Oh Snap! Ran the code again, and Task Manager reported a drop from 3404 to 656, a near 520% decrease!!! I love this thing! Writing AutoIt scripts since _DateAdd("d", -2, _NowCalcDate()) Link to comment Share on other sites More sharing options...
w0uter Posted July 10, 2005 Share Posted July 10, 2005 forgot to close the handle: Func _ReduceMemory() Local $ai_GetCurrentProcessId = DllCall('kernel32.dll', 'int', 'GetCurrentProcessId') Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ai_GetCurrentProcessId[0]) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0]) Return $ai_Return[0] EndFunc My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
therks Posted July 13, 2005 Share Posted July 13, 2005 Oh Snap! Ran the code again, and Task Manager reported a drop from 3404 to 656, a near 520% decrease!!!I love this thing!<{POST_SNAPBACK}>I'm no math scholar... but isn't 520% of 3404 equal to 17700.8?And as such, a 520% drop from 3404 would be -14296.8, would it not?3404 to 656 is about an 81% drop. I believe you did your math backwards (divided 3404 by 656, when to get the percentage of drop you find the difference of 3404 and 656 (2748) and then divide that by 3404, giving you ~0.807)...But enough about that, I just thought it looked funny.BACK ON TOPIC...The function works great. I created a program with GUI that let's me create hotkeys for running programs and stuff (my own personal replacement for WinKey [i was going to find a link.. but it appears Copernic isn't providing it anymore, and Download.com doesn't have it either]). It was running with a hefty 4mb+ of memory, so I have it call _ReduceMemory() everytime the window is hidden/minimized and it drops it down to around 1800 (actually drops to about 1400, but it pops up to 1750-1800 right away). Awesome function, and one of the few non-personal UDFs that I'll keep on using. My AutoIt Stuff | My Github Link to comment Share on other sites More sharing options...
MSLx Fanboy Posted July 13, 2005 Share Posted July 13, 2005 Hehe, I was never too good with percentages Writing AutoIt scripts since _DateAdd("d", -2, _NowCalcDate()) Link to comment Share on other sites More sharing options...
lgodfrey Posted July 14, 2005 Share Posted July 14, 2005 Hehe, I was never too good with percentages <{POST_SNAPBACK}>I tried _ReduceMemory too, works great, drops mem useage in half for me.Ps, my engineers always messed up % change too, until I made them say ten times each day for a week "Percent change is new value minus old value over old value times 100%! RegardsLarry I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
FuryCell Posted July 15, 2005 Share Posted July 15, 2005 Finally got around to trying it and it works great. This should be added to the UDF libary that comes with AutoIt. You should post it here.P.S. Does anybody know of a good resource on dlls\com objects besides MSDN.-SolidSnake HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code. Link to comment Share on other sites More sharing options...
Zedna Posted July 15, 2005 Share Posted July 15, 2005 It's interesting.I have little optimizations idea:There is 3 times DllCall('kernel32.dll',...and it 3x internaly do DllOpen, DllCall, DllCloseso we can in the begining use DllOpen('kernel32.dll')then 3x DllCalland at end DllCloseI'm not sure with this on 100% just idea ...And note: This function works only on WinXP and above. Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
w0uter Posted July 15, 2005 Share Posted July 15, 2005 (edited) will do.[edit]tested it. and it was slower.[/edit][edit2]is MSDN wrong here => http://msdn.microsoft.com/library/default....yworkingset.aspit says that i need "PROCESS_QUERY_INFORMATION and PROCESS_SET_INFORMATION"but if i use them. it will say ACCESS DENIED. while PROCESS_ALL_ACCESS works.[/edit2] Edited July 16, 2005 by w0uter My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
Zedna Posted July 16, 2005 Share Posted July 16, 2005 @wOutertested it. and it was slower.Have you tried this?Func _ReduceMemory() Local $dll = DllOpen("kernel32.dll") Local $ai_GetCurrentProcessId = DllCall($dll, 'int', 'GetCurrentProcessId') Local $ai_Handle = DllCall($dll, 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $ai_GetCurrentProcessId[0]) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) DllCall($dll, 'int', 'CloseHandle', 'int', $ai_Handle[0]) DllClose($dll) Return $ai_Return[0] EndFunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
w0uter Posted July 16, 2005 Share Posted July 16, 2005 yep. well i used another variable for the dll. but evrything else is the same but does anyone know why "PROCESS_QUERY_INFORMATION and PROCESS_SET_INFORMATION" dont work My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
w0uter Posted July 16, 2005 Share Posted July 16, 2005 (edited) ok. did some digging. found "GetCurrentProcess". wich is faster then getting the pid and opening itself. noticed it always returned -1 so i just left that call out. also made it affect other processes. heres what i came up with. [edit] forgot to close the handle *again* [/edit] Func _ReduceMemory($i_PID = -1) If $i_PID <> -1 Then Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0]) Else Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1) EndIf Return $ai_Return[0] EndFunc;==> _ReduceMemory() some results: Word XP 14.076 KB -> 624 KB 3dsmax 7 142.356 KB -> 2.184 KB Notepad 4.796 KB -> 112 KB Edited July 16, 2005 by w0uter My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll Link to comment Share on other sites More sharing options...
Josbe Posted July 16, 2005 Share Posted July 16, 2005 ok. did some digging. found "GetCurrentProcess". wich is faster then getting the pid and opening itself.noticed it always returned -1 so i just left that call out.also made it affect other processes.heres what i came up with.[edit] forgot to close the handle *again* [/edit]Func _ReduceMemory($i_PID = -1) If $i_PID <> -1 Then Local $ai_Handle = DllCall("kernel32.dll", 'int', 'OpenProcess', 'int', 0x1f0fff, 'int', False, 'int', $i_PID) Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', $ai_Handle[0]) DllCall('kernel32.dll', 'int', 'CloseHandle', 'int', $ai_Handle[0]) Else Local $ai_Return = DllCall("psapi.dll", 'int', 'EmptyWorkingSet', 'long', -1) EndIf Return $ai_Return[0] EndFunc;==> _ReduceMemory()some results: Word XP14.076 KB -> 624 KB3dsmax 7142.356 KB -> 2.184 KBNotepad4.796 KB -> 112 KB<{POST_SNAPBACK}>Great result with 3dsmax. Nice UDF. AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta Link to comment Share on other sites More sharing options...
Josbe Posted July 16, 2005 Share Posted July 16, 2005 (edited) With this update: 16th July 2005 - v3.1.1.60 (beta) - (thxs: Valik)...this code would be:...You're right, w0uter. Edited July 18, 2005 by josbe AUTOIT > AutoIt docs / Beta folder - AutoIt latest beta 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