Skip to content

Python program creates memory leak of Pose Landmarks on Linux only #6351

Open

Description

@Zoey-Vail

Have I written custom code (as opposed to using a stock example script provided in MediaPipe)

Yes

OS Platform and Distribution

Linux Arch 7.0.10-arch1-1

MediaPipe Tasks SDK version

1.0.1

Task name (e.g. Image classification, Gesture recognition etc.)

Pose Landmark Detection

Programming Language and version (e.g. C++, Python, Java)

Python 3.14.5

Describe the actual behavior

I was moving a mediapipe program I was making on my windows desktop to my Linux laptop, I made a new virtual environment and a fresh install of mediapipe. I was starting to get a memory leak issue that caused my program to stop functioning, and I used objgraph.show_most_common_types() to see if there was a buildup of certain objects.

After a little while, there starts to be a buildup of objects of types: Landmark, NormalizedLandmark, LP_MpLandmarkC, and LP_MpNormalizedLandmarkC, all of them in the same amount.

I found a way to de-reference the results causing the Landmark and NormalizedLandmark objects in the callback function of detectAsync, which stopped them building up so much, but I can't figure out what LP_MpLandmarkC and LP_MpNormalizedLandmarkC are, nor why the garbage collector doesn't get rid of them, or how to de-reference them manually. I can't find any documentation of these data types.

Describe the expected behaviour

There should be no buildup of LP_MpLandmarkC and LP_MpNormalizedLandmarkC objects, I need a way to de-reference these excess objects or otherwise stop them building up and causing a memory leak.

Standalone code/steps you may have used to try to get what you need

import cv2
import mediapipe as mp
from mediapipe.tasks import python
import gc
import objgraph
from memory_profiler import profile
import tracemalloc

mp_pose = mp.tasks.vision.PoseLandmarker
mp_pose_options = mp.tasks.vision.PoseLandmarkerOptions
PoseLandmarkerResult = mp.tasks.vision.PoseLandmarkerResult
VisionRunningMode = mp.tasks.vision.RunningMode

class camera_settings():
    def __init__(self):
        self.camera_index = 0
        self.camera_FPS_target = 30


def initialize_detector(camera_data):
    global pose_results
    pose_results = result_handler()
    global cap
    cap = cv2.VideoCapture(camera_data.camera_index)
    cap.set(cv2.CAP_PROP_FPS, camera_data.camera_FPS_target)
    cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)
    start_time = cv2.getTickCount()

    # Define settings for pose detection
    options = mp_pose_options(
        base_options = python.BaseOptions(model_asset_path='pose_landmarker_lite.task'),
        min_pose_detection_confidence = 0.7,
        min_tracking_confidence = 0.7,
        min_pose_presence_confidence = 0.7,
        running_mode=VisionRunningMode.LIVE_STREAM,
        # Define the callback function to run when results are acquired
        result_callback=pose_results.update_result
    )
    return options, pose_results, cap, start_time

def cam_detection(options, pose_results, cap, start_time):
    # Use the options defined earlier to initialize the pose identification
    with mp_pose.create_from_options(options) as pose:
        
        while cap.isOpened() == True:
            
            # Get the timestamp for the start of this cycle
            frame_timestamp_ms = int((cv2.getTickCount() - start_time) * 1000 / cv2.getTickFrequency())
            # Read the image from the camera and store it in 'frame'
            ret, frame = cap.read()
            # Convert the frame from RGB to BGR color, as BGR can be read quicker
            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
            # Convert the recolored frame to a mediapipe image
            mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=frame)
            # detect the landmarks from the image (the callback function will run right after)
            mp_pose.detect_async(pose, mp_image, frame_timestamp_ms)

            gc.collect()
            
            objgraph.show_most_common_types()
            


# Contains the results of the detection in Mediapipe's result structure and updates the results via the update_result callback
class result_handler():
    def __init__(self):
        self.result_landmarks = None

    def update_result(self, result: PoseLandmarkerResult, output_image: mp.Image, timestamp_ms: int):
        
            self.result_landmarks = result.pose_landmarks[0]
            result.pose_landmarks = None
            result.pose_world_landmarks = None


            gc.collect()

camera_data = camera_settings()
options, result, cap, start_time = initialize_detector(camera_data)
cam_detection(options, result, cap, start_time)

Other info / Complete Logs

This is the output of objgraph.show_most_common_types(), which is run every frame. LP_MpLandmarkC and LP_MpNormalizedLandmarkC increase quickly. This issue only really started on my Linux laptop, I don't experience the memory leak on Windows.

builtin_method             36917
LP_MpNormalizedLandmarkC   19074
LP_MpLandmarkC             19074
function                   18128
dict                       13449
tuple                      11984
list                       11124
Parameter                  5395
ReferenceType              4913
builtin_function_or_method 4857

Activity

  1. Zoey-Vail commented on Aug 30, 2026

    @Zoey-Vail
    Author

    *UPDATE
    I've managed to at least figure out that LP_MpLandmarkC and LP_MpNormalizedLandmarkC exist in c_result variable from the convert_result function in mediapipe/tasks/python/vision/pose_landmarker.py, and that they are pointers to MpLandmarkC and MpNormalizedLandmarkC objects, (I don't know if these pointers come up anywhere else). However I still can't figure out how to de-reference them, Everything I've attempted either did nothing or breaks the detection. I'm seriously confused as to why this issue doesn't happen on my Windows machine, but it does on Linux, at least for me.

  2. added
    task:pose landmarkerIssues related to Pose Landmarker: Find people and body positions
    os:linux-non-armIssues on linux distributions which run on x86-64 architecture. DOES NOT include ARM devices.
    on Aug 31, 2026
  3. HarishPermal commented on Sep 7, 2026

    @HarishPermal

    hi @Zoey-Vail

    Sorry for the delay, i've been testing this on python 3.13 i didn't see any memory leaks, i think this could be a Cpython bug

    user@user:~/Documents/6351/test_python_13$ venv313/bin/python test.py 
    INFO: Created TensorFlow Lite XNNPACK delegate for CPU.
    WARNING: Logging before InitGoogle() is written to STDERR
    W0000 00:00:1788777760.428542 1011095 inference_feedback_manager.cc:121] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.
    W0000 00:00:1788777760.458806 1011095 inference_feedback_manager.cc:121] Feedback manager requires a model with a single signature inference. Disabling support for feedback tensors.
    W0000 00:00:1788777760.500594 1011097 landmark_projection_calculator.cc:81] Using NORM_RECT without IMAGE_DIMENSIONS is only supported for the square ROI. Provide IMAGE_DIMENSIONS or use PROJECTION_MATRIX.
    3.13.15 (main, Aug 11 2026, 08:30:27) [GCC 15.2.0]
    Completed callbacks: 200
    Object-count changes:
      LP_MpLandmarkC: +0
      LP_MpNormalizedLandmarkC: +0
      Landmark: +0
      NormalizedLandmark: +0
      LiveStreamPacket: +0
      Image: +0
      LP_MpPoseLandmarkerResultC: +0
    RSS change: +56.3 MiB
    

    Im still trying with different version of python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

os:linux-non-armIssues on linux distributions which run on x86-64 architecture. DOES NOT include ARM devices.task:pose landmarkerIssues related to Pose Landmarker: Find people and body positions

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions