You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConcurrentModificationException in InMemoryTileCache.purge() #1618
When using the new 'ThemeCallback' API, after setting a new callback, I have to clear the TileCache with tileCache.purge() and then call tileRendererLayer.requestRedraw(). Rarely, but sometimes, calling tileCache.purge() throws the following exception:
FATAL EXCEPTION: main
Process: ---, PID: 11002
java.util.ConcurrentModificationException
at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:775)
at java.util.LinkedHashMap$LinkedValueIterator.next(LinkedHashMap.java:803)
at org.mapsforge.map.layer.cache.InMemoryTileCache.purge(InMemoryTileCache.java:82)
at org.mapsforge.map.layer.cache.TwoLevelTileCache.purge(TwoLevelTileCache.java:80)
Maybe setting the purge method in InMemoryTileCache to synchronized could solve the problem?
No ConcurrentModificationException seems to be thrown when using FileSystemTileCache explicitely instead of InMemoryTileCache, which was created by AndroidUtil.createTileCache
So far I have only used a tilecache which I created with AndroidUtil.createTileCache - that obviously is an InMemoryTileCache. I would like to continue using this InMemoryTileCache - but sometimes the exception occurs when calling purge. I don't really want to use the FileSystemTileCache. That's why I reported the InMemoryTileCache problem and it would be nice if it could be fixed by implementing synchronization (synchronized, ReentrantReadWriteLock, ...).
Ok, I see: AndroidUtil.createTileCache creates an InMemoryTileCache as first level tile cache AND a FileSystemTileCache as second level tile cache.
So far my approach was to only create tile caches using AndroidUtil.createTileCache, so, as you said, my map worked with 2 caches: in memory and file system.
This approach has led to the problem described (occasionally throwing of ConcurrentModificationException).
Therefore, I still suspect that synchronizing the purge method in the InMemoryTileCache class could solve the problem.
I still suspect that synchronizing the purge method in the InMemoryTileCache class could solve the problem.
This has to be verified in some way. As we cannot easily reproduce the case,
you could make the change in Mapsforge code and use it in your implementation to see if it helps?
Hmm, no, sorry. Could it be that this only happens when purge() is called before the rendering is complete, i.e. the DatabaseRenderer is still running? There is also the destroy() method, which is synchronized in the InMemoryTileCache and only calls purge(), maybe this could be a sign that the need for synchronization was recognized earlier, but someone forgot to apply it to purge() as well (this method was added later). Other methods that use the lruCache field are also synchronized, so purge() should probably be as well.
OK, I will try to change Mapsforge code and use it in my implementation to see if it helps , but this will take some time.
I don't want to be disrespectful, but I think I can give a reasonable explanation as to why the purge method in my opinion needs to be synchronized in InMemoryTileCache.
Class InMemoryTileCache uses class BitmapLRUCache for caching. Base class of BitmapLRUCache is org.mapsforge.core.util.LRUCache<K, V>. LRUCache<K, V> extends LinkedHashMap<K, V>. So you actually use a LinkedHashMap for caching Bitmaps.
JavaDoc says for class LinkedHashMap:
Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
So for methods like put, get, containsKey, getCapacity keyword synchronized is used in your code in class InMemoryTileCache. Only for method purge keyword synchronized is not used. I do not see any reason why it should be correct not to use the synchronized keyword only just for the purge method, which in my opinion obviously structurally modifies the map.
OK, I will try to change Mapsforge code and use it in my implementation to see if it helps , but this will take some time.
I don't want to be disrespectful, but I think I can give a reasonable explanation as to why the purge method in my opinion needs to be synchronized in InMemoryTileCache.
Class InMemoryTileCache uses class BitmapLRUCache for caching. Base class of BitmapLRUCache is org.mapsforge.core.util.LRUCache<K, V>. LRUCache<K, V> extends LinkedHashMap<K, V>. So you actually use a LinkedHashMap for caching Bitmaps.
JavaDoc says for class LinkedHashMap:
Note that this implementation is not synchronized. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
So for methods like put, get, containsKey, getCapacity keyword synchronized is used in your code in class InMemoryTileCache. Only for method purge keyword synchronized is not used. I do not see any reason why it should be correct not to use the synchronized keyword only just for the purge method, which in my opinion obviously structurally modifies the map.
This is quite clear, no need to explain 🙂
What about this:
Could it be that this only happens when purge() is called before the rendering is complete, i.e. the DatabaseRenderer is still running?
Is this something relevant to the problem and perhaps something you could improve in your code regardless of the synchronization issue?
@Sublimis: Is there a way to post the purge command to be executed at a time, when 'rendering is complete'?
Afaik no, there are still no "render start" and "render complete" callbacks implemented, so while such functionality could probably be emulated it would probably be hacky and not so simple. There's an idea for a feature...
Activity
devemux86 commentedon Jan 12, 2025
Can you try the change in your implementation and see if it solves the case?
Info:
FileSystemTileCacheuses aReentrantReadWriteLockin this method.wharnisch commentedon Jan 12, 2025
No
ConcurrentModificationExceptionseems to be thrown when usingFileSystemTileCacheexplicitely instead ofInMemoryTileCache, which was created byAndroidUtil.createTileCachedevemux86 commentedon Jan 12, 2025
Do you use only a file tile cache?
Map works with both memory and file tile cache.
wharnisch commentedon Jan 12, 2025
So far I have only used a tilecache which I created with
AndroidUtil.createTileCache- that obviously is anInMemoryTileCache. I would like to continue using thisInMemoryTileCache- but sometimes the exception occurs when calling purge. I don't really want to use theFileSystemTileCache. That's why I reported theInMemoryTileCacheproblem and it would be nice if it could be fixed by implementing synchronization (synchronized, ReentrantReadWriteLock, ...).devemux86 commentedon Jan 12, 2025
See the GettingStarted example.
Map works with 2 caches: in memory and file system.
wharnisch commentedon Jan 12, 2025
Ok, I see:
AndroidUtil.createTileCachecreates anInMemoryTileCacheas first level tile cache AND aFileSystemTileCacheas second level tile cache.So far my approach was to only create tile caches using AndroidUtil.createTileCache, so, as you said, my map worked with 2 caches: in memory and file system.
This approach has led to the problem described (occasionally throwing of ConcurrentModificationException).
Therefore, I still suspect that synchronizing the purge method in the InMemoryTileCache class could solve the problem.
devemux86 commentedon Jan 12, 2025
This has to be verified in some way. As we cannot easily reproduce the case,
you could make the change in Mapsforge code and use it in your implementation to see if it helps?
devemux86 commentedon Jan 12, 2025
@Sublimis Have you seen something like this?
Sublimis commentedon Jan 12, 2025
Hmm, no, sorry. Could it be that this only happens when purge() is called before the rendering is complete, i.e. the DatabaseRenderer is still running? There is also the destroy() method, which is synchronized in the InMemoryTileCache and only calls purge(), maybe this could be a sign that the need for synchronization was recognized earlier, but someone forgot to apply it to purge() as well (this method was added later). Other methods that use the lruCache field are also synchronized, so purge() should probably be as well.
wharnisch commentedon Jan 12, 2025
OK, I will try to change Mapsforge code and use it in my implementation to see if it helps , but this will take some time.
I don't want to be disrespectful, but I think I can give a reasonable explanation as to why the
purgemethod in my opinion needs to be synchronized inInMemoryTileCache.Class
InMemoryTileCacheuses classBitmapLRUCachefor caching. Base class ofBitmapLRUCacheisorg.mapsforge.core.util.LRUCache<K, V>.LRUCache<K, V>extendsLinkedHashMap<K, V>. So you actually use aLinkedHashMapfor caching Bitmaps.JavaDoc says for class
LinkedHashMap:So for methods like
put,get,containsKey,getCapacitykeywordsynchronizedis used in your code in classInMemoryTileCache. Only for methodpurgekeywordsynchronizedis not used. I do not see any reason why it should be correct not to use thesynchronizedkeyword only just for thepurgemethod, which in my opinion obviously structurally modifies the map.wharnisch commentedon Jan 12, 2025
As an alternative, class
LRUCachecould extendConcurrentHashMapinstead ofLinkedHashMapdevemux86 commentedon Jan 12, 2025
If the
synchronizedexists in the other methods, we could also add it in thepurge.[-]Exception in InMemoryTileCache.purge()[/-][+]ConcurrentModificationException in InMemoryTileCache.purge()[/+]Sublimis commentedon Jan 12, 2025
This is quite clear, no need to explain 🙂
What about this:
Is this something relevant to the problem and perhaps something you could improve in your code regardless of the synchronization issue?
wharnisch commentedon Jan 13, 2025
@Sublimis: Is there a way to post the purge command to be executed at a time, when 'rendering is complete'?
InMemoryTileCache synchronized purge, fix #1618
devemux86 commentedon Jan 13, 2025
Fixed by #1620.
Sublimis commentedon Jan 13, 2025
Afaik no, there are still no "render start" and "render complete" callbacks implemented, so while such functionality could probably be emulated it would probably be hacky and not so simple. There's an idea for a feature...
InMemoryTileCache synchronized purge, fix mapsforge#1618 (mapsforge#1620