Wednesday, July 11, 2012

iOS Mobile Web Application Image Memory Leak

When developing a mobile web application on iOS using UIWebView, if you happen to add or remove img elements regularly to the HTML DOM, the app might experience crashes due to high memory usage (you should see a few Received memory warning before the crash). This is particularly the case on iPad 1 and possibly older versions of iPhone where the amount of memory that can be used by an app is very limited.

It appears that when you remove an img element or its container, the memory used by the image does not get cleared properly and it can cause a memory leak. To resolve this, you must first remove the src attribute prior to removing the img element or its container. For example, in jQuery:

$('#container img').removeAttr('src');
$('#container').empty();

Hope this helps anyone who are also experiencing a similar issue.

1 comment:

  1. Nice.Thanks for sharing.I'm a regular reader of this blog.Almost my all friends also joined this blog and they also a regular reader.
    Mobile Application Charleston

    ReplyDelete