Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] On Linux try return not used memory to system on compile model destructor (#27534)

Pawel Raaszf4170977048d
diff --git a/src/inference/src/cpp/compiled_model.cpp b/src/inference/src/cpp/compiled_model.cpp
index c780bbee1e9..d675cba4714 100644
--- a/src/inference/src/cpp/compiled_model.cpp
+++ b/src/inference/src/cpp/compiled_model.cpp
@@ -8,6 +8,10 @@
#include "openvino/runtime/icompiled_model.hpp"
#include "openvino/runtime/properties.hpp"
+#if defined(OPENVINO_GNU_LIBC) && !defined(__ANDROID__)
+# include <malloc.h>
+#endif
+
#define OV_COMPILED_MODEL_CALL_STATEMENT(...) \
if (_impl == nullptr) \
OPENVINO_THROW("CompiledModel was not initialized."); \
@@ -23,6 +27,12 @@ namespace ov {
CompiledModel::~CompiledModel() {
_impl = {};
+#if defined(OPENVINO_GNU_LIBC) && !defined(__ANDROID__)
+ // Linux memory margent doesn't return system memory immediate after release.
+ // It depends on memory chunk size and allocation history.
+ // Try return memory from a process to system now to reduce memory usage and not wait to the end of the process.
+ malloc_trim(0);
+#endif
}
CompiledModel::CompiledModel(const std::shared_ptr<ov::ICompiledModel>& impl, const std::shared_ptr<void>& so)