If you create an application by using the Microsoft .NET
Framework version 1.0, and you use the DateTimePicker
control in the application, you may notice a memory leak when you
run the application.
This problem occurs because the garbage collector does not
collect the inaccessible memory. The DateTimePicker
control is associated with the Microsoft.Win32.UserPreferenceChangedEventHandler
delegate. When you dispose of an object of the DateTimePicker
control, this delegate is not dereferenced. Therefore, this
delegate prevents the garbage collector from collecting the DateTimePicker
To resolve this problem, obtain the latest service pack for
the .NET Framework 1.0.
To obtain the latest service pack, visit the
following Microsoft Web site:
has confirmed that this is a problem in the Microsoft products that are listed
in the "Applies to" section. This problem was
first corrected in the Microsoft .NET Framework 1.0 Service Pack
Steps to reproduce the behavior
- Create a Microsoft Windows application. To do this, follow
- Start Microsoft Visual Studio .NET.
- On the File menu, point to
New, and then click Project. The New
Project dialog box appears.
- Under Project Types, click
Visual C# Projects.
- Under Templates, click
- In the Name box, type
MyProject, and then click OK. By
default, a form that is named Form1 is created.
- On the View menu, click
- Add three TextBox controls to the Form1 form. By default, the textBox1 TextBox control, the textBox2 TextBox control, and the textBox3 TextBox control are created.
- Add two Button controls to the Form1 form. By default, the button1 Button control and the button2 Button control are created.
- Add the following code to the Form1_Load event handler of the Form1 form.
The GC.GetTotalMemory method retrieves the number of bytes currently allocated in the
long before = GC.GetTotalMemory(true);
textBox1.Text ="Before test: " + before.ToString();
- On the Form1 form, double-click
button1, and then add the following code to the button1_Click event handler of the Form1 form.
for (int i=0; i<100; i++)
DateTimePicker dtp=new DateTimePicker();
after = GC.GetTotalMemory(true);
textBox2.Text = " After test: " + after.ToString();
- On the Form1 form, double-click
button2, and then add the following code to the button2_Click event handler in the Form1 form.
The GC.Collect method forces the garbage collection and tries to collect all the
inaccessible and dereferenced memory.
long after = GC.GetTotalMemory(true);
textBox2.Text = " After Garbage Collection: " + after.ToString();
- On the File menu, click Save
All to save all the files.
- Build and then run the Windows application. To do this,
follow these steps:
- On the Build menu, click Build
- On the Debug menu, click
Start. The Form1 dialog box appears. The number of bytes that
are allocated in managed memory appears in the textBox1 text
- Click button1.
application creates an instance of the DateTimePicker control at run time, and then disposes of the DateTimePicker control. This behavior occurs 100 times. The number of bytes in
managed memory after 100 occurrences appears in the textBox2 text box. The garbage collector does not collect the inaccessible
- Click button2.
The number of bytes that remain in managed memory after
the application calls the garbage collector appear in the
textBox2 text box. You may notice that most of the
inaccessible memory is still in managed memory.
For additional information, click the
following article number To view the article In the Microsoft Knowledge Base:
Description of the standard terminology that Is used To describe Microsoft software updates
For more information about the UserPreferenceChangedEventHandler
delegate, visit the following Microsoft Developer Network (MSDN)
For more information about the DateTimePicker
class, visit the following MSDN Web site:
Article ID: 813354 - Last Review: October 26, 2013 - Revision: 1.5
- Microsoft .NET Framework 1.0
|kbnosurvey kbarchive kbbug kbfix kbgarbagecollect kbqfe kbmemory kbforms kbnetframe100presp3fix kbcode kbhotfixserver KB813354|