1/17/2024 0 Comments Hashmap java 15![]() ![]() If you want a cache this is going to be a better solution imho than a SoftHashMap. Here's an LRU cache i designed with O(1) insertion, deletion and lookup time, that has a configurable max number of elements. Or you could just implement a max size for the cache and use that to decide when to drop things. Then once free memory drops below a certain amount you can start either dropping items. If you only want to lose things when you are running out of heap space you can query available heap space via: Runtime.getRuntime().getFreeMemory() However you are going to have to decide when to eject items and which to eject. If cache removal policy is important your are going to need to do it on your own most likely using regular references. If you want to implement a cache softreferences are definetly a better idea than weak references, but it puts your entire cache removal policy in the hands of the garbage collector. Moreover, this implementation is not concurrent - it can be made synchronized, but that works less well under accesses from multiple threads. There's another implementation from Apache Commons, namely .map.ReferenceMap it does not support timed removal, but it does support choosing whether keys should be compared by identity or by equality. Jeremy Manson recommends, for any cache, using this feature to avoid the dangers of SoftReference: In particular, it simply uses, which is not so intrusive as having a separate background thread. The Google implementation uses a background thread only when timed expiration of entries is requested. It provides an alternative concurrent reference map to the Google implementation (which relies on a background thread to evict entries) 2008), but you kind find some implementation of SoftHashMap on the net.Īs Matthias mentions in the comments, the Google Guava MapMaker does use SoftReferences:Ī ConcurrentMap builder, providing any combination of these features:Īs mentioned in this thread, another JSR166y candidate: It even supports building a SoftHashMap (see CacheBuilder.newBuilder().softKeys()), but it is probably not what you want, as Java expert Jeremy Manson explains (below you'll find the link). It turns out that currently the best solution are probably Guava 13.0's Cache classes, explained on Guava's Wiki - that's what I'm going to use. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |