Skip to content

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

Merged
wyu71 merged 1 commit into
linuxdeepin:masterfrom
wyu71:master
Jul 28, 2026
Merged

fix(ocr): clear ncnn inference allocator caches#5
wyu71 merged 1 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

Manage and clear ncnn OCR inference memory pools after each analysis to prevent temporary allocator caches from growing unbounded while keeping model weights loaded.

Bug Fixes:

  • Prevent OCR inference blob and workspace allocator caches from accumulating across runs, avoiding gradual memory growth during repeated recognition.

Enhancements:

  • Configure detection and recognition networks to use dedicated ncnn pool allocators for blob and workspace memory, enabling explicit lifecycle control of inference buffers.

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

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

sourcery-ai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR wires custom ncnn pool allocators into the OCR detection and recognition networks and clears their blob/workspace pools after each analyze() call to prevent inference cache memory from growing, without changing public interfaces or model weights.

Sequence diagram for analyze and allocator clearing

sequenceDiagram
    participant App as PaddleOCRApp
    participant DetNet as detNet
    participant RecNet as recNet

    App->>App: initNet()
    App->>DetNet: detect(...)
    App->>RecNet: rec(...)
    alt [no textBoxes or needBreak]
        App->>App: clearInferenceAllocators()
        App-->>App: return false
    else [textBoxes not empty]
        App->>App: clearInferenceAllocators()
        App-->>App: return true
    end
Loading

File-Level Changes

Change Details Files
Introduce dedicated ncnn PoolAllocator instances for detection and recognition nets and hook them into ncnn::Net options
  • Include ncnn allocator header to access PoolAllocator
  • Add four PoolAllocator members for det/rec blob and workspace allocations to PaddleOCRApp
  • Assign these allocators to detNet and recNet option.blob_allocator and option.workspace_allocator during initNet()
src/paddleocr-ncnn/paddleocr.h
src/paddleocr-ncnn/paddleocr.cpp
Clear inference-time allocator pools after each OCR analyze() invocation to release temporary memory caches
  • Add clearInferenceAllocators() helper on PaddleOCRApp that clears all four PoolAllocator members
  • Call clearInferenceAllocators() in analyze() on both early-return (needBreak) and normal completion paths, preserving the existing boolean result logic
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分

■ 【总体评价】

代码通过引入池分配器并统一清理,有效修复了ncnn推理后的内存泄漏问题。
逻辑正确且无安全漏洞,性能与质量均表现优秀,故给予满分。

■ 【详细分析】

  • 1.语法逻辑 完全正确 ✓

paddleocr.cpppaddleocr.h 中新增了 clearInferenceAllocators() 方法的声明与定义,并在 initNet() 中正确绑定了四个池分配器。在 analyze() 函数的失败路径和成功路径末尾均调用了清理方法,确保所有分支都能正确释放内存。
潜在问题:无
建议:无

  • 2.代码质量 良好 ✓

命名规范清晰,detBlobAllocator 等变量名准确表达了其用途。新增的 clearInferenceAllocators() 方法封装了清理逻辑,避免了在 analyze() 中出现重复代码。引入了 #include <ncnn/allocator.h> 以支持池分配器的使用,符合规范。
潜在问题:无
建议:无

  • 3.代码性能 高效 ✓

通过绑定 ncnn::PoolAllocatordetNetrecNet,避免了推理过程中默认分配器频繁进行内存分配和释放,提升了推理性能。在推理结束后调用 clear() 释放池内存,防止了长期的内存占用和泄漏。
潜在问题:无
建议:无

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

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次修改仅涉及内部内存管理逻辑的优化,未引入任何外部输入处理或系统调用,不存在安全风险。

  • 建议:无

■ 【改进建议代码示例】

// 当前代码已完美修复内存泄漏问题,无需额外修改。
// 保持现有实现即可:
void PaddleOCRApp::clearInferenceAllocators()
{
    detBlobAllocator.clear();
    detWorkspaceAllocator.clear();
    recBlobAllocator.clear();
    recWorkspaceAllocator.clear();
}

@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 making clearInferenceAllocators() exception/early-return safe by tying it to an RAII helper or a small scoped object in analyze() so that future changes adding new return paths don’t accidentally bypass allocator cleanup.
  • If PaddleOCRApp::analyze() can be called from multiple threads concurrently, clearing shared PoolAllocator instances in clearInferenceAllocators() may introduce race conditions; clarifying the threading model or guarding allocator access would make this safer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider making `clearInferenceAllocators()` exception/early-return safe by tying it to an RAII helper or a small scoped object in `analyze()` so that future changes adding new return paths don’t accidentally bypass allocator cleanup.
- If `PaddleOCRApp::analyze()` can be called from multiple threads concurrently, clearing shared `PoolAllocator` instances in `clearInferenceAllocators()` may introduce race conditions; clarifying the threading model or guarding allocator access would make this safer.

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: pengfeixx, 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

@wyu71
wyu71 merged commit 8213eb9 into linuxdeepin:master Jul 28, 2026
20 checks passed
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.

3 participants