THE GOAL: Return as many rows as possible from a Remote method without hitting Heap limits.
We have a Visualforce page calling into a @RemoteAction method in Apex to return sObjects to the page. The catch is that the queries are very dynamic. The final SOQL statement is determined based upon how our customers have configured their app.
Other than the query portion, the remote method's Heap usage is extremely light.
The query is consistent except for one part (the customer's custom fields they select)
SELECT <<10 standard fields>> + <<x Customer fields>> FROM sObjectName WHERE <<yada yada yada>> LIMIT <<what we want to determine>>
Upon testing, it is not a linear equation as to fields queried and heap. Data Type matters and some matter more than others. For example, we noticed that including an ID (Lookup field) in the query dramatically impacts Heap.
I am interested in understanding the inner workings of Apex to better understand how I can better predict Heap and get the most from my query.
Some things in anticipation of your responses.
Using OFFSET is not an option. We are looking to get rows of like 5000+ and OFFSET has limits of its own, so it's not an option.
We don't want to use the API so as to not use our customers API limits. It's a native app so we should use native functionality.
This is more of a question for the Salesforce Apex team, but wanted to start here so some knowledge can get documented for everyone to benefit from.