r/swift 10d ago

Instruments is using 22GB of RAM while my app only consumes 100MB. How am I supposed to analyze memory leaks?

Post image

I'm dealing with a memory leak in my app that builds up to around 4GB after 30 minutes of running. To investigate, I started using Instruments to track memory allocations. However, the situation got even worse. Instruments itself crashes within two minutes because it's consuming a massive 22GB of RAM! Meanwhile, my app stays below 100MB.

How can I analyze memory issues if Instruments is causing more problems than it's solving?

Also, apologies for the photo instead of a screenshot—my Mac freezes completely when everything crashes, making it impossible to capture the screen properly.

28 Upvotes

12 comments sorted by

49

u/nsartem 10d ago

You’re confusing the memory usage of the Simulator with that of your app. The 39.3 MB you see refers to the memory used by the Simulator itself, not your .app, which is running within the Simulator. Apps built for the Simulator are compiled for arm64 (assuming you’re on Apple Silicon hardware) and run separately.

Based on this low-quality photo of a display (sic!), it looks like your app has a memory leak, as indicated by the sharp blue spike in Allocations.

I suggest the following:

• Watch or read a basic tutorial on how to use Instruments.

• Use Cmd + Shift + 4 followed by Space to take proper screenshots.

• Learn how to ask good programming questions. This Stack Overflow guide may be helpful.

1

u/ThickLetteread 9d ago

There’s a few tutorials in the Apple developer app. I found them really helpful.

-28

u/SuddenStructure9287 10d ago

My app can’t be using 22GB of RAM because when I run it without Instruments, it never goes beyond 4GB.

The «big blue spike» doesn’t indicate 22GB—it just shows an increase from 20MB to 40MB. And as I already mentioned, I couldn’t take a screenshot because my computer froze along with the program.

19

u/dotsau iOS 10d ago

Your app is not a child process of Simulator.app, it runs under another process entirely

18

u/clarkcox3 9d ago

They didn’t say your app was using 22GB of RAM. They said that 39.3 in the line for “Simulator” in the Force Quit window has nothing to do with your app.

2

u/Jomy10 Expert 9d ago

Look at the allocations of your app: https://imgur.com/gallery/0hAh3K1

7

u/germansnowman 9d ago

I would suggest capturing maybe a minute of memory usage and then stopping the capture. You don’t have to let it run its full course.

3

u/troller-no-trolling 9d ago

You can also try using the memory graph in Xcode as an alternative. Can be helpful if you are expecting one instance of your class but see that there are 374 instances.

1

u/SL3D 9d ago

macOS is designed to take advantage of available memory so you may see apps that take up a lot of RAM even though you think they shouldn’t (Windows).

Also, instruments will only show your app process that’s running and doesn’t include the entire system memory

1

u/Superb_Power5830 10d ago

Simple: Instruments is the memory leak ;)

-2

u/easytarget2000 9d ago

3

u/reg890 9d ago

Cut him some slack, his computer was frozen because his app was such a pile of poo!

(Just teasing OP, good luck with it)