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

Item 15: Avoid creating unnecessary objects #58

Open
rjmurillo opened this issue Aug 16, 2024 · 0 comments
Open

Item 15: Avoid creating unnecessary objects #58

rjmurillo opened this issue Aug 16, 2024 · 0 comments
Labels
analyzers enhancement New feature or request help wanted Extra attention is needed .NET
Milestone

Comments

@rjmurillo
Copy link
Owner

Related: BillWagner/EffectiveCSharpAnalyzers#15

Style
This may be hard to find, It's looking for member fields that are new'ed up twice in a constructor path.
The most likely case is initialization and ctor.

Creating and destroying heap-based objects is expensive, and doing so unnecessarily in frequently called methods can lead to significant performance degradation. This is particularly problematic in scenarios such as painting operations or string concatenations, where objects may be created and discarded rapidly, leading to excessive pressure on the garbage collector (GC). The .NET Framework provides mechanisms to avoid unnecessary allocations, but it’s crucial to be mindful of these practices when writing code.

One common issue is the creation of objects like fonts, brushes, and strings in methods that are invoked frequently, such as in the OnPaint event handler. Creating a new object each time the method is called causes the GC to work harder, cleaning up these objects after they are no longer in use. This not only slows down your application but also introduces inefficiencies that could easily be avoided.

Examples

OnPaint event handler

protected override void OnPaint(PaintEventArgs e)
{
    // Bad: Created the same font every paint event.
    using (Font myFont = new Font("Arial", 10.0f))
    {
        e.Graphics.DrawString(DateTime.Now.ToString(),
                              myFont,
                              Brushes.Black,
                              new PointF(0, 0));
    }
    base.OnPaint(e);
}

Issue: Every time OnPaint is called, a new Font object is created and then discarded.

String Concatenation

string msg = "Hello, ";
msg += thisUser.Name;
msg += ". Today is ";
msg += DateTime.Now.ToString();

Issue: Each += operation creates a new string object, leading to multiple unnecessary allocations. The pattern is inefficient and can be avoided by using string.Format, StringBuilder, or interpolated strings.

For further details, see #28

@rjmurillo rjmurillo added this to the vNext milestone Aug 16, 2024
@rjmurillo rjmurillo added enhancement New feature or request analyzers help wanted Extra attention is needed labels Aug 16, 2024
@rjmurillo rjmurillo removed the triage label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzers enhancement New feature or request help wanted Extra attention is needed .NET
Projects
None yet
Development

No branches or pull requests

1 participant