Skip to main content

imp.getmemoryfree()

Returns the amount of currently unused memory in the imp

Availability

Device + Agent

Returns

Integer — the amount of free RAM in bytes

Description

This method reports the amount of currently unused memory in the imp. The available memory within an imp may become fragmented. Hence allocating objects smaller than the free memory report can still fail, causing an out-of-memory error.

Example Code

Filling an array with 1KB blobs will very quickly fill an imp’s memory, as the following code demonstrates.

/***** THIS CODE WILL EVENTUALLY PRODUCE AN OUT OF MEMORY ERROR AND RESTART *****/
// GLOBAL VARIABLES
local array = [];
// FUNCTIONS
function allocateMemory() {
// Add a 1KB blob to the array
array.push(blob(1024));
// Report how much free memory there is
server.log(imp.getmemoryfree());
// Re-run the loop
imp.wakeup(5, allocateMemory);
}
// RUNTIME START
// Start the loop
allocateMemory();