Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to release a session properly? #11529

Open
beelabcloud opened this issue May 15, 2022 · 11 comments
Open

How to release a session properly? #11529

beelabcloud opened this issue May 15, 2022 · 11 comments

Comments

@beelabcloud
Copy link

I 'm using ORT C++ API to infer several models sequentialy. The consumption of memory is pretty much, so I try to unload some sessones after inference. However the consumption is not fully freed, and no memory leak detected. And I notice that the memory will be freed by the OS after a while (1 hour background alive), I guess, it might be recycled by the mechanism of system garbage collection. So I am wondering it is there some magic API to recycle the garbage, and I would like to know what cause the memory residents, the threads pooling or the weights? Is there a way to tell the OS to do recycle instantly? Thanks.

@RyanUnderhill
Copy link
Member

As long as the ORT object destructors are being called (which call the C OrtApi::Release* methods) the memory is released immediately to the OS. There is no garbage collection inside of Onnxruntime.

Can you give more details of what you're looking at? Which OS, and which memory usage metric?

@beelabcloud
Copy link
Author

beelabcloud commented May 19, 2022

session = new Ort::Session(*env, model_data, (size_t)model_data_length, sessionOptions);

If you do new, then you are responsible to call delete. Ort obejcts are designed to be instantiated by value, so the destructors are automatically called and clean up native resources. They are very much like smart pointers. One usually does not call new on, say std::unique_ptr.

@beelabcloud
Copy link
Author

By the way, the comsumption is not caused by the inference, and the program can run flawlessly for a few days, and not eating any extra memory. Thank you very much.

@beelabcloud
Copy link
Author

bool MessageLite::ParseFrom(const T& input) {
if (flags & kParse) Clear();
constexpr bool alias = (flags & kMergeWithAliasing) != 0;
return internal::MergeFromImpl(input, this, flags);
}
Is it that Kparse means keep and parse?

@beelabcloud
Copy link
Author

Should I call ::google::protobuf::ShutdownProtobufLibrary() for each cycle?

@tianleiwu
Copy link
Contributor

tianleiwu commented May 19, 2022

Is it that Kparse means keep and parse?

I search kParse and kMergeWithAliasing in onnxruntime code, there is no result found. Is the example code from OnnxRuntime?

Should I call ::google::protobuf::ShutdownProtobufLibrary() for each cycle?

I have no experience on this so I cannot answer it. You can have a try. It might introduce other issue.

@beelabcloud
Copy link
Author

Hi tianlei, thank you. "kParse" is argument used in the model loading codes in file "cmake\external\protobuf\src\google\protobuf\message_lite.h".
::google::protobuf::ShutdownProtobufLibrary() would shudwon Protobuf entirely as I read. I'm pretty sure that the model data array is retained, and it would be recycled by the OS garbage collection after a long time idle.

@ns-wxin
Copy link

ns-wxin commented Jan 31, 2023

I rebuilt onnxruntime with ASAN and got the following leak stack. Wonder if anyone shed some lights on how to release the memory correctly.

