MemoryBudgetManager
class MemoryBudgetManager
| kotlin.Any | |
| ↳ | 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.
There are two sets of limits on memory, in addition to the budgets managed through this interface. There are system limits and manifest budgets. 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. 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:
- Memory Budget Configuration:
setPackageBudgetBytes(long)/clearPackageBudget()- Sets or clears the memory budget for the application.setProcessBudgetBytes(long)/clearProcessBudget()- Sets or clears the memory budget for the calling process.
- Memory Budget Queries:
getPackageBudgetBytes()- Returns the current budget for the application.getProcessBudgetBytes()- Returns the current budget for the calling process.
- Memory Usage Queries:
getPackageCurrentUsageBytes()- Returns the aggregate memory usage across all processes in the application package.getProcessCurrentUsageBytes()- Returns the memory usage for the caller's specific process.
- Over-Budget Event Monitoring:
OnOverBudgetListener- Callback interface notified when memory usage exceeds a budget threshold.registerPackageOverBudgetListener(Looper,OnOverBudgetListener)/unregisterPackageOverBudgetListener(OnOverBudgetListener)- Registers or unregisters listeners for package-level budget breaches.registerProcessOverBudgetListener(Looper,OnOverBudgetListener)/unregisterProcessOverBudgetListener(OnOverBudgetListener)- Registers or unregisters listeners for process-level budget breaches.
Summary
| Nested classes | |
|---|---|
| abstract |
The callback that is notified when a budget is exceeded. |
| Constants | |
|---|---|
| static Long |
This value is returned by |
| Public methods | |
|---|---|
| Unit |
Removes the package budget, if any, for the caller's application. |
| Unit |
Removes the process budget, if any, for the caller's process. |
| Long |
Returns the budget for the caller's application. |
| Long |
Returns the current memory usage for the caller's application. |
| Long |
Returns the budget for the caller's process. |
| Long |
Returns the current memory usage for the caller's process. |
| Unit |
registerPackageOverBudgetListener(looper: Looper?, listener: MemoryBudgetManager.OnOverBudgetListener)Registers to be notified if the caller's application exceeds its package budget. |
| Unit |
registerProcessOverBudgetListener(looper: Looper?, listener: MemoryBudgetManager.OnOverBudgetListener)Registers to be notified if the caller's process exceeds its process budget. |
| Unit |
setPackageBudgetBytes(budgetBytes: Long)Sets the memory budget for the caller's application. |
| Unit |
setProcessBudgetBytes(budgetBytes: Long)Sets the memory budget for the caller's process. |
| Unit |
Unregisters a package budget listener. |
| Unit |
Unregisters a process budget listener. |
Constants
LIMIT_IS_DISABLED
static val LIMIT_IS_DISABLED: Long
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.
Value: -1LPublic methods
clearPackageBudget
fun clearPackageBudget(): Unit
Removes the package budget, if any, for the caller's application.
| Exceptions | |
|---|---|
java.lang.SecurityException |
if the caller is not allowed to set budgets, such as from an isolated process |
clearProcessBudget
fun clearProcessBudget(): Unit
Removes the process budget, if any, for the caller's process.
| Exceptions | |
|---|---|
java.lang.SecurityException |
if the caller is not allowed to set budgets, such as from an isolated process |
getPackageBudgetBytes
fun getPackageBudgetBytes(): Long
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.
| Return | |
|---|---|
Long |
memory limit for the caller's application in bytes; may return LIMIT_IS_DISABLED |
getPackageCurrentUsageBytes
fun getPackageCurrentUsageBytes(): Long
Returns the current memory usage for the caller's application.
| Return | |
|---|---|
Long |
the current memory usage in bytes. |
| Exceptions | |
|---|---|
java.lang.NumberFormatException |
if there is an error parsing the current usage value from the underlying memory cgroup file. |
java.lang.SecurityException |
if the caller is an isolated process or access to the underlying memory cgroup is unavailable. |
java.lang.UnsupportedOperationException |
if memory budgets are not supported. |
getProcessBudgetBytes
fun getProcessBudgetBytes(): Long
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.
| Return | |
|---|---|
Long |
memory limit for the caller's process in bytes; may return LIMIT_IS_DISABLED |
getProcessCurrentUsageBytes
fun getProcessCurrentUsageBytes(): Long
Returns the current memory usage for the caller's process.
| Return | |
|---|---|
Long |
the current memory usage in bytes. |
| Exceptions | |
|---|---|
java.lang.NumberFormatException |
if there is an error parsing the current usage value from the underlying memory cgroup file. |
java.lang.SecurityException |
if access to the underlying memory cgroup is unavailable. |
java.lang.UnsupportedOperationException |
if memory budgets are not supported. |
registerPackageOverBudgetListener
fun registerPackageOverBudgetListener(
looper: Looper?,
listener: MemoryBudgetManager.OnOverBudgetListener
): Unit
Registers to be notified if the caller's application exceeds its package budget. 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. |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if a looper is not provided and one can't be found on the current thread. |
java.lang.IllegalStateException |
if registration fails |
java.lang.SecurityException |
if the caller is an isolated process. |
registerProcessOverBudgetListener
fun registerProcessOverBudgetListener(
looper: Looper?,
listener: MemoryBudgetManager.OnOverBudgetListener
): Unit
Registers to be notified if the caller's process exceeds its process budget. 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. |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if a looper is not provided and one can't be found on the current thread. |
java.lang.IllegalStateException |
if registration fails |
setPackageBudgetBytes
fun setPackageBudgetBytes(budgetBytes: Long): Unit
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 |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if the budget is invalid. |
java.lang.SecurityException |
if the caller is not allowed to set budgets, such as from an isolated process |
setProcessBudgetBytes
fun setProcessBudgetBytes(budgetBytes: Long): Unit
Sets the memory budget for the caller's process.
| Parameters | |
|---|---|
budgetBytes |
Long: declared budget, in bytes. Value is 0 or greater |
| Exceptions | |
|---|---|
java.lang.IllegalArgumentException |
if the budget is invalid. |
java.lang.SecurityException |
if the caller is not allowed to set budgets, such as from an isolated process |
unregisterPackageOverBudgetListener
fun unregisterPackageOverBudgetListener(listener: MemoryBudgetManager.OnOverBudgetListener): Unit
Unregisters a package budget listener.
| Parameters | |
|---|---|
listener |
MemoryBudgetManager.OnOverBudgetListener: The callback to be notified of an over-budget event. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.IllegalStateException |
if the listener was not registered. |
unregisterProcessOverBudgetListener
fun unregisterProcessOverBudgetListener(listener: MemoryBudgetManager.OnOverBudgetListener): Unit
Unregisters a process budget listener.
| Parameters | |
|---|---|
listener |
MemoryBudgetManager.OnOverBudgetListener: The callback to be notified of an over-budget event. This value cannot be null. |
| Exceptions | |
|---|---|
java.lang.IllegalStateException |
if the listener was not registered. |