MemoryBudgetManager


public final class MemoryBudgetManager
extends Object

java.lang.Object
   ↳ android.app.MemoryBudgetManager


This class is the client interface to memory budgets for applications and processes. A memory budget is a promise by the application or one of its processes that its memory requirements will not exceed some limit. The budget is not a hard limit: it can be exceeded but that will likely cause processes to slow down as they try to release memory and drop below the budget limit.

A budget declaration is not a request for memory from the system, nor does the declaration influence how the system allocates memory to a process. Budgets allow an application to monitor its own resource use.

The memory usage tracked against these budgets includes:

  • Anonymous allocations, most commonly the Java heap and native heap.
  • File-backed pages (such as files mapped into memory or cached by the process).
In the case of memory shared between processes (e.g., shared memory segments), the usage is charged to the first process that maps or allocates it.

The tracked memory usage does not include native graphics buffers or DMA-BUFs.

There are two sets of limits on memory, in addition to the budgets managed through this interface. There are system limits and manifest budgets (declared via <memory-budget> in the manifest). Budgets managed through this interface may not exceed the system or manifest limits. Any attempt to exceed the system or manifest limits will result in an IllegalArgumentException. However, the budget set through this interface can be changed at any time, up or down, so long as the new value does not exceed the system or manifest limits.

A budget must be positive; attempting to set a budget that is zero or negative will result in an IllegalArgumentException.

