This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JVM caching images?!  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that the ImageIcon class (and Toolkit) are caching images that I load. This is causing me an issue because I am writing a map editor, and when someone updates the actual image of tiles, I want to be able to "Refresh" the screen and reload the graphic.

I had an elaborate image cacher in my code, and drop the cache when I need to refresh -- and now I learn that Java is doing it behind the scenes?

How can I drop the cache so I can redisplay the image? Any technical way, or clever workaround?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a bit of a non-answer, but if you're working with tiled images perhaps you should look at RenderedImage and JAI. I haven't delved too far into it but a big part of what it offers is precise control over tiled images.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't blame the JVM, it's the library! The API for java.awt.Toolkit.getImage states:

The underlying toolkit attempts to resolve multiple requests with the same filename to the same returned Image. Since the mechanism required to facilitate this sharing of Image objects may continue to hold onto images that are no longer of use for an indefinite period of time, developers are encouraged to implement their own caching of images by using the createImage variant wherever available.

The documentation in javax.swing.ImageIcon doesn't reveal all, but the constructors that are passed a URL or a filename string use Toolkit's getImage in all the source code from Sun I've read.

Advice: avoid Toolkit's getImage, and the ImageIcon constructors that take a filename string or URL. I'd like to learn the JAIm but it keeps getting bumped down my to-do list (work, *sigh*), however reading images with javax.imageio.ImageIO is a snap, and there is no caching:
BufferedImage is a subclass of Image. There's no need to use MediaTracker or ImageObserver with it -- its pixels are always in memory.
 
John Blanco
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow...another library. Java stays too far ahead of me. :-) Thanks, worked great!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!