// Copyright 2013 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // MemoryPressure provides static APIs for handling memory pressure on // platforms that have such signals, such as Android and ChromeOS. // The app will try to discard buffers that aren't deemed essential (individual // modules will implement their own policy). #ifndef BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ #define BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_ #include "base/base_export.h" #include "base/functional/callback.h" #include "base/location.h" #include "base/tracing_buildflags.h" namespace base { // To start listening, create a new instance, passing a callback to a // function that takes a MemoryPressureLevel parameter. To stop listening, // simply delete the listener object. The implementation guarantees // that the callback will always be called on the thread that created // the listener. // Note that even on the same thread, the callback is not guaranteed to be // called synchronously within the system memory pressure broadcast. // Please see notes in MemoryPressureLevel enum below: some levels are // absolutely critical, and if not enough memory is returned to the system, // it'll potentially kill the app, and then later the app will have to be // cold-started. // // Example: // // void OnMemoryPressure(MemoryPressureLevel memory_pressure_level) { // ... // } // // // Start listening. // auto listener = std::make_unique<MemoryPressureListener>( // base::BindRepeating(&OnMemoryPressure)); // // ... // // // Stop listening. // listener.reset(); // class BASE_EXPORT MemoryPressureListener { … }; } // namespace base #endif // BASE_MEMORY_MEMORY_PRESSURE_LISTENER_H_