Jump to content

5-6MB process?!


 Share

Recommended Posts

So I knocked up a script to capture keystrokes sent by the hardware buttons on my Tablet PC so I can get some sort of application-specific functionality. It's fairly short (111 lines). It does involve an infinite loop (I keep it running in order to capture said keystrokes), but it only loops every half a second (I added Sleep(500) so it wouldn't eat up all my CPU cycles).

That said, you can understand my shock when I took a look at the task manager and saw that the uncompiled script was eating almost 5MB (4792kb) of RAM. I thought that maybe compiling it would cut that down, but it only served to increase the RAM usage (went up to almost 6MB compiled).

Am I missing something, or is this normal behavior?

Link to comment
Share on other sites

So I knocked up a script to capture keystrokes sent by the hardware buttons on my Tablet PC so I can get some sort of application-specific functionality. It's fairly short (111 lines). It does involve an infinite loop (I keep it running in order to capture said keystrokes), but it only loops every half a second (I added Sleep(500) so it wouldn't eat up all my CPU cycles).

That said, you can understand my shock when I took a look at the task manager and saw that the uncompiled script was eating almost 5MB (4792kb) of RAM. I thought that maybe compiling it would cut that down, but it only served to increase the RAM usage (went up to almost 6MB compiled).

Am I missing something, or is this normal behavior?

<{POST_SNAPBACK}>

most likely it is something in your code. can't help troubleshoot without seeing code though...
Link to comment
Share on other sites

most likely it is something in your code.  can't help troubleshoot without seeing code though...

<{POST_SNAPBACK}>

Doh! Forgot to include that. :"> Here we go:

; reassign tablet buttons for application-specific functions

AutoItSetOption("WinTitleMatchMode",4);allow matching of classname

While 1
  HotKeySet("{DOWN}","RockerDown")
  HotKeySet("{UP}","RockerUp")
  HotKeySet("{ENTER}","RockerIn")
  HotkeySet("!{DOWN}","RockerAltDown")
  HotKeySet("!{UP}","RockerAltUp")
  HotKeySet("!{ENTER}","RockerAltIn")
  Sleep(500)
WEnd

Func RockerDown();Rocker Switch Down
  If WinActive("classname=Photoshop") Then
    Send("[")
  ElseIf WinActive("classname=PSDocC") Then
    Send("[")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^z")
  ElseIf WinActive("classname=Ink Art") Then
    Send("z")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^z")
  Else
    HotKeySet("{DOWN}")
    Send("{DOWN}")
    HotKeySet("{DOWN}","RockerDown")
  EndIf
EndFunc

Func RockerAltDown();Rocker Switch Down+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^-")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^-")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^0")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^-")
  Else
    HotKeySet("!{DOWN}")
    Send("!{DOWN}")
    HotKeySet("!{DOWN}","RockerAltDown")
  EndIf
EndFunc

Func RockerUp();Rocker Switch Up
  If WinActive("classname=Photoshop") Then
    Send("]")
  ElseIf WinActive("classname=PSDocC") Then
    Send("]")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^y")
  ElseIf WinActive("classname=Ink Art") Then
    Send("y")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^y")
  Else
    HotKeySet("{UP}")
    Send("{UP}")
    HotKeySet("{UP}","RockerUp")
  EndIf
EndFunc

Func RockerAltUp();Rocker Switch Up+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^!{+}")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^!{+}")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^!0")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^{+}")
  Else
    HotKeySet("!{UP}")
    Send("!{UP}")
    HotKeySet("!{UP}","RockerAltUp")
  EndIf
EndFunc

Func RockerIn();Rocker Switch Pushed In
  If WinActive("classname=Photoshop") Then
    Send("x")
  ElseIf WinActive("classname=PSDocC") Then
    Send("x")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("s")
  ElseIf WinActive("Corel Painter IX") Then
    Send("+x")
  Else
    HotKeySet("{ENTER}")
    Send("{ENTER}")
    HotKeySet("{ENTER}","RockerIn")
  EndIf
EndFunc

Func RockerAltIn();Rocker Switch In+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^h")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^h")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^0")
  Else
    HotKeySet("!{ENTER}")
    Send("!{ENTER}")
    HotKeySet("!{ENTER}","RockerAltIn")
  EndIf
EndFunc

I'm actually wondering if the IF..THEN loop isn't the most efficient way to go. :whistle:

Edited by LoTekK
Link to comment
Share on other sites

