I have just observed that writing to debug log using System.debug increases heap size as well as CPU time. Is this normal. Please see the screenshot below for the comparison. The first one with so many system.debug statements and the second one is without a single system.debug. Is this a normal behavior from the apex run time? Thanks.
Yes, this is expected, and perfectly normal. Each time you call System.debug, the object you pass in must serialized, even if the system eventually doesn't end up putting anything in to the log, and even if no log is generated at all. You should always remove all debug statements from your code before deploying to production. If you need to see the state of the system, consider using Check Points instead. This will allow you to inspect the state of the heap when the Check Point is triggered in the Developer Console.