473,603 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GC, Should I care?


I am trying to track a suspected memory leak in an application I'm writing
and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give
me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open the
second form, then close the second form, click the button, close the form
etc. about 20-30 times, the display of GetTotalMemory seems to keep going
up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.


Jul 21 '05 #1
8 1118
Jsp
Use "SetProcessWork ingSetSize(-1,-1)" from kernel32.dll instead.

"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy******** ******@TK2MSFTN GP10.phx.gbl...

I am trying to track a suspected memory leak in an application I'm writing
and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give
me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the form
etc. about 20-30 times, the display of GetTotalMemory seems to keep going
up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.



Jul 21 '05 #2
If Form2 has a collection of controls on it, they've got Finalizers and
Dispose methods that aren't being called (because the only Dispose you're
calling is on Form2 itself). So it's possible that you have a queue of
Finalizers (for those controls) that haven't been called yet, and that queue
grows every time you instantiate a form2. The CLR Profiler should help you
see what's going on.
http://www.microsoft.com/downloads/d...displaylang=en
--
Phil Wilson
[Microsoft MVP-Windows Installer]

"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy******** ******@TK2MSFTN GP10.phx.gbl...

I am trying to track a suspected memory leak in an application I'm writing
and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give
me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the form
etc. about 20-30 times, the display of GetTotalMemory seems to keep going
up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.

Jul 21 '05 #3
OK,

So should I override the Dispose method in Form2 and use it to call Dispose
on each of its controls, I was thinking of giving that a shot anyway.

Chris.
"Phil Wilson" <pd*******@nosp am.cox.net> wrote in message
news:eF******** ******@TK2MSFTN GP12.phx.gbl...
If Form2 has a collection of controls on it, they've got Finalizers and
Dispose methods that aren't being called (because the only Dispose you're
calling is on Form2 itself). So it's possible that you have a queue of
Finalizers (for those controls) that haven't been called yet, and that queue grows every time you instantiate a form2. The CLR Profiler should help you
see what's going on.
http://www.microsoft.com/downloads/d...displaylang=en --
Phil Wilson
[Microsoft MVP-Windows Installer]

"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy******** ******@TK2MSFTN GP10.phx.gbl...

I am trying to track a suspected memory leak in an application I'm writing and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the form etc. about 20-30 times, the display of GetTotalMemory seems to keep going up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.


Jul 21 '05 #4

Chris Mayers wrote:
OK,

So should I override the Dispose method in Form2 and use it to call Dispose
on each of its controls, I was thinking of giving that a shot anyway.

Chris.
No, Control.Dispose disposes all child controls as well. Because every
form derives from Control, in disposes all it's children recursively.

HTH,
Stefan


"Phil Wilson" <pd*******@nosp am.cox.net> wrote in message
news:eF******** ******@TK2MSFTN GP12.phx.gbl...
If Form2 has a collection of controls on it, they've got Finalizers and
Dispose methods that aren't being called (because the only Dispose you're
calling is on Form2 itself). So it's possible that you have a queue of
Finalizers (for those controls) that haven't been called yet, and that
The above is *not* true.

queue
grows every time you instantiate a form2. The CLR Profiler should help you
see what's going on.


http://www.microsoft.com/downloads/d...displaylang=en
--
Phil Wilson
[Microsoft MVP-Windows Installer]

"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy****** ********@TK2MSF TNGP10.phx.gbl. ..
I am trying to track a suspected memory leak in an application I'm


writing
and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to
give
me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the
form
etc. about 20-30 times, the display of GetTotalMemory seems to keep
going
up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.



Jul 21 '05 #5
Re "So should I override ..", the answer is no, unless you are chasing a real
memory leak. if you want to see memory reclaimed, then use
GC.WaitForPendi ngFinalizers in cunjunction with GC.Collect. Once upon a
time, while chasing a memory leak, I coded

Dim i As Integer
Dim l As Long
For i = 1 To 3
GC.Collect()
GC.WaitForPendi ngFinalizers()
l = GC.GetTotalMemo ry(True)
Next

My purpose was to coerce garbage collections so I could watch the memory
leak grow without the confusion of delayed garbage collections. When growth
stopped, I had fixed my memory leak. FYI the leak was my failure to
explicitly call Dispose for a context menu that I created in a non-standard
way.

The bottom line is that you don't need to worry about GC absent a problem
like a memory leak. The code fragment above may help you discover if you do
have a memory leak. Ditto Jsp's comment re trimming the working set. Both
are useful in development and debugging, but they have no place in production
code.
"Chris Mayers" wrote:
OK,

So should I override the Dispose method in Form2 and use it to call Dispose
on each of its controls, I was thinking of giving that a shot anyway.

