Thursday, 17 October 2024

Heap Size and Apex CPU Time Limit

 

Heap Size -

1) Memory size for holding object , variables and records

2)




CPU Time limit - 

1) time consuming during whole transaction



 

Aspect

Heap Size

Apex CPU Time Limit

Definition

Maximum amount of memory allocated for storing objects and data during an Apex transaction.

Maximum amount of CPU processing time used by an Apex transaction.

Measurement

Measured in bytes (e.g., MB).

Measured in milliseconds (ms).

Typical Limits

6 MB for synchronous transactions, 12 MB for asynchronous transactions.

10,000 ms (10 seconds) for synchronous transactions, 60,000 ms (60 seconds) for asynchronous transactions.

Purpose

To manage memory usage and prevent excessive consumption that could affect performance and stability.

To manage CPU processing time and prevent long-running operations from degrading platform performance.

Scope

Affects how much data and how many objects can be stored in memory during a transaction.

Affects how long a transaction can use CPU resources for processing.

Common Causes of Issues

Large collections or objects, inefficient data handling, excessive memory usage.

Complex or inefficient code, lengthy calculations, large data processing tasks.

Resolution Strategies

Optimize memory usage by managing data efficiently, using smaller collections, and clearing unused objects.

Optimize algorithms, reduce complexity, use batch processing for large tasks.

Impact of Exceeding

Causes System.LimitException: Heap Size error; affects the ability to store and manage data in memory.

Causes System.LimitException: CPU Time Limit error; affects the ability to process code efficiently.

Example

Attempting to store a large list of records or creating large objects in a single transaction.

Running a complex loop or extensive calculations that consume too much processing time.

 

For CPU Time Optimization:

  • Focus on optimizing SOQL queries to reduce the amount of data processed and minimize execution time.
  • Optimize for-loops to avoid excessive iterations and nested loops that increase CPU usage.

For Heap Size Optimization:

  • Optimize SOQL queries to limit the volume of data retrieved and stored in memory.
  • Manage collections and avoid holding large amounts of data in memory for extended periods.

No comments:

Post a Comment

Heap Size and Apex CPU Time Limit

  Heap Size - 1) Memory size for holding object , variables and records 2) CPU Time limit -  1) time consuming during whole transaction   ...