Main Content

releaseGPUMemory

R2026b

Free GPU memory allocated by video object segmenter

Since R2026b

    Description

    releaseGPUMemory(vidSegmenter) frees GPU memory allocated by the sam2VideoObjectSegmenter object vidSegmenter during model initialization and inference. Use this function to reclaim GPU resources after completing segmentation, allowing additional models to run on the GPU.

    example

    Examples

    collapse all

    Free GPU resources after completing video object segmentation to allow other GPU-based models to run.

    Create a video object segmenter on the GPU, add objects, and run segmentation.

    vidSegmenter = sam2VideoObjectSegmenter("video.mp4", ExecutionEnvironment="gpu");
    addObjectsToSegment(vidSegmenter, "car", 1, ObjectPoints=[320,240]);
    
    for idx = 1:vidSegmenter.NumFrames
        [masks, objectIDs] = segmentObjects(vidSegmenter, idx);
    end

    Release the GPU memory used by the segmenter.

    releaseGPUMemory(vidSegmenter);

    Load and use another GPU-based model with the freed resources.

    detector = yolov4ObjectDetector("csp-darknet53-coco");
    img = imread("test_image.png");
    [bboxes, scores, labels] = detect(detector, img);

    Input Arguments

    collapse all

    Video object segmenter, specified as a sam2VideoObjectSegmenter object.

    Data Types: sam2VideoObjectSegmenter

    Tips

    • After calling releaseGPUMemory, calling segmentObjects on the same object may require the model to be reloaded, which incurs additional startup time.

    • This function has no effect when the segmenter was created with ExecutionEnvironment="cpu".

    Version History

    Introduced in R2026b