Chris.
"Phil Wilson" <pd*******@nosp am.cox.net> wrote in message
news:eF******** ******@TK2MSFTN GP12.phx.gbl...
If Form2 has a collection of controls on it, they've got Finalizers and
Dispose methods that aren't being called (because the only Dispose you're
calling is on Form2 itself). So it's possible that you have a queue of
Finalizers (for those controls) that haven't been called yet, and that

queue
grows every time you instantiate a form2. The CLR Profiler should help you
see what's going on.

http://www.microsoft.com/downloads/d...displaylang=en
--
Phil Wilson
[Microsoft MVP-Windows Installer]

"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy******** ******@TK2MSFTN GP10.phx.gbl...

I am trying to track a suspected memory leak in an application I'm writing and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the form etc. about 20-30 times, the display of GetTotalMemory seems to keep going up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.



Jul 21 '05 #6
AMercer wrote:
Re "So should I override ..", the answer is no, unless you are chasing a real
memory leak. if you want to see memory reclaimed, then use
GC.WaitForPendi ngFinalizers in cunjunction with GC.Collect. Once upon a
time, while chasing a memory leak, I coded

Dim i As Integer
Dim l As Long
For i = 1 To 3
GC.Collect()
GC.WaitForPendi ngFinalizers()
l = GC.GetTotalMemo ry(True)
Next


Yes, the correct way to force a garbage collection and ensure everything
that can be collected does indeed get collected is as follows:

Int64 totalMemory;

GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();

totalMemory = GC.GetTotalMemo ry();

The reason you must make two calls to GC.Collect is because the first
call queues inaccessible objects with finalizers in the finalization
queue--in other words, they're automatically promoted to the second
generation. This is why you always hear that you should avoid the use of
finalizers, and to make sure to explicitly and deterministical ly dispose
of such objects. Anyway, you then wait for the all of the objects to be
finalized, and then you perform a full collection again to collect the
now finalized objects.

Your loop implicitly does this; however, I'd make sure to retrieve the
total memory available after an additional call to GC.Collect, instead
of calling it immediately after GC.WaitForPendi ngFinalizers.

- Jesse Towner
Jul 21 '05 #7
"Jsp" <jsp@hotjktco m> wrote in message
news:ew******** *****@TK2MSFTNG P14.phx.gbl...
Use "SetProcessWork ingSetSize(-1,-1)" from kernel32.dll instead.
This does not clear up memory leaks. All it does is tell Windows to tidy up
the processes working set. This may result in less memory use for the
moment, but will significantly slow down your app when it needs to expand
it's working set (e.g. when it creates a new variable).
"Chris Mayers" <ch************ **@SUEDEYahoo.C om> wrote in message
news:uy******** ******@TK2MSFTN GP10.phx.gbl...

I am trying to track a suspected memory leak in an application I'm
writing
and through repeated simplification of my project, I have found the
following which is quite easily replicated should you be prepared to give
me
5 minutes of your time...

Create a new C# (Probably the same in VB dotNET but I've not tried it)
Windows application with 2 forms, the default form -Form1 and one other
one.
that has a bunch of controls on it (say 20 buttons, 20 labels and a
DataGrid) - Form2.

On the default form, add a button to display the second form with some
code
like:

private void button1_Click(o bject sender, System.EventArg s e)
{
using (form2 formTwo = new form2())
{
form2.ShowDialo g();
}
}

also on Form1 add a label, and a timer that updates the label text as
follows (updates every 5 seconds -Interval = 5000-):

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
label1.Text = GC.GetTotalMemo ry(False);
}

and finally a button that forces a Garbage Collection:

private button2_Click(o bject sender, System.EventArg s e)
{
GC.Collect();
}

OK, my question/query is this, if I repeatedly click the button to open
the
second form, then close the second form, click the button, close the form
etc. about 20-30 times, the display of GetTotalMemory seems to keep going
up, and even after forcing Garbage Collections, never seems to get fully
reclaimed. Should I care? and if I should, what (if anything) can I do
about
it?

I would really appreciate an answer from someone who understands these
things...

Thanks a lot,

Chris.


Jul 21 '05 #8
We are saying and doing the same thing. By coding GC.GetTotalMemo ry(True), I
am doing the 2nd GC.Collect() that you indicate. The for loop is used to
counter the following (related) info found in the remarks section of the
documentation for GC.GetTotalMemo ry(forceFullCol lection):

"If forceFullCollec tion is true, this method waits a short interval before
returning while the system collects garbage and finalizes objects. The
duration of the interval is an internally specified limit determined by the
number of garbage collection cycles completed and the change in the amount of
memory recovered between cycles. The garbage collector does not guarantee
that all inaccessible memory is collected."

