UIImage memory

Does reloading new images into a UIImage view increase the memory usage each time a new image is loaded, thus potentially leading to a memory leak? If so, then do I need to release the memory used for the currently loaded image before reloading the UIImage with a new image?

The reason I created this post was due to the fact that one of the players who I was playing against was kicked out of my game application due to didReceiveMemoryWarning() being called. I have never seen this memory warning occur while testing for the past many months. I assumed I might have some memory leaks since I never checked before or possibly be using too much memory in my application. I read somewhere that setting UIImages to nil might free up memory so I thought I would look whether this was true, so decided to start this post.


That being said, I just recently performed the following and concluded I do not have any memory leaks in my application:


I ran the "leaks" instrument while running my application on my real iPhone device (ie: leaks does not work on the iOS simulator running 13.4). The leaks tool never reported any leaks in my application code. There are however various leaks occurring (64bytes, 128bytes, 256bytes in other code which I do not change). I am ignoring those areas.


I ran "Malloc Stack (Live Allocations Only)" and did not see any purple exclamation points in the Debug Navigator ever


In the "Debug navigator" I clicked on "Memory" and saw my application runs at a constant 49-51MB from start to finish whether 2,3 or 4 players are in the game. If I was leaking memory I assume this memory usage amount would steadily increase throughout the 20-30 minute gameplay to some value much much higher

So may be your friend player ran out of memory because of another app.


You should ask him/her if there was a lot of other apps running, or a very hungry one.

My friend did indicate she had a lot of other applications opened and has only 2Gb left of overall available space on the iPhone. I guess this might explain the memory issue.

I am ignoring those areas.

I do not understand why you can so easily ignore them.

Even if they point to some other place than your code, the leaks did happen while your app was running.

Unless it is clarified those leaks are caused by some iOS bugs and cannot avoid, you may need to fix those leaks.

Users tend to keep apps running for a long time, small leaks would sum up to a big amount while running.


If I was leaking memory I assume this memory usage amount would steadily increase throughout the 20-30 minute gameplay to some value much much higher

Do you expect users to shut down your app after each half-hour gameplay? As I wrote above, users may keep many apps running (like your friend), so even a slow rate increase should not be ignored, when it grows constantly.


Generally, didReceiveMemoryWarning() being called does not mean that your app has non-ignorable memory leak. If a user keep many apps up and running, and total amount of memory usage gets higher than some system limit, iOS would send didReceiveMemoryWarning to any apps running, some unfortunate apps would be terminated after (or before) that.


But please keep in mind, your app needs to properly respond to didReceiveMemoryWarning, you may need to free as much memory as you can, for example, some cached content which is not immediately in use should be released.

As you have no control on the cache used by UIImage(named:), using UIImage(contentsOfFile:) might be a good option to accomplish that.


---

So, in my opinion, your usage of UIImageView or UIImage is not a direct cause of didReceiveMemoryWarning() being called, and setting nil before replacing UIImage does not help anything. But you should better re-consider fixing the leaks as much as you can and responding to didReceiveMemoryWarning properly.

My application does not cache any specific game data, files or URL images so I do not know of any cached files or memory which I could free when a didReceiveMemoryWarning( ) was received.


The memory leaks, which were reported by the "leaks" instrument, are not pointing to any functions that I created in my code so I have no idea how to progress those. The memory leaks are related to functions which make no sense to me. Knowing I just taught myself swift coding and just created my first application only, I do not currently posess the knowledge to know how to progress issues in code which I did not even write.

Some parts of your code may be calling the funcionalities of iOS or other frameworks. If your call is done in a bad manner, that may cause memory leaks in somewhere you did not write.


So, if you continue developing apps, how to solve such leaks is one thing you need to learn. Also you need to know that not all such leaks may be solved.

It looks like the "leaks" instrument output provides the stack trace of where the memory leak occurred. If this is true and I do not see any of my functions displayed in the stack trace then I do not know how to progress the issue. I do not even know where other code exists which was not originally written by myself.

It is true that if your code is not included in the stack trace, it is very hard to find what may be causing the leak.


But, assuming a vanilla project does not show any leaks,

something you have added to the project may be causing the leak.


I do not insist on taking infinite time to solve all the leaks, but ignoring them so easily does not seem to be a good habit.

Thanks for your input. I will google around to find some information and see how it goes.

UIImage memory
 
 
Q