Dynamic memory allocation which is also known as heap-based memory allocation is the allocation of memory storage for use in a computer program during the run time 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.
The allocation of memory storage for use in a computer program during the run time of that program. When you create objects in your Apex code, memory is allocated to store these objects.
To know the recent Salesforce limit on heap size, check the below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm
Note:
The heap limit in Salesforce is calculated at run time and it also differs on how your Apex code is invoked.
Best Practices:
1. Limits.getHeapSize():
Used to find the approximate amount of memory (in bytes) that has been used for the heap in the current context.
2. Limits.getLimitHeapSize():
Used to find the total amount of memory (in bytes) that can be used for the heap in the current context.
3. Use the ‘Transient’ keyword with variables.
https://help.salesforce.com/s/articleView?id=000395030&type=1
https://help.salesforce.com/s/articleView?id=000384468&type=1
https://help.salesforce.com/s/articleView?id=000385712&type=1