Is it possible to force a specific program to remain in memory after closing it?

Alfred is great, of course, and I use it all the time. However, if I haven't opened it for a little while it can take a few seconds to open the search-bar UI. Otherwise, it will open in less than a quarter of a second.

I believe this is to do with the fact that it is moved out of memory by the operating system, meaning it has to be copied back in next time I request it (note: I have a mechanical hard-drive, not an SSD, so that takes ages).

So my question is this: can I force macOS to leave an application in main memory even if I'm not using it?


Solution 1:

MacOS's memory management is quite sophisticated, and it generally does try to keep as much stuff in memory as possible. If Alfred is still running, then it will be in memory.

If you have quit Alfred, then it might still be retained in memory, which is why it is faster to load next time. However, the OS will clear the quit app from memory, if the memory is needed for something else. So you might experience slowness somewhere else if you don't let the OS clear the quit app.

Recommendations for improving speed:

  1. Leave Alfred running all the time. If it's slow to come to foreground sometimes, even when its been running in the background, then that's not a memory issue, but something else: possibly related the slowness of the HDD.
  2. Get more RAM, if possible. You don't say what model of Mac and how much RAM.
  3. Replace the HDD with an SSD.

Solution 2:

If your goal is to simply keep an application running all the time even when you close it you could use the method described here in this articled titled: Always keep an application open .

In that article they describe using this method for the Stickies application.

Steps

Excerpted here in case it ever gets deleted

For example, to keep Stickies permanently open, remove Stickies from the login items for your account (if you've placed it there), quit Stickies, and then create the following plist file in the LaunchAgents folder of your home Library (~/Library/LaunchAgents) with the name user.launchkeep.stickies.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>user.launchkeep.stickies</string>
  <key>KeepAlive</key>
  <true/>
  <key>Program</key>
  <string>/Applications/Stickies.app/Contents/MacOS/Stickies</string>
</dict>
</plist>

Now load this launchd job by logging out of your account and logging back in or launch Terminal and enter:

$ launchctl load ~/Library/LaunchAgents/user.launchkeep.stickies.plist

Stickies will now effectively be un-removable -- any time it quits, crashes, or gets forced-quit it will pop right back up. If fact, in order to quit the app at any time other than logout or shutdown you'll need to disable the job; In Terminal type:

$ launchctl remove user.launchkeep.stickies 

Or you can set up a script that does that for you.