FastDeploy
FastDeploy copied to clipboard
重复刷新模型造成的内存泄露问题
温馨提示:根据社区不完全统计,按照模板提问,可以加快回复和解决问题的速度
环境
【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;
}