[SalesForce] Heap size limit in Javascript Remoting method

I'm fairly sure that Remoting is Asynchronous. The docs state that the Apex heap size limit is 6Mb for a Synchronous op, and 12Mb for an Asynchronous op. However, from my RemoteAction (@Readonly) call I'm getting errors like

Apex heap size too large: 6041150

which I assume refers to bytes. Am I misunderstanding here? The only reason I need heap size at all is to get around the limit of 2000 for OFFSET statements.

Best Answer

Yes, it's bytes, and yes, synchronous calls are limited to 6MB.

To reduce total heap usage, use a query-more loop:

Integer skipRows = 10000;
Account[] results = new Account[0];
for(Account record:[SELECT Id, Name FROM Account LIMIT 12000]) {
    if(skipRows-- < 0) {
        results.add(record);
    }
}

This "query-more" loop uses only as much heap as necessary for 200 results plus the size of the result list.