Doh! Forgot to include that. :"> Here we go:

; reassign tablet buttons for application-specific functions

AutoItSetOption("WinTitleMatchMode",4);allow matching of classname

While 1
  HotKeySet("{DOWN}","RockerDown")
  HotKeySet("{UP}","RockerUp")
  HotKeySet("{ENTER}","RockerIn")
  HotkeySet("!{DOWN}","RockerAltDown")
  HotKeySet("!{UP}","RockerAltUp")
  HotKeySet("!{ENTER}","RockerAltIn")
  Sleep(500)
WEnd

Func RockerDown();Rocker Switch Down
  If WinActive("classname=Photoshop") Then
    Send("[")
  ElseIf WinActive("classname=PSDocC") Then
    Send("[")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^z")
  ElseIf WinActive("classname=Ink Art") Then
    Send("z")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^z")
  Else
    HotKeySet("{DOWN}")
    Send("{DOWN}")
    HotKeySet("{DOWN}","RockerDown")
  EndIf
EndFunc

Func RockerAltDown();Rocker Switch Down+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^-")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^-")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^0")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^-")
  Else
    HotKeySet("!{DOWN}")
    Send("!{DOWN}")
    HotKeySet("!{DOWN}","RockerAltDown")
  EndIf
EndFunc

Func RockerUp();Rocker Switch Up
  If WinActive("classname=Photoshop") Then
    Send("]")
  ElseIf WinActive("classname=PSDocC") Then
    Send("]")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^y")
  ElseIf WinActive("classname=Ink Art") Then
    Send("y")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^y")
  Else
    HotKeySet("{UP}")
    Send("{UP}")
    HotKeySet("{UP}","RockerUp")
  EndIf
EndFunc

Func RockerAltUp();Rocker Switch Up+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^!{+}")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^!{+}")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("^!0")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^{+}")
  Else
    HotKeySet("!{UP}")
    Send("!{UP}")
    HotKeySet("!{UP}","RockerAltUp")
  EndIf
EndFunc

Func RockerIn();Rocker Switch Pushed In
  If WinActive("classname=Photoshop") Then
    Send("x")
  ElseIf WinActive("classname=PSDocC") Then
    Send("x")
  ElseIf WinActive("Alias SketchBook Pro") Then
    Send("s")
  ElseIf WinActive("Corel Painter IX") Then
    Send("+x")
  Else
    HotKeySet("{ENTER}")
    Send("{ENTER}")
    HotKeySet("{ENTER}","RockerIn")
  EndIf
EndFunc

Func RockerAltIn();Rocker Switch In+Alt Modifier
  If WinActive("classname=Photoshop") Then
    Send("^h")
  ElseIf WinActive("classname=PSDocC") Then
    Send("^h")
  ElseIf WinActive("Corel Painter IX") Then
    Send("^0")
  Else
    HotKeySet("!{ENTER}")
    Send("!{ENTER}")
    HotKeySet("!{ENTER}","RockerAltIn")
  EndIf
EndFunc

I'm actually wondering if the IF..THEN loop isn't the most efficient way to go. :whistle:

<{POST_SNAPBACK}>

yeah the first thing i see is alot of if's..... you should check out Select Case statements... just check out Select() in the help file....also, you're setting your hotkeys over and over and over.... you should scoot the hotkeyset()'s above your while, the loop could just have the sleep in it... see if that helps...
Link to comment
Share on other sites

yeah the first thing i see is alot of if's..... you should check out Select Case statements... just check out Select() in the help file....also, you're setting your hotkeys over and over and over.... you should scoot the hotkeyset()'s above your while, the loop could just have the sleep in it... see if that helps...

<{POST_SNAPBACK}>

Ah, I think I was confused as to how HotKeySet worked. I'll go ahead and try that. As for the loops, I have to admit that I've actually never ever really known when Select...Case would be a more efficient conditional than If...Then...Else. I'll give that a shot, too, and I'll report back. Thanks!

Edit:

Well, swapped the If..Then for Select..Case, and shifted the HotKeySet declarations to be called before the While loop, but the process still eats up the same amount of RAM (hovers arond 4800kb). :hmm:

Edited by LoTekK
Link to comment
Share on other sites

Ah, I think I was confused as to how HotKeySet worked. I'll go ahead and try that. As for the loops, I have to admit that I've actually never ever really known when Select...Case would be a more efficient conditional than If...Then...Else. I'll give that a shot, too, and I'll report back. Thanks!

