Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EC 86] GC.Collect should not be called #36

Closed
GregoireBo opened this issue Apr 26, 2024 · 1 comment · Fixed by #37
Closed

[EC 86] GC.Collect should not be called #36

GregoireBo opened this issue Apr 26, 2024 · 1 comment · Fixed by #37
Assignees
Labels
🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request

Comments

@GregoireBo
Copy link
Contributor

GregoireBo commented Apr 26, 2024

Hi all, on @Djoums advice, I suggest to add this rule.
Sonar already implements this rule, see it here : https://cloud-ci.sgs.com/sonar/coding_rules?open=csharpsquid%3AS1215&rule_key=csharpsquid%3AS1215

Category : Performance
Severity : Warning

Why is this an issue

GC.Collect() is a method, often unecessary that could affect performances. It will trigger a blocking operation that will analyze every objects that are in memory in order to execute the cleanup.

When can it be ignored ?

  • If the specified generation is 0, in this case it should be harmless
  • If a special event in your program caused a lot of long-lived objects to die

Non-compliant Code Example

public static void Main()
{
   GC.Collect() // Non-compliant
}

public static void Main()
{
   GC.Collect(generation: 10) // Non-compliant
}

Compliant Code Example

public static void Main()
{
   GC.Collect(generation: 0); // Compliant
}
@Djoums
Copy link
Collaborator

Djoums commented Apr 26, 2024

The rule PR on the main repository : green-code-initiative/creedengo-rules-specifications#289

@Djoums Djoums added 🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request labels Apr 26, 2024
@Djoums Djoums closed this as completed in #37 May 6, 2024
@Djoums Djoums removed this from C# - Rules Jun 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🗃️ rule rule improvment or rule development or bug 🚀 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants