MemoryBudget


public final class MemoryBudget
extends Object implements Parcelable

java.lang.Object
   ↳ android.content.pm.MemoryBudget


Memory budget information for an application. MemoryBudget entries are retrieved from <memory-budget> entries in an application's manifest, and can be obtained through ERROR(/ApplicationInfo.getMemoryBudgets).

A MemoryBudget that is declared in the Android manifest sets a ceiling on the memory available to the package or to a process within the package. MemoryBudget declarations may appear at the application level and at the process level. The two are independent and neither overrides the other. In practice, however, the smaller budget dominates.

Every MemoryBudget has a state and a feature; these two attributes determine if and when the MemoryBudget is applicable.

  • The state is the operational state of the process or the application, and is one of Foreground, Perceptible, or Background.
  • The feature selects for device form-factor and is one of Unspecified, TV, Auto, and Watch.
A MemoryBudget is applicable to a process if its feature is Unspecified or if its feature matches the device's form-factor. A MemoryBudget applies to a process if it is applicable to the process and the MemoryBudget state is less than or equal to the operational state of the process. Applications also have state, which is roughly the lowest state of any process running in the application.

A MemoryBudget declares an upper bound on the amount of memory the process or application may use. The upper bound is computed from three factors:

  • maxMb is a baseline amount of memory, expressed in MiB. This must be positive.
  • additionalBytesPerPixel is a scaling factor: bytes per display pixel. This must be non-negative and defaults to zero.
  • additionalMbPerDensity is a scaling factor: MiB times display density. This must be non-negative and defaults to zero.
The actual limit is calculated as the maxMb value plus the scale factors applied to the default display of the device.

If a process RSS exceeds its declared budget then the kernel will force it to drop memory. This usually means dropping file cache pages but it may also mean that some memory will be moved to swap. In most cases this happens without any impact on the process, but if the process is actively using more memory than the budget limit, the process will slow down as pages are swapped out and then back in. In the limit, the process may ANR.

Summary

Constants

int FEATURE_AUTOMOTIVE

Indicates that this MemoryBudget applies to the automotive form factor.

int FEATURE_LEANBACK

Indicates that this MemoryBudget applies to the TV form factor.

int FEATURE_UNSPECIFIED

Indicates that this MemoryBudget is generic, and can apply to any form factor.

int FEATURE_WATCH

Indicates that this MemoryBudget applies to the watch form factor.

int STATE_BACKGROUND

Indicates that this MemoryBudget can apply to a process that is in the background.

int STATE_FOREGROUND

Indicates that this MemoryBudget can apply to a process that is in the foreground (or in a less important state).

int STATE_PERCEPTIBLE

Indicates that this MemoryBudget can apply to a process that is perceptible (or in a less important state).

Inherited constants

Fields

public static final Creator<MemoryBudget> CREATOR

Public methods

int describeContents()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation.

boolean equals(Object o)

Indicates whether some other object is "equal to" this one.

long getAdditionalBytesPerDisplayPixel()

Returns the additional bytes per pixel that this MemoryBudget's limit will be raised by.

long getAdditionalMbPerDensity()

Returns the additional memory (in MiB) that this MemoryBudget's limit will be raised by, based on the screen density of the device.

int getFeature()

Returns the form factor to which this MemoryBudget applies.

long getMaxMb()

Returns the memory limit (in MiB) that this MemoryBudget represents.

int getState()

Returns the process state in which this MemoryBudget applies.

int hashCode()

Returns a hash code value for the object.

String toString()

Returns a string representation of the object.

void writeToParcel(Parcel dest, int flags)

Flatten this object in to a Parcel.

Inherited methods

Constants

FEATURE_AUTOMOTIVE

Added in API level 10000
public static final int FEATURE_AUTOMOTIVE

Indicates that this MemoryBudget applies to the automotive form factor.

Constant Value: 1 (0x00000001)

FEATURE_LEANBACK

Added in API level 10000
public static final int FEATURE_LEANBACK

Indicates that this MemoryBudget applies to the TV form factor.

Constant Value: 2 (0x00000002)

FEATURE_UNSPECIFIED

Added in API level 10000
public static final int FEATURE_UNSPECIFIED

Indicates that this MemoryBudget is generic, and can apply to any form factor. This is the default if "feature" is not specified in the manifest.

Constant Value: 0 (0x00000000)

FEATURE_WATCH

Added in API level 10000
public static final int FEATURE_WATCH

Indicates that this MemoryBudget applies to the watch form factor.

Constant Value: 3 (0x00000003)

STATE_BACKGROUND

Added in API level 10000
public static final int STATE_BACKGROUND

Indicates that this MemoryBudget can apply to a process that is in the background.

Constant Value: 300 (0x0000012c)

STATE_FOREGROUND

Added in API level 10000
public static final int STATE_FOREGROUND

Indicates that this MemoryBudget can apply to a process that is in the foreground (or in a less important state). This is the default if "state" is not specified in the manifest.

Constant Value: 100 (0x00000064)

STATE_PERCEPTIBLE

Added in API level 10000
public static final int STATE_PERCEPTIBLE

Indicates that this MemoryBudget can apply to a process that is perceptible (or in a less important state).

Constant Value: 200 (0x000000c8)

Fields

CREATOR

Added in API level 10000
public static final Creator<MemoryBudget> CREATOR

Public methods

describeContents

Added in API level 10000
public int describeContents ()

Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of writeToParcel(Parcel,int), the return value of this method must include the CONTENTS_FILE_DESCRIPTOR bit.

Returns
int a bitmask indicating the set of special object types marshaled by this Parcelable object instance.
Value is either 0 or

equals

Added in API level 10000
public boolean equals (Object o)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o Object: the reference object with which to compare.

Returns
boolean true if this object is the same as the obj argument; false otherwise.

getAdditionalBytesPerDisplayPixel

Added in API level 10000
public long getAdditionalBytesPerDisplayPixel ()

Returns the additional bytes per pixel that this MemoryBudget's limit will be raised by.

Returns
long The additionalBytesPerDisplayPixel attribute.

getAdditionalMbPerDensity

Added in API level 10000
public long getAdditionalMbPerDensity ()

Returns the additional memory (in MiB) that this MemoryBudget's limit will be raised by, based on the screen density of the device. The formula used for calculating the additional memory is (N * { ldpi=0.75, mdpi=1.0, hdpi=1.5, xhdpi=2... }).

Returns
long The additionalMbPerDensity attribute.

getFeature

Added in API level 10000
public int getFeature ()

Returns the form factor to which this MemoryBudget applies.

Returns
int The form factor to which this MemoryBudget applies.
Value is one of the following:

getMaxMb

Added in API level 10000
public long getMaxMb ()

Returns the memory limit (in MiB) that this MemoryBudget represents.

Returns
long The memory limit (in MiB) that this MemoryBudget represents.

getState

Added in API level 10000
public int getState ()

Returns the process state in which this MemoryBudget applies.

Returns
int The process state in which this MemoryBudget applies.
Value is one of the following:

hashCode

Added in API level 10000
public int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Returns
int a hash code value for this object.

toString

Added in API level 10000
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

writeToParcel

Added in API level 10000
public void writeToParcel (Parcel dest, 
                int flags)

Flatten this object in to a Parcel.

Parameters
dest Parcel: This value cannot be null.

flags int: Additional flags about how the object should be written. May be 0 or Parcelable.PARCELABLE_WRITE_RETURN_VALUE.
Value is either 0 or a combination of the following: