+ Start a Discussion
Prachi Gadewar SFDCPrachi Gadewar SFDC 

Getting heap size error while serializing list of records

Hi,

I am suffering with Salesforce apex LimitException issue. I am fetching thousands of records from few objects and putting it into the collection like Map. Now I have a requirement to generate JSON for same records which will be used for mobile devices to download data from salesforce.
When I tried to serialize those records by using System.JSON.Serialize() method; it is generating a huge JSON string and I am getting System.LimitException error because there will be more memory required than available space.
I tried to catch that issue using try/catch block but here is a reference available which says that System.LimitException can’t be caught by Catch block.
Referral URL: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_exception_methods.htm
I know that we can check heap size limit through Limits.getHeapSize() method. But is there any work around so that I can handle this issue from apex side.
A code sample or some reference in this regard would be highly appreciated.
Thanks in advanced.
suresh dupadasuresh dupada
Hi I am also looking for solution to this issue,  Please let me know if you already done something about to over come this issue

Thanks in Adavance
kibitzerkibitzer
Well, let's start with the obvious. You do have a heap size limit to deal with.
Now, I'm not sure what the overall architecture you're trying to implement is, here. If I you have control over the mobile application as well as the server side, you could look at breaking up the requests so that your mobile app doesn't try to pull in one giant JSON file at once. Not only might that take a while to download, but the processing time on the client could be significant.
But, let's say you do need to create one gigantic JSON string and store it, say, in a document, in order to send it to a mobile device (or even serve it up dynamically).

There are two possibilities at this point.

1. The resulting JSON string alone is causing the heap exception.
2. The JSON string fits into memory, but the combination of the JSON string and source records do not.

If the problem is #1, you can't serve the string dynamically - you have to build a document that contains the string. For this you'll most likely need to use batch Apex to iterate over the records and build of the JSON string manually.

If the problem is #2, you can do a SOQL loop on the objects so you only query 200 at a time - thus limiting the size of the objects in memory. This may reduce the heap usage enough for you to build your JSON string dynamically and either store it in a document, or serve it up dynamically.

JSON strings are build manually using the JSONGenerator class. It's not terribly hard to use, just tedious, as it involves iterating through the objects and object fields and writing to the generator one at a time.

Be aware that serialization is CPU intensive - so even if you get under heap limits, you may run into CPU time limits. Just saying.

Are you sure there's no way to break up the requests into smaller datasets?
 
Kevin AkermanisKevin Akermanis
Why are you pulling back ALL the records at once?  Do the end users really need to see all the records at once?
You may need to look at using SOQL pagination to limit the amount of data that comes back and once the user scrolls further down the page, perform another SOQL query to pull more records down just in time (like how this discussion forum works - as you scroll down the page it loads more posts as you get there).
Check out this blog post to introduce SOQL pagination: https://developer.salesforce.com/blogs/tech-pubs/2012/06/the-joys-of-soql-pagination.html