The nature of garbage collection is that its frequency and duration are somewhat unpredictable. At least in terms of the performance of a specific piece of code, the garbage collector simply adds noise to the statistics.
From the point of view of the whole program, you are right that if your program is running on a system where the garbage collector is running, then yes, which should be taken into account when measuring the performance of a program. But this is usually not taken into account for individual functions.
source share