Salesforce Decode
Salesforcedecode
Back to questions
ApexAdvancedheapmemorylimits

Explain heap size management in complex Apex transformations

Real World Scenario

Batch job processing CSV JSON payloads hits "Apex heap size too large" at 50k line items deserialized into List<Object>.

Expected Answer

• Stream processing in batch execute with smaller chunk sizes • Avoid holding entire dataset in memory—process and release per chunk • Use JSONParser streaming for large JSON instead of deserializeUntyped bulk • Limit relationship query depth and field selection to required columns • Clear collections by re-instantiating or remove cleared elements in long methods • Monitor heap in debug logs with Limits.getHeapSize during development • Move heavy transformation to middleware for extreme volumes

Follow-Up Questions & Answers

Click to expand — each follow-up includes a direct, interview-ready answer

Main difference: use case and scale. Stream processing in batch execute with smaller chunk sizes. Avoid holding entire dataset in memory—process and release per chunk. Pick based on your integration pattern and team capability. Heap limits bite at scale—architects prototype batch jobs at 10x expected execute scope early. Optimize for scale and operational observability.

Architect Perspective

Heap limits bite at scale—architects prototype batch jobs at 10x expected execute scope early.