While iPhone 2.x has provided great improvements (e.g. Safari multi-
touch interface, SQL database, full screen web apps), there were
persistent reports about strange crashes, malfunctions and
instabilities in Safari and in native apps. While tracking down
problems reported by some of our users, I have found the root of the
problems -- broken garbage collection in Safari (desktop and mobile)
in combination with severe limitation (~4.5 MB of png/jpeg data) on
total image data.
It seems that Safari never releases memory for inaccessible/released
images, as they get replaced with new images (e.g. via change in
img.src or via creation of new Image() elements overwriting the same
img variable). In the desktop Safari one can observe continued
increase in the memory footprint as you replace old image with new
image, until it uses up entire system memory and crashes. In the
mobile Safari the problem is far more serious, since the mobile Safari
simply stops loading new images after about 4.5MB of png or jpeg image
data was loaded. What is even worse, the mobile Safari ceases to issue
either load, error or abort events so your code doesn't even know
there is a problem. Of course, user sees the problem since the picture
on the screen freezes.
I made a little bare bones slide show, which simply loads new pictures
(via replacement of img.src of a displayed image element, see the
source), illustrating the problem on iPhone 2.x Safari:
http://www.myf2p.com/test/slideshow.htm
After loading 9 images (out of 12), each using ~460k, the image
loading and all loading events quietly stop. Desktop Safari or iPhone
1.x (or other browsers) will load all 12 images. Note that it is
essential for the problem that images are distinct (Safari apparently
recognizes identical images and doesn't increase the memory footprint
in that case). The problem reproduces on iPhone 2.x every time (you
can also vary how you create or load new image; even using
XMLHttpRequest to load image as data URI has the same problem). In
contrast, iPhone 1.x will run such slide show with distinct images
indefinitely. The current desktop Safari 3.1 will show distinct images
until it runs out of system memory (having never reclaimed any memory
for released images). If you were to repeat this test on iPhone 2.x
with each run serving a new set of 9 images (different from previous
images; this needs server side code which is not included in the
sample above), the Safari will crash when the total image data (png or
jpeg) over multiple runs reaches ~30MB (i.e. after repeating the above
test ~7 times).