Skip to content

fix(ocr): clear ncnn inference allocator caches - #3

Closed
wyu71 wants to merge 0 commit into
linuxdeepin:masterfrom
wyu71:master
Closed

fix(ocr): clear ncnn inference allocator caches#3
wyu71 wants to merge 0 commit into
linuxdeepin:masterfrom
wyu71:master

Conversation

@wyu71

@wyu71 wyu71 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Clear ncnn blob and workspace pools after OCR completes. OCR 完成后清理 ncnn blob 和 workspace 缓存。

Log: 清理OCR推理缓存,避免重复识别内存持续增长
Influence: 保留模型权重,释放推理临时缓存;不改变公共接口。

Summary by Sourcery

Clear OCR inference memory pools after each analysis run to prevent temporary ncnn allocator caches from accumulating.

Bug Fixes:

  • Release ncnn blob and workspace pool allocators used by detection and recognition nets after OCR analysis completes to avoid unbounded memory growth.

Enhancements:

  • Route detection and recognition networks to use dedicated ncnn pool allocators for blob and workspace memory, enabling explicit cache clearing between OCR runs.

@sourcery-ai

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds explicit ncnn pool allocators for OCR detection/recognition networks and clears their blob/workspace caches after each OCR run to prevent inference memory from growing over repeated calls while keeping model weights resident.

Sequence diagram for OCR analyze clearing ncnn allocators

sequenceDiagram
  actor Client
  participant PaddleOCRApp
  participant detBlobAllocator
  participant detWorkspaceAllocator
  participant recBlobAllocator
  participant recWorkspaceAllocator

  Client->>PaddleOCRApp: analyze()
  alt [no textBoxes]
    PaddleOCRApp-->>Client: return false
  else [textBoxes not empty]
    PaddleOCRApp-->>Client: return true
  end
  note over PaddleOCRApp: At end of analyze()
  PaddleOCRApp->>PaddleOCRApp: clearInferenceAllocators()
  PaddleOCRApp->>detBlobAllocator: clear()
  PaddleOCRApp->>detWorkspaceAllocator: clear()
  PaddleOCRApp->>recBlobAllocator: clear()
  PaddleOCRApp->>recWorkspaceAllocator: clear()
Loading

File-Level Changes

Change Details Files
Introduce dedicated ncnn pool allocators for detection and recognition nets and wire them into Net options so inference uses reusable pools instead of default allocators.
  • Include ncnn allocator header in the OCR app header to access PoolAllocator types.
  • Add four PoolAllocator members to PaddleOCRApp for det/rec blob and workspace allocations.
  • Assign these allocators to detNet and recNet ncnn::Net::opt blob_allocator and workspace_allocator during initNet().
src/paddleocr-ncnn/paddleocr.h
src/paddleocr-ncnn/paddleocr.cpp
Ensure inference temporary memory is released after each analyze() run by clearing the pool allocators once OCR completes or aborts.
  • Add clearInferenceAllocators() helper that clears all four PoolAllocator instances on the PaddleOCRApp.
  • Call clearInferenceAllocators() on the early-return error path in analyze() to avoid leaking pooled buffers when OCR is interrupted.
  • Call clearInferenceAllocators() on the normal completion path in analyze() while preserving the existing boolean result return semantics.
src/paddleocr-ncnn/paddleocr.cpp
src/paddleocr-ncnn/paddleocr.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码通过引入自定义内存分配器并在推理后显式清理,有效修复了内存泄漏问题
逻辑清晰,实现正确,无安全漏洞,代码质量良好

■ 【详细分析】

  • 1.语法逻辑(基本正确)✓

代码在 PaddleOCRApp::analyze 的两个退出分支均调用了 clearInferenceAllocators,确保内存被释放。initNet 中正确绑定了分配器。
潜在问题:若 analyze 内部存在其他异常退出路径,可能遗漏清理调用
建议:考虑使用 RAII 机制或在 analyze 函数开头使用 try-catch 确保清理逻辑必然执行

  • 2.代码质量(良好)✓

命名规范清晰,clearInferenceAllocators 函数职责单一,代码结构整洁
潜在问题:分配器作为成员变量直接暴露在类内部,耦合度略高
建议:可考虑封装为单独的内存管理类以提高内聚性

  • 3.代码性能(无性能问题)✓

通过 PoolAllocatorclear 方法释放池化内存,解决了 NCNN 推理内存只增不减的问题,避免了长期运行时的内存占用持续增长
建议:注意观察频繁清理和重新分配内存是否对推理速度有显著影响,必要时可考虑延迟清理策略

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次修改仅涉及内存分配器的初始化与清理,未引入外部输入处理,不存在安全风险

  • 建议:保持现有安全编码规范

■ 【改进建议代码示例】

// 考虑使用 RAII 或确保所有退出路径调用清理逻辑
bool PaddleOCRApp::analyze()
{
    // 使用 scope guard 确保清理
    auto guard = finally([this] {
        clearInferenceAllocators();
    });

    // ... 前置推理逻辑 ...
    if (needBreak) {
        allResult.clear();
        boxesResult.clear();
        needBreak = false;
        return false;
    } else {
        //对识别结果进行最后清理,将未识别到文字的检测框排除掉
        // ...
        return !textBoxes.empty();
    }
}

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • Consider also clearing the pool allocators in resetNet() so that any cached inference memory is released when the networks are torn down or reinitialized, not only after analyze() completes.
  • If analyze() has or gains additional early-return code paths in the future, it might be safer to ensure clearInferenceAllocators() is called via a small RAII helper or at a single exit point to avoid missing cache clears.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider also clearing the pool allocators in `resetNet()` so that any cached inference memory is released when the networks are torn down or reinitialized, not only after `analyze()` completes.
- If `analyze()` has or gains additional early-return code paths in the future, it might be safer to ensure `clearInferenceAllocators()` is called via a small RAII helper or at a single exit point to avoid missing cache clears.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wyu71

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants