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

  • SEARCH
  • Home
  • 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 8438511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:44:20+00:00 2026-06-10T07:44:20+00:00

I am a bit of a n00b with memory allocation, but I am trying

  • 0

I am a bit of a n00b with memory allocation, but I am trying to understand how using AsyncCallbacks with the BeginInvoke type design pattern might cause a memory allocation and thusly might cause the garbage collector to be invoked more frequently.

For instance, if I had a class with an event that I wished to invoke asynchronously with a callback, I would normally set it up as follows:

Method I

public class PriceBook
{
    private double Value;

    public void UpdateValue(double Value)
    {
        this.Value = Value;
    }

    public event EventHandler ValueUpdated;
    private void DoValueUpdated(double Value)
    {
        if (ValueUpdated != null)
        {
            ValueUpdated.BeginInvoke(this, EventArgs.Empty, ValueUpdateCompleted, Value);
        }
    }

    private void ValueUpdateCompleted(IAsyncResult ar)
    {
        // Do something when event has finished being called.
    }
}

Somebody that I work with says that calling the BeginInvoke in this way actually allocates memory by creating a new AsyncCallback every time it is called. So, he suggested doing it like this instead:

Method II

public class PriceBook
{
    private AsyncCallback ValueUpdateCallback;
    private double Value;

    public PriceBook1()
    {
        ValueUpdateCallback = ValueUpdateCompleted;
    }

    public void UpdateValue(double Value)
    {
        this.Value = Value;
        DoValueUpdated(Value);
    }

    public event EventHandler ValueUpdated;
    private void DoValueUpdated(double Value)
    {
        if (ValueUpdated != null)
        {
            ValueUpdated.BeginInvoke(this, EventArgs.Empty, ValueUpdateCallback, Value);
        }
    }

    private void ValueUpdateCompleted(IAsyncResult ar)
    {
        // Do something when event has finished being called.
    }
}

As you can see, here I set the AsyncCallback once in the constructor. Does method II really save on memory allocation? Are there any other benefits of either method/is there an even better way of accomplishing what I am trying to do?

  • William
  • 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-06-10T07:44:21+00:00Added an answer on June 10, 2026 at 7:44 am

    Yes, that saves one delegate allocation.

    This

    ValueUpdated.BeginInvoke(..., ValueUpdateCompleted, ...);
    

    is really this:

    ValueUpdated.BeginInvoke(..., new AsyncCallback(ValueUpdateCompleted), ...);
    

    You can see the allocation now.

    If the invocation is not on a hot path, meaning that it is not performance critical, I recommend not to cache the delegate because code maintainability is suffering from it.

    Also, a thread-pool invocation is a lot more expensive than that delegate allocation. I cannot imagine a situation where this might make a difference.

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

Sidebar

Related Questions

The topic might be a bit cryptic, but here's my problem (perhaps a n00b-thing?):
This is child's play, but I'm a bit of a vc n00b. I get
Bit of a n00b question this one, I guess, but here goes. As part
bit of a silly question perhaps. But I want to understand why the syntax
This is probably a n00b question, but nontheless... I have a bit of a
I am sorry if this is a bit of a n00b question but for
I'm using codeigniter and trying to do an UPDATE query, but it keeps creating
I'm a bit of a Java N00B, but I am majorly confused about clone.
bit of a n00b question this one but I haven't been able to find
still a bit of a n00b on SharpSVN, I'm looking to get some simple

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.