Gui thread, memory leak, and garbage collection

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Hi all,

I seem to be chewing up memory at a nasty rate when my app runs.
It has a polling loop and not surprisingly this is the part that keeps doing it.
I think I've located the line that is my main offender and its when I marshal to get back onto the GUI thread so I can update my controls.

private void KPI_ScreenUpdate(KPI KPIType)
{

if (this.InvokeRequired)
this.Invoke(new KPI_ScreenUpdateDelegate(KPI_ScreenUpdate), new object[] { KPIType });
else

{

DPL_Units_Through.Value = myKPI_Shift.Units_Through;
DPL_Shift_Time.Value = tmpElapsed.ToString();
DPL_Elapsed_Setup_Time.Value = tmpSetupElapsed.ToString();

}



Now what I've noticed is that anytime I create an object using 'NEW' keyword in my loop (called every n seconds) .. I can watch the memory usage of my app go up and never down.

So mostly I've been ensuring that I never create an object in one of these loops.

But I thought that the garbage collector would deal with these objects once they go out of scope .. and I dont know any other way except the above snippet to ensure I dont cross thread for the control update.

Will the garbage collector keep my memory under control if I use it implicitly ??? or am I doing something fundementally flawed when marshalling threads ?

Regards Bob
 
Bob,

From what I see in the code you posted I must say you don't do anything wrong and this is exactly the way to update the UI from a worker thread.

Regarding the memory leaks there might be problems some where in the code, but from what I can see here the GC should take care of the memory heap and should keep everything under control. Just keep in mind that the GC kick off only if there is need for that; until then the memory consumption will increase.


--

Stoitcho Goutsev (100)
Hi all,

I seem to be chewing up memory at a nasty rate when my app runs.
It has a polling loop and not surprisingly this is the part that keeps doing it.
I think I've located the line that is my main offender and its when I marshal to get back onto the GUI thread so I can update my controls.

private void KPI_ScreenUpdate(KPI KPIType)
{

if (this.InvokeRequired)
this.Invoke(new KPI_ScreenUpdateDelegate(KPI_ScreenUpdate), new object[] { KPIType });
else

{

DPL_Units_Through.Value = myKPI_Shift.Units_Through;
DPL_Shift_Time.Value = tmpElapsed.ToString();
DPL_Elapsed_Setup_Time.Value = tmpSetupElapsed.ToString();

}



Now what I've noticed is that anytime I create an object using 'NEW' keyword in my loop (called every n seconds) .. I can watch the memory usage of my app go up and never down.

So mostly I've been ensuring that I never create an object in one of these loops.

But I thought that the garbage collector would deal with these objects once they go out of scope .. and I dont know any other way except the above snippet to ensure I dont cross thread for the control update.

Will the garbage collector keep my memory under control if I use it implicitly ??? or am I doing something fundementally flawed when marshalling threads ?

Regards Bob
 
Bob,

The garbage collection doesn't run immediately. It may be that memory
goes up and eventually the garbage collector runs, but since you keep
creating new objects, memory doesn't go down (b/c you're pretty much
filling the space just emptied).

I would just check to make sure that everything you use which
implements IDisposable is being disposed of properly.

Andy
Hi all,

I seem to be chewing up memory at a nasty rate when my app runs.
It has a polling loop and not surprisingly this is the part that keeps doing it.
I think I've located the line that is my main offender and its when I marshal to get back onto the GUI thread so I can update my controls.

private void KPI_ScreenUpdate(KPI KPIType)
{

if (this.InvokeRequired)
this.Invoke(new KPI_ScreenUpdateDelegate(KPI_ScreenUpdate), new object[] { KPIType });
else

{

DPL_Units_Through.Value = myKPI_Shift.Units_Through;
DPL_Shift_Time.Value = tmpElapsed.ToString();
DPL_Elapsed_Setup_Time.Value = tmpSetupElapsed.ToString();

}



Now what I've noticed is that anytime I create an object using 'NEW' keyword in my loop (called every n seconds) .. I can watch the memory usage of my app go up and never down.

So mostly I've been ensuring that I never create an object in one of these loops.

But I thought that the garbage collector would deal with these objects once they go out of scope .. and I dont know any other way except the above snippet to ensure I dont cross thread for the control update.

Will the garbage collector keep my memory under control if I use it implicitly ??? or am I doing something fundementally flawed when marshalling threads ?

Regards Bob

------=_NextPart_000_0006_01C68A59.A0F96860
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 2429

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2873" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hi all, </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I seem to be chewing up memory at a nasty rate when
my app runs.</FONT></DIV>
<DIV><FONT face=Arial size=2>It has a polling loop and not surprisingly this is
the part that keeps doing it.</FONT></DIV>
<DIV><FONT face=Arial size=2>I think I've located the line that is my main
offender and its when I marshal to get back onto the GUI thread so I can update
my controls.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV>
<P><FONT face=Arial size=2>private void KPI_ScreenUpdate(KPI
KPIType)<BR>{</FONT></P>
<P><FONT face=Arial size=2>if (this.InvokeRequired)<BR>&nbsp;&nbsp;&nbsp;
&nbsp;this.Invoke(new KPI_ScreenUpdateDelegate(KPI_ScreenUpdate), new object[] {
KPIType });<BR>else</FONT></P>
<P><FONT face=Arial size=2>{</FONT></P>
<P><FONT face=Arial size=2>&nbsp;DPL_Units_Through.Value =
myKPI_Shift.Units_Through;<BR>&nbsp;DPL_Shift_Time.Value =
tmpElapsed.ToString();<BR>&nbsp;DPL_Elapsed_Setup_Time.Value =
tmpSetupElapsed.ToString();</FONT></P>
<P><FONT face=Arial size=2>}</FONT></P>
<P><FONT face=Arial size=2></FONT>&nbsp;</P>
<P><FONT face=Arial size=2>Now what I've noticed is that anytime I create an
object using 'NEW' keyword in my loop (called every n seconds) .. I can watch
the memory usage of my app go up and never down.</FONT></P>
<P><FONT face=Arial size=2>So mostly I've been ensuring that I never create an
object in one of these loops.</FONT></P>
<P><FONT face=Arial size=2>But I thought that the garbage collector would deal
with these objects once they go out of scope .. and I dont know any other way
except the above snippet to ensure I dont cross thread for the control
update.</FONT></P>
<P><FONT face=Arial size=2>Will the garbage collector keep my memory under
control if I use it implicitly ??? or am I doing something fundementally flawed
when marshalling threads ?</FONT></P>
<P><FONT face=Arial size=2>Regards Bob</FONT></P></DIV></BODY></HTML>

------=_NextPart_000_0006_01C68A59.A0F96860--
 
Back
Top