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 7538139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:02:35+00:00 2026-05-30T07:02:35+00:00

The following code has a memoryleak. class Program { static void Main(string[] args) {

  • 0

The following code has a memoryleak.

class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            AssemblyResolveMemoryTest assemblyResolveMemoryTest = new AssemblyResolveMemoryTest();
        }
    }
}

class AssemblyResolveMemoryTest 
{
    private byte[] _allocateMemory;

    public AssemblyResolveMemoryTest()
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        //memory is not released anymore
        _allocateMemory = new byte[300000000]; 
    }
    System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        return null;
    }
}

It seems the AssemblyResolve event cause the memory leak.

What is the reason for this?
Do i need to remove the eventhandler explicitly in this case?
If yes, where is the right place to remove this eventhandler? Implement IDisposable or use Try/Finally?

  • 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-30T07:02:36+00:00Added an answer on May 30, 2026 at 7:02 am

    Yes, the event will cause a memory leak. It’s because AppDomain.CurrentDomain.AssemblyResolve is static, so its life doesn’t end until the program ends. So, it will have to keep a reference to all event handlers that have been registered (+=) to it so that they remain in memory when an event occurs which will result in the handlers being called.

    I would suggest that you implement IDisposable in class AssemblyResolveMemoryTest and get it to -= the event.

    Then within the for loop, add a using statement which will cause the dispose to be called.

        for (int i = 0; i < 10; i++)
        {
            using( AssemblyResolveMemoryTest assemblyResolveMemoryTest = new AssemblyResolveMemoryTest() )
            {
            }
        }
    

    You could store the assemblyResolveMemoryTest instances in a list and write a second loop that goes round calling Dispose on them before your program exists. There’s not much point in this though because when your program exists, everything will be dispose and the ‘leaked memory’ will be freed.

    So, in reality, if you do want these event handlers to remain and be in use for the life of your program, then this isn’t actually a memory leak and is memory usage your program needs to function as you want it to.

    I guess the buffer isn’t something you have in a real program, and you’re just using it to demonstrate the problem. Perhaps, if you’ve got memory referenced by your a real assemblyResolveMemoryTest equivalent object in production code, you could try and restructure your program so that the event handler for AssemblyResolve isn’t part of an object that holds onto other memory that’s not relevant to the handling of the event.

    You could use a static method as the event handler, so that no member objects of the class will stay around in memory as you won’t actually need to create an instance of the class in the first place.

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

Sidebar

Related Questions

I just wonder what usage the following code has: public class Sub extends java.util.ArrayList<String>
Consider the following program: int main() { while(...) { int* foobar = new int;
Class 1 has the following code that generates the exception - -(IBAction) searchAllAction: (id)
My object has a private NSMutableArray items. I am using the following code to
The following code has a new callstack when the debugger fires in d (jsfiddle
Why does following code has a correct output? int GGT has no return statement,
The following code has me confused int a=2,b=5,c; c=a+++b; printf(%d,%d,%d,a,b,c); I expected the output
I have the following code: public class Library { public void myFunction() { //
The following code has a simple binding which binds the Text of the TextBlock
I noticed using task manager that the following code has a GDI leak in

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.