My objective was an accurate estimate managed memory to help chase a memory
leak. The phrases "short interval" and "does not guarantee" did not inspire
confidence, hence the for loop. In my environment, 3 iterations were
productive, and 4 or more were not.

"Jesse Towner" wrote:
AMercer wrote:
Re "So should I override ..", the answer is no, unless you are chasing a real
memory leak. if you want to see memory reclaimed, then use
GC.WaitForPendi ngFinalizers in cunjunction with GC.Collect. Once upon a
time, while chasing a memory leak, I coded

Dim i As Integer
Dim l As Long
For i = 1 To 3
GC.Collect()
GC.WaitForPendi ngFinalizers()
l = GC.GetTotalMemo ry(True)
Next


Yes, the correct way to force a garbage collection and ensure everything
that can be collected does indeed get collected is as follows:

Int64 totalMemory;

GC.Collect();
GC.WaitForPendi ngFinalizers();
GC.Collect();

totalMemory = GC.GetTotalMemo ry();

The reason you must make two calls to GC.Collect is because the first
call queues inaccessible objects with finalizers in the finalization
queue--in other words, they're automatically promoted to the second
generation. This is why you always hear that you should avoid the use of
finalizers, and to make sure to explicitly and deterministical ly dispose
of such objects. Anyway, you then wait for the all of the objects to be
finalized, and then you perform a full collection again to collect the
now finalized objects.

Your loop implicitly does this; however, I'd make sure to retrieve the
total memory available after an additional call to GC.Collect, instead
of calling it immediately after GC.WaitForPendi ngFinalizers.

- Jesse Towner

Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2272
by: Don Bruder | last post by:
Got a stumper here. I imagine that for someone experienced in C++, this is too pathetic for words. For a rookie, using this project as a sort of "midterm exam" in his self-taught "how to program in C++" course, it's seeming to be an insurmountable problem. Can anyone assist? I'm at wit's end here. Everything *LOOKS* (to this rookie, anyway) correct, but the compiler is barfing on it. Here's the class declaration for Bar class Bar {
1
1547
by: mrskkoroma | last post by:
Dear friend, I am Mrs.kate koroma wife to the late john koroma from Sierra Leon. I am writing you in absolute confidence prmarily to seek your assistance to transfer our cash of($12,000.000.00) now in the custody of a private Security trust firm in Europe. the money is in trunk boxesdeposited and declared as Precious stones by my late Husband as a matter of fact the company does not know the content as money, although my husband made...
354
15714
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets hooked up to interfaces. The Gtk is more than enough gui.
0
1031
by: Chris Mayers | last post by:
Hi, I am trying to track a suspected memory leak in an application I'm writing and through repeated simplification of my project, I have found the following which is quite easily replicated should you be prepared to give me 5 minutes of your time... Create a new C# (Probably the same in VB dotNET but I've not tried it) Windows application with 2 forms, the default form -Form1 and one other one. that has a bunch of controls on it (say...
1
2856
by: Bartje | last post by:
Hey, I am wondering what the best solution will be to program the following problem in access, the dutch 97 edition. I am developing a database for a day care centre (for my girlfriend). This database consist out of six tables, Child, Employee, Group, ChildByGroup, EmployeeByGroup (it must be possible to place a child or an employee in more then 1 group) and GroupSort (this table gives some information about the different kind of...
12
2437
by: polite person | last post by:
I posted a reply to his scatter chart problem over 24 hours ago and still he hasn't replied!
8
1348
by: Simon Willison | last post by:
Hi all, I have an API design question. I'm writing a function that can either succeed or fail. Most of the time the code calling the function won't care about the reason for the failure, but very occasionally it will. I can see a number of ways of doing this, but none of them feel aesthetically pleasing: 1.
122
4199
by: ivan | last post by:
hi all, if I have: if(A && B || C) which operation gets executed first? If I remeber well should be &&, am I correct? thanks
1
1481
by: =?Utf-8?B?V2luIERlZmVuZGVyIEluc3RhbGF0aW9uIGVycm9y | last post by:
I atempted to instal Windows Defender program. But a error: " first remove Windows one care program first" I uninstaled the program via the uninstal wizard & rebooted my HP Media Center 2005 xp. The computer actknoledges the One care removal and warns me I have no virus program runing or instaled. BUT the error: " first remove Wimdows one care program first" persist and stops the Defender program from installing.
2
1372
by: =?Utf-8?B?cGlwZGV2?= | last post by:
Two PCs linked by ethernet cable:- This one - Clarencetoo The other - Claratoo both running XP cannot share printer attached to Claratoo with this PC. This is a new problem as of some 3 or 4 months ago.
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6735
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.