function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naganjaneya Lakshman TadepalliNaganjaneya Lakshman Tadepalli 

What is meant by heap size in salesforce and what are limitation due to it?

William TranWilliam Tran
It is in reference to the amount of memory used by the objects. When you create objects in Apex code, as with Java or any standard programming language, memory is allocated to store the objects. And that memory is allocated from the allocated heap.Think of it is a designated amount of memory you can use up in your Apex request

Limits are for synchronous heap size is 6 MB, for asynchronous it is 12 MB

see this for more info:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm#total_heap_size_limit_desc

Thx
Amit Chaudhary 8Amit Chaudhary 8
HI Naganjaneya Lakshman Tadepalli,

Head Size
1.1 Head Size issue in eclipse


If you looking for Head Size issue in eclipse. Please check below blog
http://amitsalesforce.blogspot.in/2015/08/how-to-increase-heap-size-in-eclipse.html
http://www.tgerm.com/2012/12/fixing-performance-and-memory-issues-in.html

1.2 in Apex

What is the heap? Dynamic memory allocation (also known as heap-based memory allocation) is the allocation of memory storage for use in a computer program during the runtime of that program. In Apex the heap is the reference to the amount of memory used by your reachable objects for a given script and request. When you create objects in your Apex code, memory is allocated to store these objects.

Triggers - 300,000 bytes
Anonymous Blocks, Visualforce Controllers, or WSDL Methods - 3,000,000 bytes
Tests - 1,500,000 bytes


Please check below post for more detail.
http://blog.jeffdouglas.com/2010/08/16/managing-the-heap-in-salesforce-com/
https://help.salesforce.com/apex/HTViewSolution?id=000170956&language=en_US (https://help.salesforce.com/apex/HTViewSolution?id=000170956&language=en_US)


Resolution
1.  Look for heap size in debug logs. If heap size is approaching the limit, investigate it and refactor the code.
2. Use the 'Transient' keyword with variables. It is used to declare instance variables that cannot be saved, and shouldn’t be transmitted as part of the view state for a Visualforce page.
3. Use Limit methods. Use heap limits methods in your Apex code to monitor/manage the heap during execution.Limits.getHeapSize() – Returns the approximate amount of memory (in bytes) that has been used for the heap in the current context.
Limits.getLimitHeapSize() – Returns the total amount of memory (in bytes) that can be used for the heap in the current context.
// check the heap size at runtime
if (Limits.getHeapSize > 275000) {
     // implement logic to reduce
}
4. Reduce heap size during runtime by removing items from the collection as you iterate over it.
5. Use SOQL for loops. To avoid heap size limits, developers should always use a SOQL "for" loop to process query results that return many records. See http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_loops_for_SOQL.htm for more information.

View State issue

Please check below blog for same issue.
https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State
http://salesforce.stackexchange.com/questions/19136/how-to-reduce-view-state

Please let us know if this will help you,

Thanks,
Amit Chaudhary