Skip to content

ConcurrentModificationException in InMemoryTileCache.purge() #1618

Closed

Description

@wharnisch

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?

Activity

  1. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    Can you try the change in your implementation and see if it solves the case?

    Info: FileSystemTileCache uses a ReentrantReadWriteLock in this method.

  2. wharnisch commented on Jan 12, 2025

    @wharnisch
    Author

    No ConcurrentModificationException seems to be thrown when using FileSystemTileCache explicitely instead of InMemoryTileCache, which was created by AndroidUtil.createTileCache

  3. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    Do you use only a file tile cache?
    Map works with both memory and file tile cache.

  4. wharnisch commented on Jan 12, 2025

    @wharnisch
    Author

    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, ...).

  5. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    See the GettingStarted example.
    Map works with 2 caches: in memory and file system.

  6. wharnisch commented on Jan 12, 2025

    @wharnisch
    Author

    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.

  7. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    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?

  8. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    @Sublimis Have you seen something like this?

  9. Sublimis commented on Jan 12, 2025

    @Sublimis
    Contributor

    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.

  10. wharnisch commented on Jan 12, 2025

    @wharnisch
    Author

    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.

  11. wharnisch commented on Jan 12, 2025

    @wharnisch
    Author

    As an alternative, class LRUCache could extend ConcurrentHashMap instead of LinkedHashMap

  12. devemux86 commented on Jan 12, 2025

    @devemux86
    Collaborator

    If the synchronized exists in the other methods, we could also add it in the purge.

  13. changed the title [-]Exception in InMemoryTileCache.purge()[/-] [+]ConcurrentModificationException in InMemoryTileCache.purge()[/+] on Jan 12, 2025
  14. Sublimis commented on Jan 12, 2025

    @Sublimis
    Contributor

    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?

  15. wharnisch commented on Jan 13, 2025

    @wharnisch
    Author

    @Sublimis: Is there a way to post the purge command to be executed at a time, when 'rendering is complete'?

  16. added this to the 0.24.0 milestone on Jan 13, 2025
  17. added a commit that references this issue on Jan 13, 2025
    484a3a4
  18. devemux86 commented on Jan 13, 2025

    @devemux86
    Collaborator

    Fixed by #1620.

  19. Sublimis commented on Jan 13, 2025

    @Sublimis
    Contributor

    @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...

  20. added a commit that references this issue on Jan 15, 2025
    96cef32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions