Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3605274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:06:11+00:00 2026-05-18T21:06:11+00:00

Update : I wrote a program to test the memory implications of each of

  • 0

Update: I wrote a program to test the memory implications of each of the techniques I mention below. Not too surprisingly, I found that, sure enough, the conventional approach using .NET events creates a lot more garbage than the other approaches (meaning, it does actually create garbage, as opposed to the other two strategies both of which seem not to create any garbage at all).

Really, I should have stressed all along that I was more interested in the memory overhead of TEventArgs arguments in .NET events than the cost in terms of speed. Ultimately I have to concede that, for all practical purposes—in terms of both memory and speed—the cost is negligible. Still, I thought it was interesting to see that raising a lot of events the “conventional” way does cost something—and that, in extreme cases, it can even lead to gen 1 garbage collections, which may or may not matter depending on the situation (the more “real-time” a system needs to be, in my experience, the more important it is to be mindful of where garbage is being created and how to minimize it where appropriate).


This might seem like a dumb question. I realize that Windows Forms, for instance, could easily be considered a “high-performance” scenario, with hundreds or even thousands of events being raised in very rapid succession (e.g., the Control.MouseMove event) all the time. But still I wonder if it’s really reasonable to design a class with .NET events when it is expected that the class will be used in high-performance, time-critical code.

The main concern I have is with the convention that one use something like EventHandler<TEventArgs> for all events, where TEventArgs derives from EventArgs and is in all likelihood a class that must be intantiated every single time the event is raised/handled. (If it’s just plain EventsArgs, obviously, this is not the case as EventArgs.Empty can be used; but assuming any meaningful and non-constant information is contained in the TEventArgs type, instantiation will probably be needed.) It seems like this results in greater GC pressure than I would expect for a high-performance library to create.

That said, the only alternatives I can think of are:

  1. Using unconventional delegate types (i.e., not EventHandler<TEventArgs>) for events, taking only parameters that don’t require object instantiation such as int, double, etc. (even string, and passing references to existing string objects).
  2. Skipping events altogether and using virtual methods, forcing client code to override them as desired. This seems to have basically the same effect as the previous idea but in a somewhat more controlled way.

Are my concerns about the GC pressure of .NET events unfounded to begin with? If so, what am I missing there? Or, is there some third alternative that is better than the two I’ve just listed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:06:11+00:00Added an answer on May 18, 2026 at 9:06 pm

    Before you do anything, you should consider doing some level of profiling to make sure this is actually going to present a real problem. For cases like UI mouse movement, events happen at such a low frequency compared to the machine that it generally has a negligible impact on GC behavior. Keep in mind that the .NET GC is quite good at collecting short lived objects with few references to them – it’s medium and long-lived objects referenced in many places that could create problems.

    However, if (for some reason) this proves to indeed by an issue, there are a couple of possible approaches to mitigate the cost.

    1. Use a non-standard event delegate signature. You have already identified this as a possible alternative – however, you could always use a generic signature that restricts the type of the event arguments to be a struct. Passing a struct around as a parameter should reduce cases where the parameter is allocated on the heap, at the cost of creating copies of this data.

    2. Use a TEventArgs flyweight implementation that recycles instances of the event argument after use. This can be a tricky proposition, as you need to make sure that event handlers never store an instance of the parameter for use elsewhere. When properly implemented however, this pattern can significantly reduce the number of instances of lightweight types that need to be managed and collected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a .net program to update active directory data in an
I need write an update statement that used multiple tables to determine which rows
I can write: update my_table set xml = updateXML(xml, '/a/b', '1') where document_id =
In Oracle, you can write: update t set xml = updateXML(xml, '/a/b/text()', 'gaga') This
I wrote a batch script which recursively updates my source code against the company's
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add
How do I write the SQL code to INSERT (or UPDATE) an array of
I'm trying to write a query that updates rows in a table if a
I want to write a word addin that does some computations and updates some
I'm in a project to write a program using local database. We use Delphi,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.