Link to home
Start Free TrialLog in
Avatar of sutorius
sutoriusFlag for United States of America

asked on

GC continues to grow how can I clean it up?

I have the following code and Gen#0 GC continues to grow. I thought adding gc.collect into the code would help but it doesn't. What code do I need to add to get the GC to stop growing?

int i = 0;

            try
            {
                while (i < 200000)
                {
                    tEditStart = DateTime.Now;
                    IAsyncResult iar = dlEdit.BeginInvoke(5, out threadId, null, null);

                    while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

                    int iDllReturn = dlEdit.EndInvoke(out threadId, iar);

                    tEditFinish = DateTime.Now;

                    tsEdit = tEditFinish - tEditStart;
                    lNbrSec = tsEdit.Milliseconds;
                    i++;
                    textBox1.Text = "DLL Result: " + iDllReturn + " - Call# " + i + " took " + lNbrSec.ToString();
               
                    GC.Collect(0);
                }
            }
ASKER CERTIFIED SOLUTION
Avatar of BlackTigerX
BlackTigerX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sutorius

ASKER

How do you recommend output the DLL Result?
Avatar of BlackTigerX
BlackTigerX

do you have to display that 200,000 times?

first, move it out of the while loop to see if that helps

and remove the GC.Collect();
With each call to the DLL I need to see the result, even if it is for a blip of a second.

That solved part of the problem. The .NET Framework class Control.MultithreadSafeCallScope is no longer growing but another one is, WindowsFormsSynchronizationContext.

I am a web guy not a windows guy so I don't understand how you are penalized for having a textbox on a form in a while loop as a memory leak (undisposed instances). Can you explain?
the problem there is that you are allocating 200,000 strings, strings are immutable objects, which means you cannot change their contents (in managed code), everytime you assign a string, you are actually trowing away the current one, requesting memory for the new string, and assigning it's value
also, this code

while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

is not a very good idea, you should put a Sleep in between, at least

while (iar.IsCompleted == false)
{
  Thread.Sleep(30);
  Application.DoEvents();
}
Thanks for your assistance.
and even better than that, you should use functions to wait for signals instead of that loop, I don't know exactly what objects you are using, so I can't be more specific
Can you be specific now with your idea on using functions instead of a loop?

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public delegate int AsyncDelegate(int callType, out int threadId);
        int threadId;
        string sProcessMsg = "";
        //string strWaiting = "Waiting";
        //long lNbrMin = 0;
        long lNbrSec = 0;
        DateTime tEditStart;
        DateTime tEditFinish;
        TimeSpan tsEdit;
        int iDllReturn;

        public Form1()
        {

            InitializeComponent();
           
            //this.Controls.Add(this.textBox1);

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            AsyncCalls();
        }

        public void AsyncCalls ()
        {
            // Create an instance and delegate of the Edit DLL.  
            EditCall cEdit = new EditCall();
            AsyncDelegate dlEdit = new AsyncDelegate(cEdit.CallHCFA);

            int i = 0;

            try
            {
                while (i < 200000)
                {
                    tEditStart = DateTime.Now;
                    IAsyncResult iar = dlEdit.BeginInvoke(5, out threadId, null, null);

                    while (iar.IsCompleted == false)
                    {
                        Application.DoEvents();
                    }

                    iDllReturn = dlEdit.EndInvoke(out threadId, iar);

                    tEditFinish = DateTime.Now;

                    tsEdit = tEditFinish - tEditStart;
                    lNbrSec = tsEdit.Milliseconds;
                    i++;
                }
                //textBox1.Text = "DLL Result: " + iDllReturn + " - Call# " + i + " took " + lNbrSec.ToString();
            }
            catch (Exception f)
            {
                MessageBox.Show("Exception: " + f.Message, sProcessMsg,
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (components != null)
                {
                    components.Dispose();
                }
                base.Dispose();
            }
        }
    }

   

    public class EditCall
    {
        [DllImport("Ecghcfa.dll", EntryPoint = "ECgHCFA")]
        public extern static int ECgHCFA(int lEditNo, string sParams, string sServer, string sDatabase, string sUsername, string sPassword);
        // The method to be executed asynchronously.
        public int CallHCFA(int callType, out int iReturn)
        {
            if (callType == 3)
                try
                {
                iReturn = ECgHCFA(3, "", "cltecdev5\\audit", "ca101web", "Install", "LiveStrong");
                }
                catch (Exception f)
                {
                    MessageBox.Show("Message " + f);
                }
            else
                try
                {
                    iReturn = ECgHCFA(5, "", "cltecdev5\\audit", "ca101web", "Install", "LiveStrong");
                }
                catch(Exception f)
                {
                    MessageBox.Show("Message " + f);
                }
            iReturn = 0;
            return (iReturn);
        }
    }
}
first of all, why are you even using asynchronous methods, if you're waiting for it to be done before you proceed anyway?

here's some interesting reading
http://www.codeproject.com/csharp/begininvoke.asp
http://www.codeproject.com/csharp/workerthread.asp?df=100&forumid=2441&exp=0&select=1178892
Thanks for the articles. Very helpful. You make a good point about using asynch.