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

Call malloc_trim(0) every 5m to release unused heap memory back to the OS #9998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/icinga/icingaapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@
#include "base/loader.hpp"
#include <fstream>

#ifdef __linux__
# include <malloc.h>
#endif /* __linux__ */

using namespace icinga;

static Timer::Ptr l_RetentionTimer;

#ifdef __linux__
static Timer::Ptr l_MallocTrimTimer;
#endif /* __linux__ */

REGISTER_TYPE(IcingaApplication);
/* Ensure that the priority is lower than the basic System namespace initialization in scriptframe.cpp. */
INITIALIZE_ONCE_WITH_PRIORITY(&IcingaApplication::StaticInitialize, InitializePriority::InitIcingaApplication);
Expand Down Expand Up @@ -109,6 +117,18 @@ int IcingaApplication::Main()
l_RetentionTimer->OnTimerExpired.connect([this](const Timer * const&) { DumpProgramState(); });
l_RetentionTimer->Start();

#ifdef __linux__
l_MallocTrimTimer = Timer::Create();
l_MallocTrimTimer->SetInterval(300);

l_MallocTrimTimer->OnTimerExpired.connect([this](const Timer * const&) {
Log(LogNotice, "IcingaApplication") << "Releasing heap memory via malloc_trim(0).";
Log(LogNotice, "IcingaApplication") << "malloc_trim(0) returned " << malloc_trim(0) << ".";
});

l_MallocTrimTimer->Start();
#endif /* __linux__ */

RunEventLoop();

Log(LogInformation, "IcingaApplication", "Icinga has shut down.");
Expand All @@ -121,6 +141,10 @@ void IcingaApplication::OnShutdown()
{
ObjectLock olock(this);
l_RetentionTimer->Stop();

#ifdef __linux__
l_MallocTrimTimer->Stop();
#endif /* __linux__ */
}

DumpProgramState();
Expand Down
Loading