MemoryPressureHandlerWin.cpp [plain text]
#include "config.h"
#include "MemoryPressureHandler.h"
#include <WebCore/Timer.h>
#include <WebCore/Win32Handle.h>
#include <psapi.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
class CheckMemoryTimer : public TimerBase {
public:
CheckMemoryTimer(MemoryPressureHandler&);
private:
virtual void fired();
void handleMemoryLow();
MemoryPressureHandler& m_pressureHandler;
Win32Handle m_lowMemoryHandle;
};
CheckMemoryTimer::CheckMemoryTimer(MemoryPressureHandler& pressureHandler)
: m_pressureHandler(pressureHandler)
{
m_lowMemoryHandle = CreateMemoryResourceNotification(LowMemoryResourceNotification);
}
void CheckMemoryTimer::fired()
{
m_pressureHandler.setUnderMemoryPressure(false);
BOOL memoryLow;
if (QueryMemoryResourceNotification(m_lowMemoryHandle.get(), &memoryLow) && memoryLow) {
handleMemoryLow();
return;
}
#if CPU(X86)
PROCESS_MEMORY_COUNTERS_EX counters;
if (!GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&counters), sizeof(counters)))
return;
const int maxMemoryUsageBytes = 0.9 * 1024 * 1024 * 1024;
if (counters.PrivateUsage > maxMemoryUsageBytes)
handleMemoryLow();
#endif
}
void CheckMemoryTimer::handleMemoryLow()
{
m_pressureHandler.setUnderMemoryPressure(true);
m_pressureHandler.releaseMemory(Critical::Yes);
}
void MemoryPressureHandler::platformReleaseMemory(Critical)
{
}
static std::unique_ptr<CheckMemoryTimer>& memCheckTimer()
{
static NeverDestroyed<std::unique_ptr<CheckMemoryTimer>> memCheckTimer;
return memCheckTimer;
}
void MemoryPressureHandler::install()
{
m_installed = true;
memCheckTimer() = std::make_unique<CheckMemoryTimer>(*this);
memCheckTimer()->startRepeating(60.0);
}
void MemoryPressureHandler::uninstall()
{
if (!m_installed)
return;
memCheckTimer() = nullptr;
m_installed = false;
}
void MemoryPressureHandler::holdOff(unsigned seconds)
{
}
void MemoryPressureHandler::respondToMemoryPressure(Critical critical, Synchronous synchronous)
{
uninstall();
releaseMemory(critical, synchronous);
}
std::optional<MemoryPressureHandler::ReliefLogger::MemoryUsage> MemoryPressureHandler::ReliefLogger::platformMemoryUsage()
{
return std::nullopt;
}
}