Indirect leak of 8 byte(s) in 1 object(s) allocated from:
#0 0x7f22934ff758 in operator new(unsigned long) ../../../../libsanitizer/asan/asan_new_delete.cpp:95
#1 0x7f228698e5c2 in void std::vector<onnxruntime::NodeArg const*, std::allocator<onnxruntime::NodeArg const*> >::_M_realloc_insert<onnxruntime::NodeArg const* const&>(__gnu_cxx::__normal_iterator<onnxruntime::NodeArg const**, std::vector<onnxruntime::NodeArg const*, std::allocator<onnxruntime::NodeArg const*> > >, onnxruntime::NodeArg const* const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x25f05c2)
#2 0x7f22869cd4c7 in onnxruntime::Graph::InitializeStateFromModelFileGraphProto() (/opt/3p/lib/libonnxruntime.so.1.12.1+0x262f4c7)
#3 0x7f2286a28311 in onnxruntime::Graph::Graph(onnxruntime::Model const&, onnx::GraphProto*, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int, std::hash<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, int> > > const&, long, std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, onnxruntime::Graph*, onnxruntime::Node const*, onnxruntime::logging::Logger const&, bool) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x268a311)
#4 0x7f2286a29949 in onnxruntime::Graph::Graph(onnxruntime::Model const&, onnx::GraphProto*, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int, std::hash<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, int> > > const&, long, std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, onnxruntime::logging::Logger const&, bool) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x268b949)
#5 0x7f2286ae3bfe in onnxruntime::Model::Model(onnx::ModelProto&&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::__cxx11::list<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, std::allocator<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection > > const*, onnxruntime::logging::Logger const&, onnxruntime::ModelOptions const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x2745bfe)
#6 0x7f2286ae81f7 in onnxruntime::Model::Load(int, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::shared_ptronnxruntime::Model&, std::__cxx11::list<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, std::allocator<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection > > const*, onnxruntime::logging::Logger const&, onnxruntime::ModelOptions const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x274a1f7)
#7 0x7f2286ae8af4 in onnxruntime::common::Status onnxruntime::LoadModelHelper<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, onnxruntime::LoadModel<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::shared_ptronnxruntime::Model&, std::__cxx11::list<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, std::allocator<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection > > const*, onnxruntime::logging::Logger const&, onnxruntime::ModelOptions const&)::{lambda(int)#1}>(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, onnxruntime::LoadModel<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::shared_ptronnxruntime::Model&, std::__cxx11::list<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, std::allocator<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection > > const*, onnxruntime::logging::Logger const&, onnxruntime::ModelOptions const&)::{lambda(int)#1}) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x274aaf4)
#8 0x7f2286ae97cc in onnxruntime::Model::Load(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, std::shared_ptronnxruntime::Model&, std::__cxx11::list<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection, std::allocator<std::shared_ptronnxruntime::IOnnxRuntimeOpSchemaCollection > > const*, onnxruntime::logging::Logger const&, onnxruntime::ModelOptions const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x274b7cc)
#9 0x7f2284b2aec2 in std::_Function_handler<onnxruntime::common::Status (std::shared_ptronnxruntime::Model&), onnxruntime::InferenceSession::Load(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)::{lambda(std::shared_ptronnxruntime::Model&)#1}>::_M_invoke(std::_Any_data const&, std::shared_ptronnxruntime::Model&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x78cec2)
#10 0x7f2284b7a3e5 in onnxruntime::InferenceSession::Load(std::function<onnxruntime::common::Status (std::shared_ptronnxruntime::Model&)>, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x7dc3e5)
#11 0x7f2284b7b582 in onnxruntime::common::Status onnxruntime::InferenceSession::Load(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x7dd582)
#12 0x7f2284ba2002 in onnxruntime::InferenceSession::Load(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x804002)
#13 0x7f2284a051b6 in (anonymous namespace)::CreateSessionAndLoadModel(OrtSessionOptions const*, OrtEnv const*, char const*, void const*, unsigned long, std::unique_ptr<onnxruntime::InferenceSession, std::default_deleteonnxruntime::InferenceSession >&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x6671b6)
#14 0x7f2284a08364 in OrtApis::CreateSession(OrtEnv const*, char const*, OrtSessionOptions const*, OrtSession**) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x66a364)
#15 0x5f0bdb in Ort::Session::Session(Ort::Env&, char const*, Ort::SessionOptions const&) /opt/3p/include/onnxruntime/core/session/onnxruntime_cxx_inline.h:596
#16 0x5f0bdb in fingerprint::ImageFingerprintGenerator::loadModel(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) ../libs/dlp/fp20svc/src/ImageFingerprintGenerator.cpp:84
#17 0x5f2945 in fingerprint::ImageFingerprintGenerator::ImageFingerprintGenerator(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int) ../libs/dlp/fp20svc/src/ImageFingerprintGenerator.cpp:43
#18 0x45b619 in void std::_Construct<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(fingerprint::ImageFingerprintGenerator*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#19 0x45b619 in void std::allocator_traits<std::allocator >::construct<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::allocator&, fingerprint::ImageFingerprintGenerator*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#20 0x45b619 in std::_Sp_counted_ptr_inplace<fingerprint::ImageFingerprintGenerator, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#21 0x45b619 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::ImageFingerprintGenerator, std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(fingerprint::ImageFingerprintGenerator*&, std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#22 0x45b619 in std::__shared_ptr<fingerprint::ImageFingerprintGenerator, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#23 0x45b619 in std::shared_ptrfingerprint::ImageFingerprintGenerator::shared_ptr<std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#24 0x45b619 in std::shared_ptrfingerprint::ImageFingerprintGenerator std::make_shared<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#25 0x45b619 in fingerprint::FingerprintGeneratorPool::FingerprintGeneratorPool(int, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int) ../libs/dlp/fp20svc/ImageFingerprintGenerator.hpp:137
#26 0x45b619 in void std::_Construct<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(fingerprint::FingerprintGeneratorPool*, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#27 0x45b619 in void std::allocator_traits<std::allocator >::construct<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(std::allocator&, fingerprint::FingerprintGeneratorPool*, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#28 0x45b619 in std::_Sp_counted_ptr_inplace<fingerprint::FingerprintGeneratorPool, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<int, char const (&) [46], int>(std::allocator, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#29 0x45b619 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::FingerprintGeneratorPool, std::allocator, int, char const (&) [46], int>(fingerprint::FingerprintGeneratorPool*&, std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#30 0x45b619 in std::__shared_ptr<fingerprint::FingerprintGeneratorPool, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, int, char const (&) [46], int>(std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#31 0x45b619 in std::shared_ptrfingerprint::FingerprintGeneratorPool::shared_ptr<std::allocator, int, char const (&) [46], int>(std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#32 0x45b619 in std::shared_ptrfingerprint::FingerprintGeneratorPool std::make_shared<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#33 0x445036 in __static_initialization_and_destruction_0 ../libs/dlp/fp20svc/test/src/DlpFp20Test.cpp:35
#34 0x6ccafc in __libc_csu_init (/home/wxin/src/ns/dataplane/obj-asan/dlp_fp20_lib_test+0x6ccafc)

