PaddlePaddle / PaddlePaddle/FastDeploy

重复刷新模型造成的内存泄露问题

Open
#2,511 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
3.7k
Forks
756
Avg merge
19h 28m
Merged PRs (30d)
4

Description


温馨提示:根据社区不完全统计,按照模板提问,可以加快回复和解决问题的速度


环境

【FastDeploy版本】: deploy自己编译
【编译命令】export ENABLE_ORT_BACKEND = OFF
export ENABLE_PADDLE_BACKEND = OFF
export ENABLE_OPENVINO_BACKEND = OFF
export ENABLE_VISION = ON
export ENABLE_TEXT = OFF
export ENABLE_TRT_BACKEND = OFF
export WITH_GPU = OFF
【系统平台】: Windows 11
【硬件】: 笔记本 intel i7-1260p
【编译语言】:C++

问题日志及出现问题的操作流程

由于工程要求,每次刷新相机之后都需要重新刷新一次AI模型。经过测试发现,如果重复调用多次模型初始化函数,会造成内存泄漏,每隔10s~20s会泄露0.3M。下面是我的初始化函数
bool OcrModel::InitModel(OcrModelConfig& config) {
// 将 GBK 路径转换为 UTF-8 路径
std::string model_dir = config.strNetPath;

// 构建模型文件的路径
std::filesystem::path model_path = std::filesystem::path(model_dir) / "inference.pdmodel";
std::filesystem::path params_path = std::filesystem::path(model_dir) / "inference.pdiparams";
std::filesystem::path config_path = std::filesystem::path(model_dir) / "inference.yaml";

std::string& strError = config.strError;

// 检查文件是否存在
if (!fs::exists(model_path) || !fs::exists(params_path)) {
    config.strError = "Model or parameter file does not exist.";
    return false;
}

// 初始化模型
option.UseCpu();  // 使用 CPU
try {
    model = fastdeploy::vision::ocr::DBDetector(model_path.string(), params_path.string(), option);
}
catch (const std::exception& e) {
    strError = "Failed to create the OCR model: " + std::string(e.what());
    return false;
}

if (!model.Initialized()) {
    strError = "Failed to initialize the OCR model.";
    return false;
}

try {
    model.GetPostprocessor().SetDetDBThresh(config.det_db_thresh);
    model.GetPostprocessor().SetDetDBBoxThresh(config.det_db_box_thresh);
    model.GetPostprocessor().SetDetDBUnclipRatio(config.det_db_unclip_ratio);
}
catch (const std::exception& e) {
    strError = "Failed to set post-processing parameters: " + std::string(e.what());
    return false;
}
return true;

}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OcrModel::InitModel function shown in the report, especially repeated assignment of the fastdeploy::vision::ocr::DBDetector model. Reproduce repeated model refreshes on Windows 11 with the reported CPU configuration and measure memory over time; done means repeated initialization no longer causes the reported leak.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
ai, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.