Onnxruntime Java loading的内存溢出&持续增长问题

Onnxruntime Java loading的内存溢出&持续增长问题

背景

业务服务内存持续增加,看了一大堆文章分析来分析去也搞明白。

尝试

用jstat,jmap gdb perf MAT等工具分析了jvm的情况,总结一句话堆内内存没问题。 问题出在了堆外内存上。

又从github onnxruntime的issue上找到了问题所在 https://github.com/microsoft/onnxruntime/issues?q=memory+leak。

问题原因

OnnxTensor和OrtSession.Result都需要Close的。 但是在我之前程序中忽略了这个问题,还以为git repo的example写的try-catch是个多余的,万万没想到这两个类居然都实现AutoCloseable了。

解决方法

  1. 创建tensor和那预测结果都用try(onnx相关代码放这){}
  2. 手动执行下close。

参考

  1. https://github.com/microsoft/onnxruntime/issues?q=memory+lea
  2. https://juejin.cn/post/6844903810398158862
  3. https://www.jianshu.com/p/d23e7197d3fa
### ONNX Runtime 1.15.0 CPU C++ Installation and Usage Guide For installing the ONNX Runtime version 1.15.0 specifically tailored for C++ applications on a CPU, one can follow these instructions: #### Prerequisites Ensure that all necessary development tools are installed including CMake (version >= 3.12), as well as Python (for running tests). Additionally, ensure an environment capable of compiling C++ code is set up properly. #### Downloading Pre-built Binaries or Building from Source One option involves downloading pre-built binaries directly from GitHub releases page[^1]. Another approach entails building ONNX Runtime from source which allows customization but requires more setup effort. To download pre-built binaries: - Navigate to the official release section of ONNX Runtime's repository. - Select the appropriate package matching your operating system and architecture requirements. Alternatively, when opting to build from sources: ```bash git clone --recursive https://github.com/microsoft/onnxruntime.git cd onnxruntime ./build.sh --config Release --use_cuda=OFF --parallel --build_wheel ``` This command sequence clones the repository along with its submodules into `onnxruntime`, changes directory accordingly, then runs the build script specifying configuration parameters such as disabling CUDA support since this guide focuses on CPUs only while enabling parallel builds for efficiency purposes. After successfully acquiring either through downloads or compilation, integrate ONNX Runtime within projects by linking against static/dynamic libraries provided alongside header files during project configurations like so in CMakeLists.txt file: ```cmake find_package(ONNXRuntime REQUIRED) target_link_libraries(your_project_name PRIVATE onnxruntime::onnxruntime) include_directories(${ONNXRUNTIME_INCLUDE_DIRS}) ``` With respect to utilizing ONNX models after installation has been completed, refer to sample codes available under `/examples` folder inside cloned repositories where examples demonstrate loading model sessions, preparing inputs/outputs tensors, executing inference operations among others. --related questions-- 1. What are some common issues encountered while setting up ONNX Runtime? 2. How does one optimize performance using ONNX Runtime for specific hardware architectures? 3. Can you provide guidance on debugging problems related to ONNX Runtime installations? 4. Are there any differences between versions regarding API stability?
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值