How to clean the memory properly (conceptual, SWIFT)

158 views Asked by At

I have a loop where I add objects (.append) to an array and remove them after a while in a Background-Task (dispatch_async(backgroundQueue, {) During this loop my array has theoretically a max amount of objects in it, because I am removing old objects.

What I observe is, that the memory cunsumption goes high (over 400MB) till I get a didReceiveMemoryWarning Warning and my didReceiveMemoryWarning is called and sometimes the app just crashes. So it looks like my cleaning doesn't work, BUT When stopping in the middle of the loop and waiting for some seconds (say 10 seconds) I see the memory slowly going down to my expected value (60MB).

So I guess I conceptually doing this wrong.

How to do this properly ? Is there a way to force cleaning memory like in Java forcegc ?

When cleaning the objects in the main task (dispatch_async(dispatch_get_main_queue(),{) the memory doesn't reach the limit, but the GUI is stocking.

0

There are 0 answers