@RyanUnderhill
Copy link
Member

@ns-wxin Could you use a more recent build? It looks like you're using onnxruntime 1.12.1? Is the leak only 8 bytes?

@ns-wxin
Copy link

ns-wxin commented Feb 1, 2023

Yeah. It's 1.12.1. There's various leaks detected such as the following leaking 536 bytes. Looks like the default constructor of SessionOptions allocated some space but not released. I tried Ort::SessionOptions.release() in my destructor but it didn't seem to get rid of this leak.

Direct leak of 536 byte(s) in 1 object(s) allocated from:
#0 0x7f267fc33758 in operator new(unsigned long) ../../../../libsanitizer/asan/asan_new_delete.cpp:95
#1 0x7f26711d1287 in OrtApis::CreateSessionOptions(OrtSessionOptions**) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x6ff287)
#2 0x5f24b2 in Ort::SessionOptions::SessionOptions() /opt/3p/include/onnxruntime/core/session/onnxruntime_cxx_inline.h:412
#3 0x5f24b2 in fingerprint::ImageFingerprintGenerator::ImageFingerprintGenerator(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int) ../libs/dlp/fp20svc/src/ImageFingerprintGenerator.cpp:40
#4 0x45b619 in void std::_Construct<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(fingerprint::ImageFingerprintGenerator*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#5 0x45b619 in void std::allocator_traits<std::allocator >::construct<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::allocator&, fingerprint::ImageFingerprintGenerator*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#6 0x45b619 in std::_Sp_counted_ptr_inplace<fingerprint::ImageFingerprintGenerator, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#7 0x45b619 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::ImageFingerprintGenerator, std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(fingerprint::ImageFingerprintGenerator*&, std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#8 0x45b619 in std::__shared_ptr<fingerprint::ImageFingerprintGenerator, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#9 0x45b619 in std::shared_ptrfingerprint::ImageFingerprintGenerator::shared_ptr<std::allocator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::_Sp_alloc_shared_tag<std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#10 0x45b619 in std::shared_ptrfingerprint::ImageFingerprintGenerator std::make_shared<fingerprint::ImageFingerprintGenerator, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&>(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#11 0x45b619 in fingerprint::FingerprintGeneratorPool::FingerprintGeneratorPool(int, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int) ../libs/dlp/fp20svc/ImageFingerprintGenerator.hpp:137
#12 0x45b619 in void std::_Construct<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(fingerprint::FingerprintGeneratorPool*, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#13 0x45b619 in void std::allocator_traits<std::allocator >::construct<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(std::allocator&, fingerprint::FingerprintGeneratorPool*, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#14 0x45b619 in std::_Sp_counted_ptr_inplace<fingerprint::FingerprintGeneratorPool, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<int, char const (&) [46], int>(std::allocator, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#15 0x45b619 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::FingerprintGeneratorPool, std::allocator, int, char const (&) [46], int>(fingerprint::FingerprintGeneratorPool*&, std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#16 0x45b619 in std::__shared_ptr<fingerprint::FingerprintGeneratorPool, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, int, char const (&) [46], int>(std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#17 0x45b619 in std::shared_ptrfingerprint::FingerprintGeneratorPool::shared_ptr<std::allocator, int, char const (&) [46], int>(std::_Sp_alloc_shared_tag<std::allocator >, int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#18 0x45b619 in std::shared_ptrfingerprint::FingerprintGeneratorPool std::make_shared<fingerprint::FingerprintGeneratorPool, int, char const (&) [46], int>(int&&, char const (&) [46], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#19 0x445036 in __static_initialization_and_destruction_0 ../libs/dlp/fp20svc/test/src/DlpFp20Test.cpp:35
#20 0x6ccafc in __libc_csu_init (/home/wxin/src/ns/dataplane/obj-asan/dlp_fp20_lib_test+0x6ccafc)

