I have heard from few of my developer friends saying: “Garbage Collection is Automatic. So, I don’t have to worry about it.“ First part is true, i.e. “Garbage Collection is Automatic” on all modern platforms – JVM (Java Virtual Machine), ART (Android Run Time)… But the second part may not be as true as you think, i.e. “I don’t have to worry about it.” Garbage Collection is automatic, but it’s not free. It comes with a price. In fact, the price can be *very expensive*.
Poor Garbage Collection can lead to:
- Unpleasant user experience (SLA Breaches)
- Increase the bill from cloud hosting providers
- Puts entire Application Availability under risk
Let’s discuss them.
1. Unpleasant user experience (SLA Breaches)
To garbage collect objects automatically, entire application must be paused intermittently to mark the objects that are in use and sweep away the objects that are not used. During this pause period, all customer transactions which are in motion will be stalled (i.e. frozen). Depending on the type of GC algorithm and memory settings that you configure, pause times can run from few milliseconds to few seconds to few minutes. Thus, Garbage Collection can affect your application SLA (Service Level Agreement) significantly. Frequent pauses in the mobile application can jank the app (i.e. stuttering, juddering, or halting). It can leave a very unpleasant experience for your user.
2. Increase the bill from cloud hosting providers
Garbage collection consumes a lot of CPU cycles. Each application will have thousands/millions of objects sitting in memory. Each object in memory should be investigated periodically to see whether they are in use? If it’s in use, who is referencing it? Whether those references are still active? If they are not in use, they should be evicted from memory. All these investigations and computation requires a considerable amount of CPU power.
Most applications saturate memory first before saturating other resources (CPU, network bandwidth, storage). Most applications upgrade their EC2 instance size to get additional memory rather get additional CPU or network bandwidth. More object creation translates to more frequent Garbage Collection. Thus, you will end up buying more compute power. It will increase the bill from your cloud hosting providers.
3. Puts entire Application Availability under risk
Sometimes garbage collection events pause the application for several seconds to several minutes. Sometimes garbage collection events might run repeatedly. When Garbage Collection runs repeatedly, no customers transactions will be processed. When Garbage Collection runs repeatedly, to recover from the situation, the application has to be recycled. Such events can put the availability of your applications under risk.
Thus, to achieve ‘Wow’ user experience, reduce bills from hosting providers and increase your application’s availability, one would have to study and optimize the Memory/Garbage Collection settings. Tools such as GCeasy.io, HP Jmeter can help you to study and optimize the Memory/Garbage collection settings.