Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TensorRT 10.5.0 -- CPU Memory leak while using nvinfer1::createInferBuilder on 4060 #4281

Closed
Iridium771110 opened this issue Dec 12, 2024 · 3 comments
Assignees
Labels
Engine Build Issues with engine build triaged Issue has been triaged by maintainers

Comments

@Iridium771110
Copy link


Description

when I using nvinfer::IBuilder = nvinfer1::createInferBuilder (logger) to create a model engine, it seems a CPU Memory leak.
the full code is shown below.

#include <iostream>
#include <string>
#include <cstring>
#include <NvInfer.h>

class Logger : public nvinfer1::ILogger{
    void log(Severity severity, const char* msg) noexcept override{
        if (severity <= Severity::kWARNING) std::cout<<msg<<std::endl;
    }
};

size_t physical_memory_used_by_process(){
    FILE* file = fopen("/proc/self/status", "r");
    int result = -1;
    char line[128];
    while (fgets(line, 128, file) != nullptr) {
        if (strncmp(line, "VmRSS:", 6) == 0) {
            int len = strlen(line);
            const char* p = line;
            for (; std::isdigit(*p) == false; ++p) {}
            line[len - 3] = 0;
            result = atoi(p);
            break;
        }
    }
    fclose(file);
    return result;
}

int main(int argc, char *argv[]){
    size_t gpu_total_byte_mem;
    size_t gpu_free_byte_mem;
    size_t gpu_alloced_byte_mem;
    cudaDeviceSynchronize();
    cudaMemGetInfo(&gpu_free_byte_mem, &gpu_total_byte_mem);
    gpu_alloced_byte_mem = gpu_total_byte_mem - gpu_free_byte_mem;
    std::cout<<"init status"<<std::endl;
    std::cout<<"used gpu mem: "<<double(gpu_alloced_byte_mem) / 1024.0 / 1024.0<<"MB"<<std::endl;
    std::cout<<"used cpu mem: "<<double(physical_memory_used_by_process()) / 1024.0 <<"MB"<<std::endl;

    Logger logger;

    cudaDeviceSynchronize();
    cudaMemGetInfo(&gpu_free_byte_mem, &gpu_total_byte_mem);
    gpu_alloced_byte_mem = gpu_total_byte_mem - gpu_free_byte_mem;
    std::cout<<"logger created"<<std::endl;
    std::cout<<"used gpu mem: "<<double(gpu_alloced_byte_mem) / 1024.0 / 1024.0<<"MB"<<std::endl;
    std::cout<<"used cpu mem: "<<double(physical_memory_used_by_process()) / 1024.0 <<"MB"<<std::endl;

    nvinfer1::IBuilder* builder = nvinfer1::createInferBuilder(logger);

    cudaDeviceSynchronize();
    cudaMemGetInfo(&gpu_free_byte_mem, &gpu_total_byte_mem);
    gpu_alloced_byte_mem = gpu_total_byte_mem - gpu_free_byte_mem;
    std::cout<<"builder created"<<std::endl;
    std::cout<<"used gpu mem: "<<double(gpu_alloced_byte_mem) / 1024.0 / 1024.0<<"MB"<<std::endl;
    std::cout<<"used cpu mem: "<<double(physical_memory_used_by_process()) / 1024.0 <<"MB"<<std::endl;

    delete builder;

    cudaDeviceSynchronize();
    cudaMemGetInfo(&gpu_free_byte_mem, &gpu_total_byte_mem);
    gpu_alloced_byte_mem = gpu_total_byte_mem - gpu_free_byte_mem;
    std::cout<<"builder deleted"<<std::endl;
    std::cout<<"used gpu mem: "<<double(gpu_alloced_byte_mem) / 1024.0 / 1024.0<<"MB"<<std::endl;
    std::cout<<"used cpu mem: "<<double(physical_memory_used_by_process()) / 1024.0 <<"MB"<<std::endl;

    return 0;
}

build the code and run, I see the memory printed as
Image
so it seems like a CPU memory leak by createInferBuilder API or IBuilder pointer.
do you have any suggestion here? thanks.

Environment

TensorRT Version: 10.5.0

NVIDIA GPU: 4060

NVIDIA Driver Version: 560.35.03

CUDA Version: 12.4

CUDNN Version: --

Operating System:

Python Version (if applicable):

Tensorflow Version (if applicable):

PyTorch Version (if applicable):

Baremetal or Container (if so, version):

Steps To Reproduce

just build the code and run it.

@lix19937
Copy link

You had better to use Valgrind to detect memory management .

@asfiyab-nvidia asfiyab-nvidia added Engine Build Issues with engine build triaged Issue has been triaged by maintainers labels Dec 23, 2024
@asfiyab-nvidia
Copy link
Collaborator

@zhenhuaw-me can you please look into this?

@zhenhuaw-me
Copy link
Member

@Iridium771110 Thank you for reporting this issue. Some CUDA resources such as kernels are released when the process terminated. So the measurement is inaccurate.

As @lix19937 , if you think there is a memory leak, please use valgrind to detect and confirm.

I am going to close this ticket since it's a false alarm. Feel free to reopen if any further concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Engine Build Issues with engine build triaged Issue has been triaged by maintainers
Projects
None yet
Development

No branches or pull requests

4 participants