<{POST_SNAPBACK}>

all you really have to do is like:

Opt("WinTitleMatchMode",4)

$title = WinGetTitle("active","")
Select
case $title = "app1"
;... app1 code
case $title = "app2"
;... app2 code
case $title = "app3"
;... app3 code
case $title = "app4"
;... app4 code
EndSelect
Link to comment
Share on other sites

I took a look at the Memory Reduction thingie, and it looks pretty nifty, but I'm a little confused as to how often it should get called from the script (this is what happens when you let an artist code :whistle:).

edit: Huh. Just took a look at the task manager again, and the script seems to have dropped to around 1700kb. This is without having tried the memory reduction doodad.

Edited by LoTekK
Link to comment
Share on other sites

I took a look at the Memory Reduction thingie, and it looks pretty nifty, but I'm a little confused as to how often it should get called from the script (this is what happens when you let an artist code :whistle:).

edit: Huh. Just took a look at the task manager again, and the script seems to have dropped to around 1700kb. This is without having tried the memory reduction doodad.

<{POST_SNAPBACK}>

I don't think you need to put it in your while loop, just because i'm sure it doesn't need to be run THAT often, but maybe add the function to your script, and add a call to it in your other functions, so that after your hotkey is processed, the cleanup is run..
Link to comment
Share on other sites

I don't think you need to put it in your while loop, just because i'm sure it doesn't need to be run THAT often, but maybe add the function to your script, and add a call to it in your other functions, so that after your hotkey is processed, the cleanup is run..

<{POST_SNAPBACK}>

I seem to be quite the dolt. I get an Unknown Function error when I drop the function declaration in and run the script. I have psapi.dll, which the forum thread mentions is needed. I'm using Wouter's latest version of the function.

Posted Image

Side note: I tried running the script again, without the memreduce function, and unlike the last time I checked, it's actually gone up again, to about 5500kb, instead of the previous 1700kb. :whistle:

Edited by LoTekK
Link to comment
Share on other sites

I seem to be quite the dolt. I get an Unknown Function error when I drop the function declaration in and run the script. I have psapi.dll, which the forum thread mentions is needed. I'm using Wouter's latest version of the function.

Posted Image

Side note: I tried running the script again, without the memreduce function, and unlike the last time I checked, it's actually gone up again, to about 5500kb, instead of the previous 1700kb. :whistle:

<{POST_SNAPBACK}>

could you post your newest code, also are you compiling this before running, or just executing as an .au3 and looking at how much is being taken up by autoit?
Link to comment
Share on other sites

could you post your newest code, also are you compiling this before running, or just executing as an .au3 and looking at how much is being taken up by autoit?

<{POST_SNAPBACK}>

I've been trying both script as well as compiled .exe. The compiled version actually ends up eating more RAM than the uncompiled script. Here's the code with the Memory Reduction snippet included.

; reassign tablet buttons for application-specific functions

AutoItSetOption("WinTitleMatchMode",4);allow matching of classname

HotKeySet("{DOWN}","RockerDown")
HotKeySet("{UP}","RockerUp")
HotKeySet("{ENTER}","RockerIn")
HotkeySet("!{DOWN}","RockerAltDown")
HotKeySet("!{UP}","RockerAltUp")
HotKeySet("!{ENTER}","RockerAltIn")

While 1
  Sleep(500)
WEnd

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()

Func RockerDown();Rocker Switch Down
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("[")
  Case WinActive("classname=PSDocC")
    Send("[")
  Case WinActive("Alias SketchBook Pro")
    Send("^z")
  Case WinActive("classname=Ink Art")
    Send("z")
  Case WinActive("Corel Painter IX")
    Send("^z")
  Case Else
    HotKeySet("{DOWN}")
    Send("{DOWN}")
    HotKeySet("{DOWN}","RockerDown")
  EndSelect
EndFunc

Func RockerAltDown();Rocker Switch Down+Alt Modifier
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("^-")
  Case WinActive("classname=PSDocC")
    Send("^-")
  Case WinActive("Alias SketchBook Pro")
    Send("^0")
  Case WinActive("Corel Painter IX")
    Send("^-")
  Case Else
    HotKeySet("!{DOWN}")
    Send("!{DOWN}")
    HotKeySet("!{DOWN}","RockerAltDown")
  EndSelect
EndFunc

