How to Generate a Memory Leak in the Keyboard Maestro Engine

After poking around in the KM Editor for an hour or two I finally found a way to cause a memory leak in the KM Engine. I have no idea if this will work for anyone else, but it works for me.

All I have to do is click the check mark for a macro group to enable or disable the group. Each time I click on it, according to the Activity Monitor application, the KM Engine grabs another chuck of memory, typically around 50 MB, but it can range from 20 MB to 100 MB. Within a couple of minutes of clicking I can get the KM Engine to take up 5 GB of memory. At that point my system becomes sluggish, as I have only 8 GB of RAM. So it gets hard to make the engine go higher, but I have seen it as high as 8 GB this week.

Upon further examination it even leaks memory when the macro group is empty! But not quite as much memory. It seems that the amount of memory leaking appears to be related to how many macros are in the group. Yup, if my group has about 100 macros, the amount of memory lost is about 100 MB per click. Per click!! :scream:

This is a very specific report. It should be investigatable. I really don't know if anyone else will see this behaviour. Maybe it's just me. Maybe my macOS is broken.

I cannot reproduce that behavior, running Keyboard Maestro 8.2.4 on macOS 10.14.5.

The KM Engine does briefly increase memory by a modest amount, ~5MB (nothing ever > 10MB), but then quickly reduces the memory after I stop clicking on the enable/disable button.

So I don't see this as a "memory leak", but simply normal behavior as the KM Engine is responding to user input.

I should have explained my environment. It is KM 8.2.4 on macOS 10.14.5. Like yours.

I'm glad you tested it on your system, thanks. I agree that your copy of the KM Engine is not leaking. At the same time, it still appears that my copy of the Engine is leaking. But that may be something else with my system triggering this behaviour. Or maybe my KM copy is somehow is corrupted.

Do you think my next step is for me to reinstall KM, or to wait for PeterNLewis to chime in? I suppose I'm willing to reinstall macOS also.

I think I'll wait until at least one more person runs this test and reports whether it's a memory leak for them or not. I'm sure someone will volunteer to do that before I start reinstalling things.

Same environment: KM 8.2.4 on macOS 10.14.5. Before the test the KM Engine was using 1.10GB of memory, which does seem higher than I would expect, but enabling and disabling my biggest macro group containing 187 macros was instantaneous no matter how many times I did it and seemingly had no effect at all on how much memory the KM Engine used.

I will say that I did actually notice the KM Engine taking up around 5GB of memory sometime in the last few weeks, but in my experience, that is very unusual. I've been using KM for maybe 7-8 years at this point and I can count the number of times that sort of excessive memory usage has happened on one hand, so I don't think it's normal or expected behavior.

Thanks. I consider your results to be a little more on the memory leak side than the no leak side because either 1.1GB or 5 GB is, I would guess, way more than Peter would ever expect to see. Perhaps 1.1 GB isn't so bad if you have lots of macros enabled. Did you say earlier in another post how many macros you have enabled? I think that information would be useful here.

Looking at my Enabled Macros smart group now, it looks like I have 483 out of 873 macros enabled at the moment, though I'm fairly sure I'm only actively using a fraction of those.

Ok, well I disabled everything, just five macros left active, and I still have a memory leak. But it's small. I got the Engine to rise from the baseline of 134 MB to 190 MB after about 50 or so enable/disable clicks. So for additional testing I found a Macro Group with about 100 macros in it (all of which have NO triggers) and each time I click on the Enable/Disable group I'm getting about 5 MB of lost RAM. That's less than the 100 MB I reported earlier. Then I tried a different folder with seven macros (all with No triggers) and each click causes about 20 MB of lost RAM. The correlation of leak to the number of macros in the group appears to be there, if a little unclear how or why that matters.

When doing this test you need to be aware that the Activity Monitor app doesn't update instantly. Sometimes you have to wait about 5 seconds before the new memory numbers show up.

With easily >100 clicks (more like 200) on different groups I was able to “pump up” KM Engine from 68MB to 224MB. Since the 68MB were immediately after a system wake and the 224MB are entirely in my normal range, I wouldn’t call this a memory leak.

After waiting ~10 minutes I added again roughly 100 clicks and only could get it up to 235MB. So, pretty definitely no leak here, rather like @JMichaelTX has said: “the KM Engine is responding to user input”.

I also tested it explicitly on a group with 12 macros, all without trigger, and no leak here either.

macOS 10.14.5; KM 8.2.4

Thanks for the helpful information. Perhaps when I trim my Macros.plist file down from 72 MB that may help me. We'll see soon.

Yes, the Keyboard Maestro Engine proactively caches things after launch (stuff like application lists and icons and such) so that when later asked it has the information at hand. So you should expect the memory usage to rise for a period of time after use.

Also, Keyboard Maestro lazily loads whatever facilities it can, so the first use of any facility may well cause a memory increase that is not a leak.

Xcode cannot detect any appreciable leaks in my testing, even though Activity Monitor shows increasing memory usage, so I don't really know what the situation is.

As long as you are aware of this behaviour, I'm content. I've reduced my memory footage by about half, by removing most macros from the Editor, and I'm still getting about 2 MB memory increase per click on enabling an empty macro Group.

72MB, really?!

Mine has 1.6MB (for 646 macros).

I suspect you don't have a lot of images stored in your macros. I do. There's no way for me to count, but I probably have hundreds of images the average size of your thumbnail. And as far as I can tell, Peter can't used compressed images for image storage, because image searching is more accurate when you don't damage the image with compression, so he probably uses uncompressed images internally. Image compression typically reduces storage by about 10x. So, as a result, images stored in the Plist file are 10x larger than what people would be used to seeing, if they are uncompressed. If he could compress images then my file would probably be more like 7 MB.

Even though I've reduced my Plist to about 10 MB, I'm still getting the KM Engine reaching 3 GB of RAM about once per hour. It brings my system to a crawl, (which is how I notice it) but it's easy to fix by restarting the engine. I suppose I could reduce my file to 1MB, to see if I'm still getting 3 GB of RAM used by the Engine.

Images are stored as compressed tiffs.

If it is growing that much that quickly, there must be stuff going on a lot in the hour. What is happening over that hour? Presumably you're not just sitting there toggling the enable on a macro group all hour.

If you can narrow down the behaviour that causes the leak, then I can have a chance to fix it.

You are right I'm not just clicking on the button to enable/disable groups. Instead I am writing macros that take plain ascii text out of old public domain books and paste them into Keynote for easier viewing. It's just basic text processing. With a little bit of Application switching. There are no images involved. But as far as I can recall I've had this problem for years, every day, no matter what kind of macros I'm working on. I don't recall if the memory is leaking when I'm not using the computer, I should take note of that. I'll make note what the memory level is at when I'm done for the night and check in the morning.

I'm not really sure what to suggest. Your experience seems quite different to other folks, so that is why I'd like to know what is different about your Mac or your macros or your behaviour that results in such a significant leak.

I have two big old Mac computers sitting next to me and I could turn them on and see if they also have the problem, even though they aren't sharing the same collection of macros that I have here on this Mac. Which might make it a good test. Maybe tomorrow, tonight's almost done.

So, the problem does persist across OS updates? And over different KM versions?

Since I don't really pay attention to specific release dates for OSs or for KM, I should be cautious saying yes here. But I think if there's a way to search these forums for old posts from old users like me, you will see I specifically raised this issue years ago. So if we can find those posts we can put a minimum duration for this problem.

Overnight, with all macros disabled, my KM Engine levitated at its new baseline of 44 MB ram.