ncnn icon indicating copy to clipboard operation
ncnn copied to clipboard

Memory leak when using GPU (Vulkan) in Android

Open shery909 opened this issue 7 months ago • 5 comments

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.

Image

shery909 avatar Jul 21 '25 07:07 shery909

hi, ncnn-20230517-android-vulkan is outdated. please try the latest version

nihui avatar Jul 24 '25 06:07 nihui

I actually updated the library to ncnn-20250503-android-vulkan as well. However, the memory leak issue was still there.

shery909 avatar Jul 24 '25 09:07 shery909

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.

nihui avatar Jul 24 '25 11:07 nihui

ref yolov8 sample project https://github.com/nihui/ncnn-android-yolov8

nihui avatar Jul 24 '25 11:07 nihui

@nihui even with the default options the memory leak still happening as .cpp memory allocation is increasing as the app runs.

shery909 avatar Sep 22 '25 12:09 shery909