Coding Optimisations - Debug.Log
The simplest optimisation which i've hardly seen discussed is removing un-needed debug logs, I wrote a bit about why they are much more expensive than you may think for university and thought i'd share.
An example of reducing unnecessary operations and memory management is removing “Debug.Log”s after the needed information is gathered, especially if it's being called in update. In this situation there is a new string being created every frame, along with all the information which the “Debug.Log” is gathering for its intended debugging purposes, which then has to be printed to the console. This effect on performance can be increased depending on what is being logged, for example, “Debug.Log("Position: " + transform.position);”, (The combination of a string and a Vector3). This operation triggers a string concatenation, meaning the engine has to create a new string which contains both variables. “Transform.position” is not a string but a Vector3, so the engine must convert it to a string representation using its (x,y,z) axis as part of the concatenation, which creates garbage (memory sitting around unused but taking up ram, which then must be found, collected and removed, hurting performance). This new string then needs memory, so a new string object is allocated to the heap, which will then need to be cleaned up by the garbage collector for memory management. The more ‘Garbage’ created, the more unused memory must be identified, marked, cleaned and reclaimed. Large amounts of garbage collection lead to frame drops as computational power is put towards clearing memory, and a buildup of garbage lowers the amount of memory the computer has to actually run the game, if the memory reaches capacity, it leads to major slowdowns and freezes, and potentially a memory error, a crash which occurs when a game tries to use memory it does not have. Debug.Logs also write into the Player.log file, which is a slow operation, they are also not removed from the code once the game is built by default, leading to confusion on optimisation.
My personal practice is to comment out debug logs after use, this way they can just be re-enabled if needed.