Microsoft Knowledge Base Email Alertz

KBAlertz.com: Memory usage increases when you run a Visual Basic 2005 application that creates an instance of a class

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **


Community Site



We Send hundreds of thousands of emails using ASP.NET Email


ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 919481 - Last Review: March 27, 2009 - Revision: 2.0

Memory usage increases when you run a Visual Basic 2005 or Visual Basic 2008 application that creates an instance of a class

On This Page

SYMPTOMS

Consider the following scenario:
  • You create a Microsoft Visual Basic application in Microsoft Visual Studio 2005 or in Visual Studio 2008.
  • The Visual Basic application contains a class.
  • The class contains an event.
  • The Visual Basic application creates an instance of the class.
  • You compile the Visual Basic application in debug mode.
In this scenario, when you run the Visual Basic application, memory usage increases.

Note You are more likely to notice this behavior when the following conditions are true:
  • The Visual Basic application runs continuously for a long time.
  • The Visual Basic application creates many instances of the class that contains an event. If instances are created enough times, the application will eventually fail with an OutOfMemoryException or "out of memory" error. For example, the Visual Basic application contains a loop that creates multiple instances of the class.

CAUSE

This issue occurs because the Edit and Continue debugging feature in Visual Studio 2005 or in Visual Studio 2008 maintains a list of weak references to objects that have been created. If the object that has been created is a class that contains an event, these weak references are maintained for the duration of the program. This behavior increases memory usage.

RESOLUTION

To resolve this issue, use either of the following methods.

Method 1: Restart the Visual Basic application

You can restart the Visual Basic application. When you do this, the memory is freed.

Method 2: Compile the Visual Basic application in release mode

You can compile the Visual Basic application in release mode. When you run a Visual Basic application that was compiled in release mode, memory usage does not increase when you create an instance of a class that contains an event.

MORE INFORMATION

Steps to reproduce the issue

  1. Start Visual Studio 2005 or Visual Studio 2008.
  2. On the File menu, point to New, and then click Project.
  3. Under Project types, click Visual Basic. If you cannot see the Visual Basic project type, expand Other Languages, and then click Visual Basic.
  4. Under Templates, click Console Application.
  5. Type a project name, and then click OK.
  6. Replace all the code in the Code window with the following code.
    Module Module1
    
      Private Sub CreateClassWithEvent()
        Dim TestClass As New CLeak
      End Sub
    
      Sub Main()               
        ' This loop infinitely creates a class that contains an event.
        While True
          CreateClassWithEvent()
          System.GC.Collect()
          System.GC.WaitForPendingFinalizers()
          Threading.Thread.Sleep(50)
        End While
      End Sub
    
    End Module
    
    Class CLeak
      Event myEvent()
    End Class
    
  7. On the Debug menu, click Start Debugging.

APPLIES TO
  • Microsoft Visual Studio 2008 Standard Edition
  • Microsoft Visual Studio 2008 Professional Edition
  • Microsoft Visual Studio 2005 Team System Architect Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Basic 2005
Keywords: 
kbtshoot kbprb KB919481
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please