@ns-wxin
Copy link

ns-wxin commented Feb 1, 2023

Sorry, the un-necessary release() on session caused most of the leak. Now that is fixed, I'm only seeing the leaking when using extensions. Specifically on CustomDomain related code.
The code looks like the following:

`TextMLFingerprintGenerator::TextMLFingerprintGenerator(std::string modelFile, int threadNum)
: m_modelFile(std::move(modelFile)), m_modelThreads(threadNum)
{
m_modelSession = loadModel(m_modelFile);
m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
OrtMemType::OrtMemTypeDefault);

// setting input name for the model
m_inputName = TEXT_ML_MODEL_INPUT_NAME.c_str();

// setting input dimensions
m_inputDims = {1};

// setting output names
m_outputName = TEXT_ML_MODEL_OUTPUT_NAME.c_str();
m_outputDims = {1, TEXT_ML_EMBEDDING_LENGTH};

}

std::shared_ptrOrt::Session
TextMLFingerprintGenerator::loadModel(const std::string &modelFile)
{
DLOG(1) << "Loading model: " << modelFile;

m_sessionOptions.SetIntraOpNumThreads(m_modelThreads);
m_sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
m_sessionOptions.DisableCpuMemArena();

m_sessionOptions.EnableOrtCustomOps();
std::shared_ptr<Ort::Session> session =
    std::make_shared<Ort::Session>(m_env, modelFile.c_str(), m_sessionOptions);

LOG(INFO) << "Loaded model: " << modelFile;
return session;

}`

==1926430==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 56 byte(s) in 1 object(s) allocated from:
#0 0x7fbfdb814758 in operator new(unsigned long) ../../../../libsanitizer/asan/asan_new_delete.cpp:95
#1 0x7fbfcccd7d42 in OrtApis::CreateCustomOpDomain(char const*, OrtCustomOpDomain**) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x624d42)
#2 0x7fbfcf3cbb16 in RegisterCustomOps (/opt/3p/lib/libonnxruntime.so.1.12.1+0x2d18b16)
#3 0x7fbfccce24f0 in OrtApis::EnableOrtCustomOps(OrtSessionOptions*) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x62f4f0)
#4 0x605318 in Ort::SessionOptions::EnableOrtCustomOps() /opt/3p/include/onnxruntime/core/session/onnxruntime_cxx_inline.h:452
#5 0x605318 in fingerprint::TextMLFingerprintGenerator::loadModel(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) ../libs/dlp/fp20svc/src/TextMLFingerprintGenerator.cpp:88
#6 0x606d9a in fingerprint::TextMLFingerprintGenerator::TextMLFingerprintGenerator(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int) ../libs/dlp/fp20svc/src/TextMLFingerprintGenerator.cpp:64
#7 0x458605 in void std::_Construct<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(fingerprint::TextMLFingerprintGenerator*, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#8 0x458605 in void std::allocator_traits<std::allocator >::construct<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(std::allocator&, fingerprint::TextMLFingerprintGenerator*, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#9 0x458605 in std::_Sp_counted_ptr_inplace<fingerprint::TextMLFingerprintGenerator, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<char const (&) [54], int>(std::allocator, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#10 0x458605 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::TextMLFingerprintGenerator, std::allocator, char const (&) [54], int>(fingerprint::TextMLFingerprintGenerator*&, std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#11 0x458605 in std::__shared_ptr<fingerprint::TextMLFingerprintGenerator, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, char const (&) [54], int>(std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#12 0x458605 in std::shared_ptrfingerprint::TextMLFingerprintGenerator::shared_ptr<std::allocator, char const (&) [54], int>(std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#13 0x458605 in std::shared_ptrfingerprint::TextMLFingerprintGenerator std::make_shared<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#14 0x44506d in __static_initialization_and_destruction_0 ../libs/dlp/fp20svc/test/src/DlpFp20Test.cpp:39
#15 0x6cc3dc in __libc_csu_init (/home/wxin/src/ns/dataplane/obj-asan/dlp_fp20_lib_test+0x6cc3dc)

