Repository navigation
Description
What is the bug?
When using multiple TileLayers with frequently changing urlTemplate, my application’s GPU memory usage continues to increase over time. Even after older URLs are no longer referenced, or after certain TileLayers are no longer visually needed, flutter_map appears to retain those textures in GPU memory. For example, GL mtrack over 3.5 GB before device forcibly closes the app:
** MEMINFO in pid 3605 [com.myapp.myapp] **
Pss Private Private SwapPss Rss Heap Heap Heap
Total Dirty Clean Dirty Total Size Alloc Free
------ ------ ------ ------ ------ ------ ------ ------
Native Heap 119067 109828 9228 81685 119308 213812 180648 33163
Dalvik Heap 8933 3020 5836 863 9280 11225 5081 6144
Dalvik Other 10674 8548 712 273 12168
Stack 1252 1156 96 1536 1260
Ashmem 0 0 0 0 36
Gfx dev 0 0 0 0 4
Other dev 14 0 8 0 432
.so mmap 2772 120 1540 533 12316
.jar mmap 2492 0 1608 0 7144
.apk mmap 10811 556 10152 56 10916
.ttf mmap 107 0 12 0 748
.dex mmap 0 0 0 0 16
.oat mmap 70 0 60 0 492
.art mmap 10049 260 8956 6649 15296
Other mmap 945887 944384 1076 0 946436
EGL mtrack 57352 57352 0 0 57352
GL mtrack 3502976 3502976 0 0 3502976
Unknown 488650 486260 2388 204612 488724
TOTAL 5457313 5114460 41672 296207 5184904 225037 185729 39307
How can we reproduce it?
Well, add something like 72 TileLayer(), and update each URL every 5 minutes. Pan the map around from time to time, and also run:
adb shell dumpsys meminfo com.myapp.myapp
...and watch the GPU memory grow forever.
It could be this is an issue "most" people do not bump up against, but is magnified given my situation.
Do you have a potential solution?
If it doesn't already exist, add a way to force tile disposal for old URLs and/or old keys. Possibly exposing a new “evict” API (similar to old tileStore concept).
Provide documentation clarifying how to remove stale GPU textures (e.g., remove the TileLayer from the widget tree entirely, wait for disposal, etc.)
Platforms
Galaxy S23
Severity
Fatal: Causes the application to crash
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Effort
Activity
corepuncher commentedon Jan 2, 2025
I performed a sequential test. First, with Skia, and then Impeller.
I loaded the app and panned around (or snapped programmatically to a lat/lon).
After each step, I ran:
adb shell dumpsys meminfo com.myapp.myapp
TLDR:
The root issue seems to be a lack of robust tile/texture eviction within flutter_map or the Flutter engine.
Impeller only magnifies the memory growth issue (Flutter 3.27+ default on Android now).
The test suggests that once a tile texture is uploaded to GPU memory, neither backend is discarding it—leading to near-monotonic growth.
Below you will find the SKIA test with memory numbers (pay attention to GL mtrack and TOTAL), followed by Impeller test. Basically the same result, except even more GPU memory locked up. People would not see these values in devtools.
JaffaKetchup commentedon Jan 2, 2025
I haven't read through the logs in detail yet, but I suspect the first place to start would be to make sure the
ImageCacheis being used correctly.I seem to remember you said you've changed it? Is that in this app?
corepuncher commentedon Jan 2, 2025
Regarding the global image cache, the only things I do are:
But to rule out anything funny, I"ll comment all those out and test again now.
As far as flutter map code I do not recall ever changing anything related to image caching.
Also I posted a generalized app crash issue here (since it has to do with Impeller upgrade): flutter/flutter#160941
Technically, the app STILL does crash, but not nearly as often using Skia.
corepuncher commentedon Jan 2, 2025
Some more testing (went back and forth twice) on Android:
SKIA: The GL mtrack memory periodically goes down during map panning/zooming, and the TOTAL memory seemed to stabilize. Typically around 2 GB GL mtrack and around 3GB TOTAL. And it's more common for these values to drop by a few hundred MB from time to time.
IMPELLER:
No matter what I do, the GL mtrack and TOTAL memory ONLY go up. Nothing ever seems to get removed from the GPU and eventually, the app is killed at around 3.5 GB GL mtrack and 5.3 GB TOTAL used.
Those two examples above are same app, same imagery, same general usage/map behavior. Worlds apart.
UPDATE: Also toyed with NOT setting imageCache max size, vs just using the default, and, just letting system clear it when needed. Not really much difference either way.
corepuncher commentedon Jan 5, 2025
Well, it appears that flutter 3.27 (even when building "presumably" with impeller disabled) appears to use more memory, resulting in crashes. Either that, or something else has changed. Perhaps it is less aggressive in reclaiming memory or clearing image cache, IDK. So for now, I have reverted back to 3.24.5 :-(
However, I have been working on mitigating crashes. I did two primary things:
I use the debounce transformer on all TileLayer NOT currently visible (i.e., "beneath" the visible frame). That way giant slews of tiles are not pulled until panning or zooming ends.
As a last resort, the app user can now flip a switch which will enable a special mode.
Lets say we have a 36 frame loop. Before, I had all 36 frames loaded at once, and turn opacity on/off.
Now, I only load half. This results in much less tile downloads (half) as the user pans or zooms.
Now, for looping, this means we are only loading 18 out of 36 frames, (1 back in case user hits back button, and 17 "ahead").
As the frames animate, the next 17 are of course downloaded. As each frame iterates, another TileLayer is created well ahead, while the one 2 frames ago is destroyed. Given these long loops, that is enough time to get new tiles downloaded if needed, but most of the time it appears that even though the tile layer is disposed, the image remains in the image cache (since I increased to 1GB).
At first I was afraid that disposing the tile layer would result in new downloads, but thankfully that is NOT the case. So yay.
mootw commentedon Jan 6, 2025
I would highly recommend not loading all layers at once, and instead, changing the tile URL for a single layer and let it handle swapping out tiles. This will avoid the memory issues you are experiencing. Though, i would recommend staying on 3.24.5 for the time being, given how unstable 3.27 has been so far
JaffaKetchup commentedon Feb 16, 2025
I'm going to have to mark this as external/won't fix, sorry! I'm not 100% sure we're causing the issue, as it seems to be so related to Impeller, and you are placing an unusually high strain on the Flutter image cache memory, so this issue is unlikely to appear for other users.
However, I'm still interested in finding out more if you make any more discoveries, so feel free to comment here :)
corepuncher commentedon Feb 23, 2025
No problem! Just want ya'll to be aware. I'll pass on any info I find. But be aware it may crop up for others over time as FM gets (even) more popular.