A package (application) budget applies to all processes in the application as a group (specifically, all processes running under the application's UID). If the application shares a UID with other applications, the budget applies to the aggregate usage of all processes running under that shared UID.

A process budget applies to a single process. It is possible to set a large budget on a process and a small budget on the package; the package budget dominates and the large process budget is irrelevant. This is not an error.

The primary interfaces provided by this class are:

Summary

Nested classes

interface MemoryBudgetManager.OnOverBudgetListener

The callback that is notified when a budget is exceeded. 

Constants

long LIMIT_IS_DISABLED

This value is returned by getPackageBudgetBytes() and getProcessBudgetBytes() if there is no memory limit associated with the application or process, respectively.

Public methods

void clearPackageBudget()

Removes the package budget, if any, for the caller's application.

void clearProcessBudget()

Removes the process budget, if any, for the caller's process.

long getPackageBudgetBytes()

Returns the budget for the caller's application.

long getPackageCurrentUsageBytes()

Returns the aggregate memory usage across all processes running under the caller's application UID.

long getProcessBudgetBytes()

Returns the budget for the caller's process.

long getProcessCurrentUsageBytes()

Returns the current memory usage for the caller's process.

void registerPackageOverBudgetListener(Looper looper, MemoryBudgetManager.OnOverBudgetListener listener)

Registers to be notified if the caller's application exceeds its package budget.

void registerProcessOverBudgetListener(Looper looper, MemoryBudgetManager.OnOverBudgetListener listener)

Registers to be notified if the caller's process exceeds its process budget.

void setPackageBudgetBytes(long budgetBytes)

Sets the memory budget for the caller's application.

void setProcessBudgetBytes(long budgetBytes)

Sets the memory budget for the caller's process.

void unregisterPackageOverBudgetListener(MemoryBudgetManager.OnOverBudgetListener listener)

Unregisters a package budget listener.

void unregisterProcessOverBudgetListener(MemoryBudgetManager.OnOverBudgetListener listener)

Unregisters a process budget listener.

Inherited methods

Constants

LIMIT_IS_DISABLED

Added in version 37.2
public static final long LIMIT_IS_DISABLED

This value is returned by getPackageBudgetBytes() and getProcessBudgetBytes() if there is no memory limit associated with the application or process, respectively. This value may not be passed to a function that sets a budget: use the clear functions instead.

Constant Value: -1 (0xffffffffffffffff)

Public methods

clearPackageBudget

Added in version 37.2
public void clearPackageBudget ()

Removes the package budget, if any, for the caller's application. Any manifest budget or system limit that was masked by the package budget is resumed.

Throws
SecurityException if the caller is not allowed to set budgets, such as from an isolated process

clearProcessBudget

Added in version 37.2
public void clearProcessBudget ()

Removes the process budget, if any, for the caller's process. Any manifest budget or system limit that was masked by the process budget is resumed.

Throws
SecurityException if the caller is not allowed to set budgets, such as from an isolated process

getPackageBudgetBytes

Added in version 37.2
public long getPackageBudgetBytes ()

Returns the budget for the caller's application. The value returned will be the last value set via setPackageBudgetBytes(long), if any, or the existing system or manifest limit, if there is no package budget.

Returns
long memory limit for the caller's application in bytes; may return LIMIT_IS_DISABLED

getPackageCurrentUsageBytes

Added in version 37.2
public long getPackageCurrentUsageBytes ()

Returns the aggregate memory usage across all processes running under the caller's application UID.

Returns
long the current memory usage in bytes.

Throws
NumberFormatException if there is an error parsing the current usage value from the underlying memory tracking system.
SecurityException if the caller is an isolated process or access to the underlying memory tracking system is unavailable.
UnsupportedOperationException if memory budgets are not supported.

getProcessBudgetBytes

Added in version 37.2
public long getProcessBudgetBytes ()

Returns the budget for the caller's process. The value returned will be the last value set via setProcessBudgetBytes(long), if any, or the existing system or manifest limit, if there is no process budget.

Returns
long memory limit for the caller's process in bytes; may return LIMIT_IS_DISABLED

getProcessCurrentUsageBytes

Added in version 37.2
public long getProcessCurrentUsageBytes ()

Returns the current memory usage for the caller's process.

Returns
long the current memory usage in bytes.

Throws
NumberFormatException if there is an error parsing the current usage value from the underlying memory tracking system.
SecurityException if access to the underlying memory tracking system is unavailable.
UnsupportedOperationException if memory budgets are not supported.

registerPackageOverBudgetListener

Added in version 37.2
public void registerPackageOverBudgetListener (Looper looper, 
                MemoryBudgetManager.OnOverBudgetListener listener)

Registers to be notified if the caller's application exceeds its package budget. The package budget is based on the aggregate memory usage of all processes running under the same UID. An over-budget event can be triggered by memory allocations in any of these processes.

An OnOverBudgetListener callback can only be registered once.

Parameters
looper Looper: OnOverBudgetListener callbacks will be dispatched on this Looper's thread. If null, the looper from Looper.getMainLooper() will be used.

listener MemoryBudgetManager.OnOverBudgetListener: callback to be notified of an over-budget event.
This value cannot be null.

Throws
IllegalArgumentException if a looper is not provided and one can't be found on the current thread.
IllegalStateException if registration fails
SecurityException if the caller is an isolated process.

registerProcessOverBudgetListener

Added in version 37.2
public void registerProcessOverBudgetListener (Looper looper, 
                MemoryBudgetManager.OnOverBudgetListener listener)

Registers to be notified if the caller's process exceeds its process budget. This listener will only be notified of breaches due to memory allocations within the calling process.

An OnOverBudgetListener callback can only be registered once.

Parameters
looper Looper: OnOverBudgetListener callbacks will be dispatched on this Looper's thread. If null, the looper from Looper.getMainLooper() will be used.

listener MemoryBudgetManager.OnOverBudgetListener: callback to be notified of an over-budget event.
This value cannot be null.

Throws
IllegalArgumentException if a looper is not provided and one can't be found on the current thread.
IllegalStateException if registration fails

setPackageBudgetBytes

Added in version 37.2
public void setPackageBudgetBytes (long budgetBytes)

Sets the memory budget for the caller's application. The budget applies to the cumulative memory use by all processes within the application. This does not affect any budgets for processes within the application.

Parameters
budgetBytes long: declared budget, in bytes.
Value is 0 or greater

Throws
IllegalArgumentException if the budget is invalid.
SecurityException if the caller is not allowed to set budgets, such as from an isolated process

setProcessBudgetBytes

Added in version 37.2
public void setProcessBudgetBytes (long budgetBytes)

Sets the memory budget for the caller's process.

Parameters
budgetBytes long: declared budget, in bytes.
Value is 0 or greater

Throws
IllegalArgumentException if the budget is invalid.
SecurityException if the caller is not allowed to set budgets, such as from an isolated process

unregisterPackageOverBudgetListener

Added in version 37.2
public void unregisterPackageOverBudgetListener (MemoryBudgetManager.OnOverBudgetListener listener)

Unregisters a package budget listener.

Parameters
listener MemoryBudgetManager.OnOverBudgetListener: The callback to be notified of an over-budget event.
This value cannot be null.

Throws
IllegalStateException if the listener was not registered.

unregisterProcessOverBudgetListener

Added in version 37.2
public void unregisterProcessOverBudgetListener (MemoryBudgetManager.OnOverBudgetListener listener)

Unregisters a process budget listener.

Parameters
listener MemoryBudgetManager.OnOverBudgetListener: The callback to be notified of an over-budget event.
This value cannot be null.

Throws
IllegalStateException if the listener was not registered.