- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
GWT Image.prefetch memory leak and suggestions for improvement
In answering a question over at the GWT google group, I came across what I think is a misfeature with Image.prefetch. The code for prefetch looks like this:
public static void prefetch(String url) { Element img = DOM.createImg(); DOM.setImgSrc(img, url); prefetchImages.put(url, img);}
The reason that the images are stored in a static hash is because in some browsers, under some circumstances, the image DOM element is garbage collected before it is loaded. That's great. But there is a problem here: these prefetched images are now not garbage collected at all. If you prefetch 1024 images of 1M each, you will have 1G of images in your browser. In some browsers, prefetching the same image several times will apparently create lots of copies, each chewing up more memory. (I haven't seen this in IE7 and FF2 in my brief testing.)
So, what to do? For starters, I'd suggest testing to see if we've already loaded an image. Second, I'd add the ability to unfetch or release a resource:
public static void prefetch(String url) { if (!prefetchImages.containsKey(url)) { Element img = DOM.createImg(); DOM.setImgSrc(img, url); prefetchImages.put(url, img); }}
public static void release(String url) { prefetchImages.remove(url);}
You could do something with an onLoad event or automatically kick out earliest image from a FIFO queue when prefetching a new image, but I'm not sure that gives you enough control. Anyhow, the above gives you enough control to be useful.
Technorati Tags: ajax, gwt, prefetching images, memory leak
Topics: GWT
Comments: 1 so far
Leave a comment
About Pathfinder
Recent
- Push Button Phones and the Limits of User Testing
- Firefox Plugin Malware ‘Trojan.PWS.ChromeInject.A’
- Pathfinder releases version 1 of the its Flash Platform microsite (codename Mica)
- Pimp my Rails: Five Plugins & Gems to Make Rails Better
- iPhone: Using Pre-processor Directives for Device Testing
- Subtle OpenGL Projection Matrix Difference Between iPhone Simulator and Device
- App Security: Throw Out the Org Chart!
- Pimp my jQuery: Five plugins to replace the features Prototype and Scriptaculous users expect
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
Archives
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006


Good point. I had noticed the same thing. I still don’t like the fact that I must explicitly call the release. I think the framework should be smart enough to GC the images in the prefetchImages HashMap
Comment by Harold Campbell, Saturday, August 23, 2008 @ 10:26 pm