Clean up app resources without bottleneck

Cleaning app resources sometimes makes the situation worse than We thought. It is not just simply throw them away.

Complexity of discarded resources

Image for post
Image for post

I begin the story in a simple scope. There is an array which has 100 objects in it

Cleaning up the array of 100 objects is not a big deal.

Continue develop the complexity, I assume that each object has 10 properties. Each property is some kind of object.

Image for post
Image for post

Let do a simple calculation. Each object needs to deallocate 10 of its properties, so free 100 objects will actually deallocate 1000 objects.

This begins to take significant time.

Continue develop the complexity, I assume each object has 10 child objects and each child has 10 grand-child objects.

Image for post
Image for post

Assuming We have an array with 100 objects of this type, so We really has 2 dimensions table. How many objects will be freed when cleaning up the array?

10.000 objects

It is vary in situations. It can be 10 thousands of message objects, 10 thousands of emails, 10 thousands of books, 10 thousands of metadata objects, 10 thousands of songs, 10 thousands of customer records, and so on.

So, getting rid of 10 thousands object is enough to make a trouble?

Review a scenario in app

Image for post
Image for post

Assuming, the left picture is the logout of a chat app.

During the user session, hundred of conversations between users, thousands of messages per each conversation are generated.

When logging out, What We must do is remove all the messages, conversations and stuffs from memory.

Look at the log-out method below

This method will free 1.000.000 objects. In the worst case, What issue would happen? Check this out

Observing FPS, We see it drops below 30 frames. It means user see app freeze for a while.


Why the FPS drops under 30 frames? To understand the issue, I put this below piece of code in either Conversation class or Message class.

In runtime, I believe it says “Deallocating on mainThread=true”

Is deallocating resources neccessary to happen on MainThread?

Resolve issue with DumpTruck

The issue we have seen here is it bottlenecks the MainThread. This end up dropping the FPS. This is bad.

Okay, now let see how to resolve the issue. In below piece of code, I’m giving a utility class which helps resolve the bottleneck. I call it “DumpTruck”.

Now, let give the DumpTruck a call

Run and see the outcome. It should be good. Trust me.

Observing the FPS, no frame drop at all.


In the above DumpTruck class, I hope I give you an initial idea how to deal with cleaning up app resources properly. That is make the cleanup happened on background.

Which cases need DumpTruck help? Actually, not everything. Removing a few objects is not worthy to give DumpTruck a call. Let consider these cases: Array, Stack, Queue, Dictionary, or anything like that.

One more important thing, We need to differentiate non-UI object and UI object. Dealing with non-UI object is easy. However a UI object does not behave like that. The UI object is more headstrong, it will not let you dispatch it to background anyway.

Deallocation of any UI object like UIView or UIViewController always happen on Main-Thread. Does it bottleneck MainThread? In the worst case, I think it is YES. To deal with that We need a special type of strategy. It could be next story I share, so stay tuned.

Source code

This story origin is from

Written by

Senior developer. Been working on iOS, tvOS, Swift, and Objective-C

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store