Func RockerUp();Rocker Switch Up
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("]")
  Case WinActive("classname=PSDocC")
    Send("]")
  Case WinActive("Alias SketchBook Pro")
    Send("^y")
  Case WinActive("classname=Ink Art")
    Send("y")
  Case WinActive("Corel Painter IX")
    Send("^y")
  Case Else
    HotKeySet("{UP}")
    Send("{UP}")
    HotKeySet("{UP}","RockerUp")
  EndSelect
EndFunc

Func RockerAltUp();Rocker Switch Up+Alt Modifier
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("^!{+}")
  Case WinActive("classname=PSDocC")
    Send("^!{+}")
  Case WinActive("Alias SketchBook Pro")
    Send("^!0")
  Case WinActive("Corel Painter IX")
    Send("^{+}")
  Case Else
    HotKeySet("!{UP}")
    Send("!{UP}")
    HotKeySet("!{UP}","RockerAltUp")
  EndSelect
EndFunc

Func RockerIn();Rocker Switch Pushed In
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("x")
  Case WinActive("classname=PSDocC")
    Send("x")
  Case WinActive("Alias SketchBook Pro")
    Send("s")
  Case WinActive("Corel Painter IX")
    Send("+x")
  Case Else
    HotKeySet("{ENTER}")
    Send("{ENTER}")
    HotKeySet("{ENTER}","RockerIn")
  EndSelect
EndFunc

Func RockerAltIn();Rocker Switch In+Alt Modifier
  _ReduceMemory()
  Select
  Case WinActive("classname=Photoshop")
    Send("{ALTUP}")
    Sleep(10)
    Send("{ALTDOWN}")
    Sleep(10)
    Send("{ALTUP}")
    Sleep(10)
    Send("^h")
  Case WinActive("classname=PSDocC")
    Send("{ALTUP}")
    Sleep(10)
    Send("{ALTDOWN}")
    Sleep(10)
    Send("{ALTUP}")
    Sleep(10)
    Send("^h")
  Case WinActive("Corel Painter IX")
    Send("{ALTUP}")
    Sleep(10)
    Send("{ALTDOWN}")
    Sleep(10)
    Send("{ALTUP}")
    Sleep(10)
    Send("^0")
  Case Else
    HotKeySet("!{ENTER}")
    Send("!{ENTER}")
    HotKeySet("!{ENTER}","RockerAltIn")
  EndSelect
EndFunc
Edited by LoTekK
Link to comment
Share on other sites

It's giving you an error at False. This is a keyword added only recently to the beta, not available in the stable release. What version of AutoIt are you running?

MsgBox(0, 'AutoIt Version', @AutoItVersion)

<{POST_SNAPBACK}>

It's reporting 3.1.1.0.

Also, just wanted to thank everyone for your patience. I know some of the questions I'm asking must seem pretty silly. :">

Link to comment
Share on other sites

Awesome, thanks for the heads up. The script is now running at slightly more than 10% of the previous memory consumption (between 550 and 650kb). :whistle: I should change it so that the memreducer gets called on a steady interval, though, as you suggested, instead of at every keypress, as I have it set up now (I've noticed it taking more cycles than it should for each keypress).

Thanks once again for everyone's help and patience. :dance:

edit:

Kay, I've got it setup now to run _ReduceMemory when it starts up, then stuck in a timer to call the function every minute. Compiled, it starts up at about 750kb or thereabouts, manages to creep up to about 1500kb before dropping back to 750-ish every minute. Fine by my books. :dance:

edit2:

On second thoughts, I went back to the original plan of calling _ReduceMemory each time it redirects a keypress, since it appears that was the only time memory usage jumped to 1500kb.

Edited by LoTekK
Link to comment
Share on other sites

Did it not occur to you that since the memory keeps going back to that value, the program might actually need those pages loaded into memory? Calling it once at startup (After everything is loaded) is enough or if it's a GUI call it whenever a GUI is minimized. Other than that, you may be "saving" memory but you are killing performance because your CPU has to reload the pages it needs back into memory every time because you keep unloading them.

Link to comment
Share on other sites

Did it not occur to you that since the memory keeps going back to that value, the program might actually need those pages loaded into memory?  Calling it once at startup (After everything is loaded) is enough or if it's a GUI call it whenever a GUI is minimized.  Other than that, you may be "saving" memory but you are killing performance because your CPU has to reload the pages it needs back into memory every time because you keep unloading them.

<{POST_SNAPBACK}>

:">

No, it didn't occur to me. Never let an artist code, especially at 4 in the morning. :dance: Thanks for the tip. :whistle:

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