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.

Identity Management in Salesforce

Heap Size issue in Apex

Integration Pattern in Salesforce

 

Pattern Approach
















Streaming API in salesforce

 

Streaming API enables streaming of events using push technology and provides a subscription mechanism for receiving events in near real time. 


The Streaming API subscription mechanism supports multiple types of events, including PushTopic events, generic events, platform events, and Change Data Capture events.


Note

Did you know? Pub/Sub API is a newer API that you can use to publish and subscribe to platform events and change data capture events. Based on gRPC API and HTTP/2, Pub/Sub API efficiently publishes and delivers binary event messages and supports multiple programming languages.



Design Pattern in Apex

 1) Singleton - example utlity class


2) Facade  - A simple and easy-to-understand example of the Facade pattern is a Computer System. When you press the power button on your computer, several subsystems (like the CPU, Memory, Hard Drive) need to work together to start the computer. Instead of interacting with these subsystems individually, you interact with a single, unified interface—the power button.


3) Strategy pattern  - Strategy pattern is a perfect example of runtime polymorphism, where different behaviors (strategies) can be swapped in and out dynamically, depending on the situation.


Factory Pattern - create interface and implements interface

Authorization ways for API

 

https://www.apexhours.com/salesforce-oauth-flows-guidelines-and-tips/

Inbound – you decide! Salesforce supports the following flows


OAuth 2.0 based different time of Authorization - 

  1. Web Server Authentication - when user logic through web app and UI is involved , eg - workbench

  2. User-Agent - used for desktop/mobile application eg - app/data loader/salesforce1

  3. JWT Bearer Token - Ideal for application which access sfdc only through API as there is no  UI involved. For example ETL tools or middleware.
  4. based to digital signature in JSON web token (JWT)
  5. No refresh token

  6. SAML Bearer Assertion
  7. User for SSO
  8. This flow also return only access token not refresh token

  1. SAML Assertion

  2. Username-Password

  3. Device Authentication
  • The OAuth 2.0 device authentication flow is typically used by applications on devices with limited input or display capabilities, such as TVs, appliances, or command-line applications

  1. Asset Token
  2. Refresh Token - 
  3. This flow also return only access token not refresh token

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   ...