Profiling Apex performance
In order to profile our Apex code, we need to take a series of measurements as to what governor resources have been utilized at multiple steps within the code's execution path. The reason to take multiple recordings is so that we can ensure that we can identify where within the code the resources are being utilized.
Unlike other platforms, which allow the inspection of code, including memory usage, for different portions of the code, there is no such feature in Salesforce. We must perform our profiling manually and then run our analysis to determine what action to take. The way in which we will do this is through the use of the Limits
class.
The Limits class
If you review a debug log generated by Salesforce when a process runs, you will see at the end of the log a long list of information that details the resource usage for the different namespaces that were invoked during the transaction. An example of this information is shown in the following...