Requesting RAM Synchronously

This section describes how an application can request for free RAM synchronously.

Perform the steps described in Allowing Large Memory Allocation .

Requesting free RAM synchronously is easy to implement because no active object is needed and is recommended when there is no need to perform any other task while waiting for the request to complete. This approach is not recommended in case of a server, as synchronous call blocks the server and it cannot serve its clients during synchronous wait.

Request free memory.
       User::LeaveIfError(iOomMonitorSession.RequestFreeMemory(KLargeValueToAllocate));
DoFunctionRequiring_KLargeValueToAllocate_Bytes();
      

The following code snippet illustrates an synchronous request for 2MB RAM:

       TInt KLargeValueToAllocate = 2097152; //2MB

// Create an OOM Monitor session
ROomMonitorSession oomMonitorSession;
CleanUpClosePushL(oomMonitorSession);
User::LeaveIfError(oomMonitorSession.Connect());

// Request free memory
User::LeaveIfError(iOomMonitorSession.RequestFreeMemory(KLargeValueToAllocate));
DoFunctionRequiring_KLargeValueToAllocate_Bytes();

// Tidy up
CleanupStack::PopAndDestroy(); //oomMonitorSession, calls Close()