Memory Advice main interface

The main interface to use Memory Advice.

Summary

Enumerations

MemoryAdvice_ErrorCode{
  MEMORYADVICE_ERROR_OK = 0,
  MEMORYADVICE_ERROR_NOT_INITIALIZED = -1,
  MEMORYADVICE_ERROR_ALREADY_INITIALIZED = -2,
  MEMORYADVICE_ERROR_LOOKUP_TABLE_INVALID = -3,
  MEMORYADVICE_ERROR_ADVISOR_PARAMETERS_INVALID = -4,
  MEMORYADVICE_ERROR_WATCHER_NOT_FOUND = -5,
  MEMORYADVICE_ERROR_TFLITE_MODEL_INVALID = -6
}
enum
All the error codes that can be returned by MemoryAdvice functions.
MemoryAdvice_MemoryState{
  MEMORYADVICE_STATE_UNKNOWN = 0,
  MEMORYADVICE_STATE_OK = 1,
  MEMORYADVICE_STATE_APPROACHING_LIMIT = 2,
  MEMORYADVICE_STATE_CRITICAL = 3
}
enum
All possible memory states that can be reported by the library.

Typedefs

MemoryAdvice_ErrorCode typedef
All the error codes that can be returned by MemoryAdvice functions.
MemoryAdvice_MemoryState typedef
All possible memory states that can be reported by the library.
MemoryAdvice_WatcherCallback)(MemoryAdvice_MemoryState state, void *user_data) typedef
void(*

Functions

MemoryAdvice_getAvailableMemory()
int64_t
Calculates an estimate for the amount of memory that can safely be allocated, int bytes.
MemoryAdvice_getMemoryState()
Returns the current memory state.
MemoryAdvice_getPercentageAvailableMemory()
float
Calculates an estimate for the amount of memory that can safely be allocated, as a percentage of the total memory.
MemoryAdvice_getTotalMemory()
int64_t
Calculates the total memory available on the device, as reported by ActivityManager::getMemoryInfo()
MemoryAdvice_init(JNIEnv *env, jobject context)
Initialize the Memory Advice library.
MemoryAdvice_initWithParams(JNIEnv *env, jobject context, const char *params)
Initialize the Memory Advice library.
MemoryAdvice_registerWatcher(uint64_t intervalMillis, MemoryAdvice_WatcherCallback callback, void *user_data)
Registers a watcher that polls the Memory Advice library periodically, and invokes the watcher callback when the memory state goes critical.
MemoryAdvice_unregisterWatcher(MemoryAdvice_WatcherCallback callback)
Removes all watchers with the given callback that were previously registered using MemoryAdvice_registerWatcher.

Macros

MEMORY_ADVICE_BUGFIX_VERSION 0
MEMORY_ADVICE_MAJOR_VERSION 2
MEMORY_ADVICE_MINOR_VERSION 1
MEMORY_ADVICE_PACKED_VERSION ANDROID_GAMESDK_PACKED_VERSION(TUNINGFORK_MAJOR_VERSION, \ TUNINGFORK_MINOR_VERSION, \ TUNINGFORK_BUGFIX_VERSION)

Enumerations

MemoryAdvice_ErrorCode

 MemoryAdvice_ErrorCode

All the error codes that can be returned by MemoryAdvice functions.

Properties
MEMORYADVICE_ERROR_ADVISOR_PARAMETERS_INVALID

The provided advisor parameters was not a valid json object.

MEMORYADVICE_ERROR_ALREADY_INITIALIZED

MemoryAdvice_init was called more than once.

MEMORYADVICE_ERROR_LOOKUP_TABLE_INVALID

The provided lookup table was not a valid json object.

MEMORYADVICE_ERROR_NOT_INITIALIZED

A call was made before MemoryAdvice was initialized.

MEMORYADVICE_ERROR_OK

No error.

MEMORYADVICE_ERROR_TFLITE_MODEL_INVALID

A correct TFLite model was not provided.

MEMORYADVICE_ERROR_WATCHER_NOT_FOUND

UnregisterWatcher was called with an invalid callback.

MemoryAdvice_MemoryState

 MemoryAdvice_MemoryState

All possible memory states that can be reported by the library.

Properties
MEMORYADVICE_STATE_APPROACHING_LIMIT

The application should minimize memory allocation.

MEMORYADVICE_STATE_CRITICAL

The application should free memory as soon as possible, until the memory state changes.

MEMORYADVICE_STATE_OK

The application can safely allocate memory.

MEMORYADVICE_STATE_UNKNOWN

The memory state cannot be determined.

Typedefs

MemoryAdvice_ErrorCode

enum MemoryAdvice_ErrorCode MemoryAdvice_ErrorCode

All the error codes that can be returned by MemoryAdvice functions.

MemoryAdvice_MemoryState

enum MemoryAdvice_MemoryState MemoryAdvice_MemoryState

All possible memory states that can be reported by the library.

MemoryAdvice_WatcherCallback

void(* MemoryAdvice_WatcherCallback)(MemoryAdvice_MemoryState state, void *user_data)

Functions

MemoryAdvice_getAvailableMemory

int64_t MemoryAdvice_getAvailableMemory()

Calculates an estimate for the amount of memory that can safely be allocated, int bytes.

Details
Returns
A positive number with an estimate of the percentage memory available.
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED (a negative number) if Memory Advice was not yet initialized.

MemoryAdvice_getMemoryState

MemoryAdvice_MemoryState MemoryAdvice_getMemoryState()

Returns the current memory state.

Details
Parameters
state
a pointer to a MemoryAdvice_MemoryState, in which the memory state will be written
Returns
A MemoryAdvice_MemoryState, if successful,
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED (a negative number) if Memory Advice was not yet initialized.

MemoryAdvice_getPercentageAvailableMemory

float MemoryAdvice_getPercentageAvailableMemory()

Calculates an estimate for the amount of memory that can safely be allocated, as a percentage of the total memory.

Details
Returns
A positive number between 0 and 100 with an estimate of the percentage memory available.
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED (a negative number) if Memory Advice was not yet initialized.

MemoryAdvice_getTotalMemory

int64_t MemoryAdvice_getTotalMemory()

Calculates the total memory available on the device, as reported by ActivityManager::getMemoryInfo()

Details
Returns
The total memory of the device, in bytes.
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED (a negative number) if Memory Advice was not yet initialized.

MemoryAdvice_init

MemoryAdvice_ErrorCode MemoryAdvice_init(
  JNIEnv *env,
  jobject context
)

Initialize the Memory Advice library.

This must be called before any other functions.

This version of the init function will use the library provided default params.

Details
Parameters
env
a JNIEnv
context
the app context
Returns
MEMORYADVICE_ERROR_OK if successful,
Returns
MEMORYADVICE_ERROR_ALREADY_INITIALIZED if Memory Advice was already initialized.

MemoryAdvice_initWithParams

MemoryAdvice_ErrorCode MemoryAdvice_initWithParams(
  JNIEnv *env,
  jobject context,
  const char *params
)

Initialize the Memory Advice library.

This must be called before any other functions.

This version of the init function will read the given params instead of using the library provided default params. The format of params is a JSON string with a semantic like default.json in the root directory of this library.

Details
Parameters
env
a JNIEnv
context
the app context
params
the advisor parameters to run the library with
Returns
MEMORYADVICE_ERROR_OK if successful,
Returns
MEMORYADVICE_ERROR_ADVISOR_PARAMETERS_INVALID if the provided parameters are not a valid JSON object,
Returns
MEMORYADVICE_ERROR_ALREADY_INITIALIZED if Memory Advice was already initialized.

MemoryAdvice_registerWatcher

MemoryAdvice_ErrorCode MemoryAdvice_registerWatcher(
  uint64_t intervalMillis,
  MemoryAdvice_WatcherCallback callback,
  void *user_data
)

Registers a watcher that polls the Memory Advice library periodically, and invokes the watcher callback when the memory state goes critical.

This function creates another thread that calls MemoryAdvice_getMemoryState every intervalMillis milliseconds. If the returned state is not MEMORYADVICE_STATE_OK, then calls the watcher callback with the current state.

Details
Parameters
intervalMillis
the interval at which the Memory Advice library will be polled
callback
the callback function that will be invoked if memory goes critical
user_data
context to pass to the callback function
Returns
MEMORYADVICE_ERROR_OK if successful,
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED if Memory Advice was not yet initialized,

MemoryAdvice_unregisterWatcher

MemoryAdvice_ErrorCode MemoryAdvice_unregisterWatcher(
  MemoryAdvice_WatcherCallback callback
)

Removes all watchers with the given callback that were previously registered using MemoryAdvice_registerWatcher.

Details
Returns
MEMORYADVICE_ERROR_OK if successful,
Returns
MEMORYADVICE_ERROR_NOT_INITIALIZED if Memory Advice was not yet initialized.
Returns
MEMORYADVICE_ERROR_WATCHER_NOT_FOUND if the given callback wasn't previously registered.

Macros

MEMORY_ADVICE_BUGFIX_VERSION

 MEMORY_ADVICE_BUGFIX_VERSION 0

MEMORY_ADVICE_MAJOR_VERSION

 MEMORY_ADVICE_MAJOR_VERSION 2

MEMORY_ADVICE_MINOR_VERSION

 MEMORY_ADVICE_MINOR_VERSION 1

MEMORY_ADVICE_PACKED_VERSION

 MEMORY_ADVICE_PACKED_VERSION ANDROID_GAMESDK_PACKED_VERSION(TUNINGFORK_MAJOR_VERSION, \
                                   TUNINGFORK_MINOR_VERSION, \
                                   TUNINGFORK_BUGFIX_VERSION)