Memory Issue when repeating a "Type a Keystroke" command

I found a similar thread (High Memory Usage During Repetitive Keystrokes) but rather than necro it, I thought I would start a new thread.

I'm on Mac OS 12.4 running KM Version 10.1.1

Essentially, if I start a loop and have it simply "Type a keystroke" of the letter "a" over and over, I can watch KM's memory climb in activity monitor until the entire system hangs.

However, if I do the same thing and run Applescript with:

tell application "System Events" to keystroke "a" 

I don't see the same issue, but at some point will likely get an abort with:

Action 63319 failed: Execute an AppleScript failed with script error: text-script:34:45: execution error: System Events got an error: Connection is invalid. (-609)

So I can use the KM "Type a Keystroke" and it will work great until the system runs out of memory or use the Applescript and be good until the OS barfs.

Any suggestion on a solution? Ideally one that uses the built in keystroke command without the memory issues?

Additional notes:

  • Insert Text by Typing exhibits the same behavior as Type a Keystroke (memory steadily increases).
  • Ending the macro/loop does not free the memory - only restarting the KM engine does.
  • Version 9.2 appears to exhibit the same issues

Howdy, welcome to the forum! As you might have already figured out, this is a great community full of people who love to help.

I'm going to start with the most simple of options... have you logged out/logged back in and or rebooted?

For odd things like that a clean Keyboard Maestro install would also be a fairly simple option.


What's the use-case for this? That will help in the search for an appropriate solution.

I have. I've restarted the machine, made sure it and KM were both updated, tested in 9.2 and clean installed per the suggestions here on the forums.

I've also verified no other macros are running and used a clean restart of the KM engine.

No problem!

The use case is almost exactly what I presented above. I have a game where I use a cmd+shift+letter command to toggle on the repeated press of a key (or set of keys) until I turn them off.

For my real life use case I actually add a bit of a delay, which prevents the memory creep from happening as fast, but it still happens.

Here's an example.

Screen Shot 2022-07-25 at 8.45.53 AM

If I do this instead, it will not memory leak but will eventually error out with the error:

Action 63319 failed: Execute an AppleScript failed with script error: text-script:34:45: execution error: System Events got an error: Connection is invalid. (-609)

Screen Shot 2022-07-25 at 8.48.10 AM

That, in a round-about way, was the point of the question -- how much of a delay can you stand? The extra info would be "and how many keypresses is your maximum need?".

I'm wondering if you're submitting events faster than they can be processed by the app, leading to a buildup of either queued or "not completed" (where the app silently drops them) events, hence the bloat. I don't know much (anything!) about how KM does things "behind the scenes" but you may be in for some process management, maybe increasing the delay as required to control memory usage.

Even a delay of 1 second on a single keystroke exhibits the behavior. And the AppleScript execution is much slower and doesn't have the same problem.

So I am hesitant to believe it is a timeout issue.

What's the reason for not using the keychord itself in the loop (checking for some other terminator)? As in:

I don't need to simulate a cmd+L.

(Let's use "a" since "l" looks like "I").

Here's what I need to do:

  • Press a key combo - cmd+shift+a
  • Start a loop that presses the "a" key every .1 seconds until
  • I press the cmd key, which should cancel the loop


Functionally, it works exactly as expected. However, I ran it for 60 seconds, typing a single letter over and over into sublime text editor without the delay (and no other macros in between) and this is what I see:

Start Memory: 22.5 MB private / 44.9 MB shared
End Memory: 31.5 MB private / 331 MB shared

start memory

end memory

OK, but isn't that what your While loop is doing? You're holding down the Command key and typing a letter (and waiting a tenth of second after).

When what you want to do, it seems, is simply continually send a letter and then wait a tenth of a second.

I'd purge the modifier as a test for the loop.

The while loop is continually pressing "a" until I press the cmd key (which then breaks the loop).

Ah, missed that. Sorry.

I would, though, still lose the modifier and try some other test. It's a modifier after all.

At mrpasini request, removed the conditional while. Updated macro to simply loop 4000 times. Removed the .1 delay to speed things up.

Restarted KM engine. Ran the macro. Took about 40 seconds to complete.

Start Memory: 23.1 MB private / 42.7 MB shared
End Memory: 23.9 MB private / 194.8 MB shared

start loop

end loop

And a screenshot of the text editor showing that no keypresses were missed or timed out:

I should have mentioned this earlier, but take a look at the Keyboard Maestro log for a clue.

Nothing in the logs. I tail them regularly when working with KM. Here's an except from earlier today:

2022-07-25 10:16:22 Execute macro “Loop Test” from trigger The Hot Key ⇧⌘A is pressed
2022-07-25 10:16:22 Log: ----- Start Loop -----
2022-07-25 10:16:47 Log: ----- End Loop -----

Did you walk away and came back later?

I was playing with this yesterday and, for me at least, I could run your original macro for a couple of minutes, end it by pressing the ⌘ key, and characters continued to be typed into Script Editor for ages. That suggests to me that KM is buffering simulated keypresses and managing their submission to the event queue -- the memory bloat is possibly the buffering.