-3

I understand there are a lot of questions talking about when should we use GC.Collect(), but I would like to know how to verify a complicated enterprise program if I realize one of the class has this line that seems to cause performance issue? Are there any good steps to follow and verify this line is safe to remove?

CharithJ
  • 46,289
  • 20
  • 116
  • 131
mting923
  • 431
  • 1
  • 6
  • 15
  • The question should argubly be _"when to **add** `GC.Collect()`"_ –  Aug 08 '17 at 07:21
  • _"...seems to cause performance issue..."_ - doubt that would be a problem post .NET 4 on workstations and .NET 4.5 on server –  Aug 08 '17 at 07:23

1 Answers1

2

When to remove GC.Collect

Pretty much whenever you see it...

Are there any good steps to follow

If you can't see any good reason to have it. Good step to follow is just removing it. I can't see it can introduce any NEW issues. Garbage collector has been optimized to call this automatically when ever needed.

As mentioned in this answer, there can be very rare cases that, this can be useful. But generally, whenever I see this, it gives me a bad impression and concerns...

verify this line is safe to remove

Sometime people blindly add this when there are memory leak concerns. But this can't help for any memory leaks. So, just use a memory profiling tool and verify that there are no memory leaks. However, just removing this can't introduce any NEW memory leak.

CharithJ
  • 46,289
  • 20
  • 116
  • 131