How to safely maximize concurrent UI rendering

I'm using Swift 6 and tasks to concurrently process multiple PDF files for rendering, and it's working well.

But currently I'm manually limiting the number of simultaneous tasks to 2 out of fear that the system might run many tasks concurrently without having enough RAM to do the PDF processing.

Testing on a variety of devices, I've tried increasing the task limit and haven't seen any crashes, but I'm quite concerned about the possibility. Any given device might be using a lot of RAM at any moment, and any given PDF might strain resources more than the average PDF.

Is there a recommended technique for handling this kind of scenario?

Should I not worry about it and just go ahead and start a high number of tasks, trusting that the system won't run too many concurrently and therefore won't run out of RAM?

You can add UIApplicationDelegateAdaptor and get memory warning callbacks. Then handle them in your SwiftUI views as needed. Please note, in the debugger you will get more memory than you would if downloaded from the AppStore, so always test without the debugger and some kind of graphical logging of memory warnings to determine how it might behave in a shipped app.

Rico


WWDR | DTS | Software Engineer

Thank you for the suggestion to respond to applicationDidReceiveMemoryWarning and for pointing out the different memory behavior of debug vs. release.

Ideally I would want to just trust the Swift cooperative thread pool and not come up with my own arbitrary limit for concurrent tasks. It’s difficult to imagine a clean way to handle older devices with few cores and little RAM versus much more capable newer devices, and I wonder if the Swift thread pool already has some magic built in to recognize things like memory pressure and to put the brakes on concurrency.

Manually responding to memory warnings almost feels too late. But I don’t know what else I should do.

To provide a little more clarity about my case, it’s rendering PDF pages to images in the background and persisting those images to disk (no immediate presentation in the user interface).

Thank you for any additional insights or tips.

How to safely maximize concurrent UI rendering
 
 
Q