Lines
100 %
Functions
#include "source/common/memory/utils.h"
#include "source/common/common/assert.h"
#include "source/common/memory/stats.h"
#if defined(TCMALLOC)
#include "tcmalloc/malloc_extension.h"
#elif defined(GPERFTOOLS_TCMALLOC)
#include "gperftools/malloc_extension.h"
#endif
namespace Envoy {
namespace Memory {
void Utils::releaseFreeMemory() {
tcmalloc::MallocExtension::ReleaseMemoryToSystem(maxUnfreedMemoryBytes());
MallocExtension::instance()->ReleaseFreeMemory();
}
/*
The purpose of this function is to release the cache introduced by tcmalloc,
mainly in xDS config updates, admin handler, and so on. all work on the main thread,
so the overall impact on performance is small.
Ref: https://github.com/envoyproxy/envoy/pull/9471#discussion_r363825985
*/
void Utils::tryShrinkHeap() {
#if defined(TCMALLOC) || defined(GPERFTOOLS_TCMALLOC)
auto total_physical_bytes = Stats::totalPhysicalBytes();
auto allocated_size_by_app = Stats::totalCurrentlyAllocated();
const uint64_t threshold = maxUnfreedMemoryBytes();
if (total_physical_bytes >= allocated_size_by_app &&
(total_physical_bytes - allocated_size_by_app) >= threshold) {
Utils::releaseFreeMemory();
} // namespace Memory
} // namespace Envoy