Memory Issue when repeating a "Type a Keystroke" command

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

Example:

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.

The loop version from here (Memory Issue when repeating a "Type a Keystroke" command - #16 by ericv) runs in about 40 seconds and didn't cause any buffering or UI issues in Sublime text editor.

But as noted here (Memory Issue when repeating a "Type a Keystroke" command - #9 by ericv) even if I set the delay to 1 second (which is huge for a simple keypress macro), if I walk away it continues to build up the shared memory usage until the system becomes unresponsive.

Appreciate you trying it! Did ending the macro and letting the buffer finish free up the memory? In my experience, the only way to free that shared memory is to restart KM Engine. I have left it for hours hoping for some trash collection or something to grab it, but it never cleans itself up.

Yeah, I've just plonked BBEdit onto my test machine and that reacts a lot more quickly than Script Editor, with no visible buffering. Edit to add: That was on a short test -- on a longer run -- 100,000+ characters -- or, perhaps, as memory use climbs, buffering becomes apparent.

That doesn't mean it isn't buffering, though! IIRC the system's Events buffer is pretty limited and so the Script Editor test suggests there's buffering in KM (which would explain the climb in memory usage). 150-200 keystroke events per second is somewhat beyond "emulating normal user interaction", so I'm not surprised we don't usually notice it!

My (very basic!) understanding of "modern" memory management is that apps don't need to "free" unused memory until they exit or the OS grabs it back for use elsewhere. So my hope was that running the macro repeatedly would keep re-using that extra buffer space. Unfortunately not -- it keeps adding to it.

Hmmm... 1GB of memory use is ~65,000 characters, ie 2^16 -- that's a very computery number and, perhaps, a clue...

I've got to ask again -- how many keypresses do you actually need? While this is a fun exercise, and I'm happy to keep poking, my 8GB Air has managed to type 500,000+ ls into BBEdit with no sign of stopping... That's an excessive amount of keypressing for even the worst no-lifer game routine!