Memory leak when using GPU (Vulkan) in Android
I am running inference using a custom YOLOv8m model on Android. A memory leak is occurring in the C++ stack due to model inference, which grows over time and leads to app crashes.
NCNN build version used: ncnn-20230517-android-vulkan Android Device used: Samsung A52s, Pixel 7/7a
The Following chart shows how the memory leak persists even after trying things like
1. Activating Lightmode
net.opt.lightmode = True
2. Using custom memory allocators and clearing after each detection
blob_vkallocator = vkdev->acquire_blob_allocator();
staging_vkallocator = vkdev->acquire_staging_allocator();
// GPU memory allocators
net.opt.use_vulkan_compute = is_gpu_enabled;
net.opt.blob_vkallocator = blob_vkallocator;
net.opt.workspace_vkallocator = blob_vkallocator; // Workspace often uses the same as blob
net.opt.staging_vkallocator = staging_vkallocator;
. . .
. . .
# clearing extractors after detection
extractor.clear();
blob_vkallocator->clear();
staging_vkallocator->clear();
workspace_vkallocator->clear();
However memory leak still seems to be there. Note: Leak stabilizes after a specific point in case of CPU but keeps growing indefinitely when model is loaded on GPU until the app crashes.
hi, ncnn-20230517-android-vulkan is outdated. please try the latest version
I actually updated the library to ncnn-20250503-android-vulkan as well. However, the memory leak issue was still there.
call vkdev->reclaim_blob_allocator() after uses
In fact, I don't recommend manually managing GPU memory settings, as you would need to handle the lifecycle of all memory allocations. The default options should work well.
ref yolov8 sample project https://github.com/nihui/ncnn-android-yolov8
@nihui even with the default options the memory leak still happening as .cpp memory allocation is increasing as the app runs.