How to Generate a Memory Leak in the Keyboard Maestro Engine

The contents of any local variable are empty when you start running any macro. That's important here. When I used local variables that already existed, I didn't see the crashes, as I stated in a previous post.

So, you didn't initialize the variable before this search? Not to be like the doctor in that old joke who tells the patient it hurts when he does this to stop doing that, but how often do you need to search an empty or non-existent variable? Even if searching them does cause a crash, if there's no practical reason why you would ever need to do so aside from a test like this, I kind of fail to see the point. Am I just missing something?

I appreciate your humour. It's a good joke, along with the line "if it ain't broke, we cannot fix it."

I often use variables, even local variables, to accumulate data that I collect. So it is quite common for a macro to use a local text variable and assume it's empty at the start. Should I not be taking advantage of this language feature, because doing so will break the Engine?

I suspect that you, or someone, will be testing my code, to get a second opinion. Maybe you won't get the same results. Since we don't want average people downloading code that breaks the engine, I decided it was best to just include a screenshot, not upload the code itself.

Philosophical questions about how the engine ought to be used aside: would it make sense to test if the variable is empty before you try and search within it for a string? It's an extra action but it might prevent the issues you seem to have been having.

I fully understand what you are saying. That might be the best solution. On the other hand, this might be an actual bug in the Engine that can and should be fixed, in which case I don't have to modify hundreds of macros.

Do you think KM should allow us to use empty local variables? Because it's not just at the start of a macro where I would have to test this, it's every time I use the variable inside the macro. Every single time. Do we really want to have to write extra code to avoid using an empty variable in a common KM action?

I have roughly 1000 macros (most of which are archived at the moment) and if I have to modify all of them to check if a local text variable is empty before I use it in an action, that would be a terrible problem for me.

I'm a little surprised nobody has tested my Engine crash macro yet.

Could you please upload it?

(Export the macro, File > Export Macros, and drag the exported macro into the forum editor.)

OK, I tried to rebuild your macro, with this regex content: |(a-z), and I can confirm your result:

No increasing memory, but the engine becomes unresponsive quickly.

In this case the culprit seems to be the size of the variable. Your regex makes the variable content grow exponentially, and when it reaches a certain size, the engine becomes unresponsive. Since your local variable is inside the loop, it isn’t reset.

You can easily confirm it this way:

  1. Make your variable global (removing the “Local”)
  2. Remove the loop (either by inserting 1 in the calculation field or just take out the main action from the loop)
  3. Run the macro one time
  4. Now open your variable ZZ in Preferences > Variables
  5. Run the macro manually a couple of times, until the engine starts to slow down.
  6. Now, in the open Variable window, delete a big part of the variable content (shift-select content and hit the Delete key).
  7. Run the macro again and you will see that now the engine is responsive again.

But I don’t see how this is related to your memory issue.

To illustrate it: The growing variable content in the Variable prefs window:


Okay. I had indicated that I didn't do that earlier because I didn't want people crashing their Engines. Although it's yet to be determined if I'm the only one affected by this macro or not.

Memory Test CRASH ENGINE.kmmacros (2.3 KB)

See post above, I already have rebuilt your macro.

I'm studying your response.

Okay, obviously I don't understand that simple Regex expression. My intent in that expression was to reduce two characters (a vertical bar and a letter) down to the single letter "a". What am I doing wrong? How would you achieve that requirement? What regex would you write to reduce a vertical bar and a letter down to a single letter?

Oh. "|" is a regex operator not a character. That explains it. Well it doesn't explain my problems, just why this macro crashes. I think I used "|" in some of my recent macros over the last week. I can fix those instances, but you are right this is unrelated to the memory leak. I guess it's about time for me to reinstall KM according to Peter's instructions. You were a good help, thanks.

Yes, | and also ( and ) are meta characters. | is an alternation (“or”) and the parenthesis define a group.

Actually I don’t understand your regex, but it seems you have created some kind of a “pathological regex” :nauseated_face::mask:

For more regex help, search the forum for “learn regex” and you’ll find lots of hints and great links, like for example…

I've had long (tens of seconds) hang-ups before by using regex. In my case, the file being searched was pretty large. I realized that I could speed it up hundreds of times (at least) and make it complete the search in a fraction of a second simply by making my regex more efficient. I pasted my file's text into and played around until I increased the efficiency significantly.

Okay I uninstalled KM, renamed the KM folder in Libraries, reinstalled it.

To my surprise the new installation seems to already be "registered." So is this really a clean installation? Where does KM store its registration data? I thought I would have to load my KM license back into it.

I also examined how much memory it is using, a bare 23.5 MB, and nothing I can do can make that number change. Which is a good sign. I will take it slowly and import as little as possible and try to stress test this new baby to see if I can get it to leak. New babies tend to leak, I've been told. :slight_smile:

Hey @Sleepy,

I think you probably missed some preference files.

** Note that sometimes the macOS saves empty pref files with oddball suffixes.


List Keyboard Maestro Preferences and Support Folder Paths v1.00.kmmacros (5.3 KB)

Oh yes, thanks, now I see a 106-byte plist file in that folder that seemed to contain little more than my email address and license string. I guess that's worth keeping. It's not big enough to be carrying anything that could be causing my problems.

I've reloaded one macro group into my fresh KM installation. So far, no problems. But I did notice that a few dozen global variables were loaded after loading the macros (and before running any of the macros.) That surprised me a bit since there's a separate file for variables within KM. Either global variables are stored in the macro file (unlikely) or they are created either during or shortly after the import (most likely).

See the last paragraph of this. And do a restart after wiping those files. + Chris’ hint.

I didn't restart. I'll do that right now. I did read that post you referenced, in fact I even copied it into a Notes file hours ago to make sure I wouldn't forget. But I was so excited about getting a fresh install that I forgot anyway. :blush: