Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Smart Image cache  (Read 2403 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2010-11-23 22:55:33 »

I have been thinking of ways to improve my image cache.

Currently I a WeakHashMap which maps an ID to an image.
If the ID isn’t in the map it will load that image and then save it to the map.
The problem is that multiples of the same images will be stored in memory.
Say its loading images from an unknown source, and so can’t improve it before this.

I’m trying to figure out a way to check after loading the image that if the same image is already in the image cache and use that instead. I guess a way would have 2 WeakHashMaps, one to map which does the id to the image and the other to do the imageID to the image.

I guess my problem is what would be the best way to get an image ID which is always the same for the same image.

Current project - Rename and Sort
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-11-23 23:28:59 »

WeakHashMap is the worst choice for a RAM cache... you really need a SoftHashMap, but Sun didn't make one.

WeakHashMaps are 'flushed' every GC, which can run up to several times per second, making them useless to do any kind of caching.

SoftHashMaps are 'flushed' every FULL GC, most likely to occur when the heap fills up, which is when you want your cache to be cleared.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline zammbi

JGO Coder


Medals: 4



« Reply #2 - Posted 2010-11-24 00:07:27 »

Quote
SoftHashMaps are 'flushed' every FULL GC, most likely to occur when the heap fills up, which is when you want your cache to be cleared.
Ah thanks for pointing that one out. I'll see if I can find a SoftHashMap later.

Edit: Replaced it with a LRUMap, seemed to fix a broken image problem that I was having Smiley

Current project - Rename and Sort
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nsigma
« Reply #3 - Posted 2010-11-24 09:54:29 »

Hi,

I've been looking at caching recently in my own stuff, and I'm not entirely sure I agree with Riven's assessment.  persecutioncomplex

IMO, WeakHashMap may be fine for your cache (I'll get to that), but the main thing is that it's the image that should be wrapped in the SoftReference - ie. the value in the Map, not the key.  You could even get away with a standard HashMap<ID, SoftReference<Image>> for that.

The use of a WeakHashMap, SoftHashMap or LRUMap could deal with the IDs themselves.  If your IDs are unique objects, then a WeakHashMap would be fine as once you have no reference to the ID, there's no way you can retrieve the image anyway.  If you're using Strings, URIs or anything else that overrides equals() though, you'd be better off with the LRUMap I would think.  However, either way I'd wrap the images in a SoftReference - then you know there'll be cleared if you run out of memory, even if you have a strong reference to the ID.

Best wishes, Neil

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2010-11-24 10:35:15 »

I've been looking at caching recently in my own stuff, and I'm not entirely sure I agree with Riven's assessment.  persecutioncomplex
Well, the javadoc is pretty clear about when SoftReference, WeakReference (and PhantomReference) ought to be garbage collected.

Surely you might use a regular Map with SoftReference values, instead of a SoftHashMap. The only thing you have to remember is not to use WeakHashMap for caching if you want to take advantage of the space on the heap as long as possible.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline nsigma
« Reply #5 - Posted 2010-11-24 10:48:48 »

Well, the javadoc is pretty clear about when SoftReference, WeakReference (and PhantomReference) ought to be garbage collected.

Yeah, not disagreeing with you here. 

Surely you might use a regular Map with SoftReference values, instead of a SoftHashMap. The only thing you have to remember is not to use WeakHashMap for caching if you want to take advantage of the space on the heap as long as possible.

This is the bit I don't agree with.  A SoftHashMap (has SoftReference keys) and a Map with SoftReference values are completely different things.  You're likely to keep strong references to the ID in all components that use the image - if the SoftReference was on the ID, it would never be GC'd before you ran out of memory.  A WeakHashMap would be fine in the specific scenario I mentioned, where when the ID has disappeared there's no way to get the image anyway.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!