18 #if defined(__GLIBC__)
22 #elif defined(__APPLE__)
23 #include <malloc/malloc.h>
28 constexpr uint64_t kMinTrimIntervalMs = 1000;
29 std::atomic<uint64_t> g_last_trim_ms{0};
30 std::atomic<bool> g_trim_in_progress{
false};
33 using namespace std::chrono;
34 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
41 const uint64_t now_ms = NowMs();
42 const uint64_t last_ms = g_last_trim_ms.load(std::memory_order_relaxed);
45 if (!force && now_ms - last_ms < kMinTrimIntervalMs)
49 bool expected =
false;
50 if (!g_trim_in_progress.compare_exchange_strong(expected,
true, std::memory_order_acq_rel))
53 bool did_trim =
false;
55 #if defined(__GLIBC__)
63 #elif defined(__APPLE__)
65 malloc_zone_t* zone = malloc_default_zone();
66 malloc_zone_pressure_relief(zone, 0);
74 g_last_trim_ms.store(now_ms, std::memory_order_relaxed);
76 g_trim_in_progress.store(
false, std::memory_order_release);
Cross-platform helper to encourage returning freed memory to the OS.
This namespace is the default namespace for all code in the openshot library.
bool TrimMemoryToOS(bool force) noexcept
Attempt to return unused heap memory to the operating system.