blob: 804f0a6b8545a0f489a25a6aea5e4b463d024b2e [file] [log] [blame]
bashib87488b2016-09-15 11:44:32 +09001// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_
6#define BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_
7
8#include "base/base_export.h"
9
10namespace base {
11
bashi783e6172016-09-27 13:15:23 +090012// OVERVIEW:
13//
14// MemoryCoordinatorClient is an interface which a component can implement to
bashif6513322017-01-30 10:05:29 +090015// adjust "future allocation" and "existing allocation". For "future allocation"
16// it provides a callback to observe memory state changes, and for "existing
17// allocation" it provides a callback to purge memory.
18//
19// Unlike MemoryPressureListener, memory state changes are stateful. State
20// transitions are throttled to avoid thrashing; the exact throttling period is
21// platform dependent, but will be at least 5-10 seconds. When a state change
22// notification is dispatched, clients are expected to update their allocation
23// policies (e.g. setting cache limit) that persist for the duration of the
24// memory state. Note that clients aren't expected to free up memory on memory
25// state changes. Clients should wait for a separate purge request to free up
26// memory. Purging requests will be throttled as well.
bashi783e6172016-09-27 13:15:23 +090027
bashib87488b2016-09-15 11:44:32 +090028// MemoryState is an indicator that processes can use to guide their memory
bashif6513322017-01-30 10:05:29 +090029// allocation policies. For example, a process that receives the throttled
30// state can use that as as signal to decrease memory cache limits.
bashi6033d182016-11-08 13:13:55 +090031// NOTE: This enum is used to back an UMA histogram, and therefore should be
32// treated as append-only.
33enum class MemoryState : int {
bashib87488b2016-09-15 11:44:32 +090034 // The state is unknown.
35 UNKNOWN = -1,
36 // No memory constraints.
37 NORMAL = 0,
bashif6513322017-01-30 10:05:29 +090038 // Running and interactive but memory allocation should be throttled.
39 // Clients should set lower budget for any memory that is used as an
40 // optimization but that is not necessary for the process to run.
41 // (e.g. caches)
bashib87488b2016-09-15 11:44:32 +090042 THROTTLED = 1,
43 // Still resident in memory but core processing logic has been suspended.
bashif6513322017-01-30 10:05:29 +090044 // In most cases, OnPurgeMemory() will be called before entering this state.
bashib87488b2016-09-15 11:44:32 +090045 SUSPENDED = 2,
46};
47
bashi6033d182016-11-08 13:13:55 +090048const int kMemoryStateMax = static_cast<int>(MemoryState::SUSPENDED) + 1;
49
bashiae2ec392016-11-01 15:36:19 +090050// Returns a string representation of MemoryState.
51BASE_EXPORT const char* MemoryStateToString(MemoryState state);
52
bashib87488b2016-09-15 11:44:32 +090053// This is an interface for components which can respond to memory status
bashi783e6172016-09-27 13:15:23 +090054// changes. An initial state is NORMAL. See MemoryCoordinatorClientRegistry for
55// threading guarantees and ownership management.
bashib87488b2016-09-15 11:44:32 +090056class BASE_EXPORT MemoryCoordinatorClient {
57 public:
bashi783e6172016-09-27 13:15:23 +090058 // Called when memory state has changed. Any transition can occur except for
59 // UNKNOWN. General guidelines are:
60 // * NORMAL: Restore the default settings for memory allocation/usage if
61 // it has changed.
bashif6513322017-01-30 10:05:29 +090062 // * THROTTLED: Use smaller limits for future memory allocations. You don't
63 // need to take any action on existing allocations.
64 // * SUSPENDED: Use much smaller limits for future memory allocations. You
65 // don't need to take any action on existing allocations.
66 virtual void OnMemoryStateChange(MemoryState state) {}
bashice75df12016-09-29 12:40:21 +090067
bashif6513322017-01-30 10:05:29 +090068 // Called to purge memory.
69 // This callback should free up any memory that is used as an optimization, or
70 // any memory whose contents can be reproduced.
71 virtual void OnPurgeMemory() {}
72
73 protected:
Chris Watkins653cd662017-12-13 13:25:58 +090074 virtual ~MemoryCoordinatorClient() = default;
bashib87488b2016-09-15 11:44:32 +090075};
76
77} // namespace base
78
79#endif // BASE_MEMORY_MEMORY_COORDINATOR_CLIENT_H_