and

Indirect leak of 256 byte(s) in 1 object(s) allocated from:
#0 0x7fbfdb814758 in operator new(unsigned long) ../../../../libsanitizer/asan/asan_new_delete.cpp:95
#1 0x7fbfcccfa1b2 in void std::vector<OrtCustomOp const*, std::allocator<OrtCustomOp const*> >::_M_realloc_insert<OrtCustomOp const*&>(__gnu_cxx::__normal_iterator<OrtCustomOp const**, std::vector<OrtCustomOp const*, std::allocator<OrtCustomOp const*> > >, OrtCustomOp const*&) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x6471b2)
#2 0x7fbfcccfa72c in OrtApis::CustomOpDomain_Add(OrtCustomOpDomain*, OrtCustomOp const*) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x64772c)
#3 0x7fbfcf3cc48f in RegisterCustomOps (/opt/3p/lib/libonnxruntime.so.1.12.1+0x2d1948f)
#4 0x7fbfccce24f0 in OrtApis::EnableOrtCustomOps(OrtSessionOptions*) (/opt/3p/lib/libonnxruntime.so.1.12.1+0x62f4f0)
#5 0x605318 in Ort::SessionOptions::EnableOrtCustomOps() /opt/3p/include/onnxruntime/core/session/onnxruntime_cxx_inline.h:452
#6 0x605318 in fingerprint::TextMLFingerprintGenerator::loadModel(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) ../libs/dlp/fp20svc/src/TextMLFingerprintGenerator.cpp:88
#7 0x606d9a in fingerprint::TextMLFingerprintGenerator::TextMLFingerprintGenerator(std::__cxx11::basic_string<char, std::char_traits, std::allocator >, int) ../libs/dlp/fp20svc/src/TextMLFingerprintGenerator.cpp:64
#8 0x458605 in void std::_Construct<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(fingerprint::TextMLFingerprintGenerator*, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/stl_construct.h:119
#9 0x458605 in void std::allocator_traits<std::allocator >::construct<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(std::allocator&, fingerprint::TextMLFingerprintGenerator*, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/alloc_traits.h:635
#10 0x458605 in std::_Sp_counted_ptr_inplace<fingerprint::TextMLFingerprintGenerator, std::allocator, (__gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<char const (&) [54], int>(std::allocator, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:604
#11 0x458605 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count<fingerprint::TextMLFingerprintGenerator, std::allocator, char const (&) [54], int>(fingerprint::TextMLFingerprintGenerator*&, std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:971
#12 0x458605 in std::__shared_ptr<fingerprint::TextMLFingerprintGenerator, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<std::allocator, char const (&) [54], int>(std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr_base.h:1712
#13 0x458605 in std::shared_ptrfingerprint::TextMLFingerprintGenerator::shared_ptr<std::allocator, char const (&) [54], int>(std::_Sp_alloc_shared_tag<std::allocator >, char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:464
#14 0x458605 in std::shared_ptrfingerprint::TextMLFingerprintGenerator std::make_shared<fingerprint::TextMLFingerprintGenerator, char const (&) [54], int>(char const (&) [54], int&&) /opt/gcc-12/include/c++/12.2.0/bits/shared_ptr.h:1010
#15 0x44506d in __static_initialization_and_destruction_0 ../libs/dlp/fp20svc/test/src/DlpFp20Test.cpp:39
#16 0x6cc3dc in __libc_csu_init (/home/wxin/src/ns/dataplane/obj-asan/dlp_fp20_lib_test+0x6cc3dc)

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

No branches or pull requests

4 participants