Search code examples
c#google-apimono

ScriptService execute eating memory


When I run Execute on a ScriptService run request, it takes some memory and fails to release it. Testing this by running on monodevelop on a Raspberry Pi shows the memory rising at an alarming rate, and will eventually crash the program. The GC.Collect was an attempt at re-claiming this memory. Is there any insight into what I am doing wrong?

public MainWindow() : base(Gtk.WindowType.Toplevel)
{
    Build();

    while (true)
    {
        getDashRow();

        Thread.Sleep(1000);
        Console.WriteLine("Total available memory before collection: {0:N0}", System.GC.GetTotalMemory(false));

        System.GC.Collect();

        Console.WriteLine("Total available memory collection: {0:N0}", System.GC.GetTotalMemory(true));
    }
 }

 private int getDashRow()
 {
    ScriptsResource.RunRequest runreq;
    DriveService driveservice;
    ExecutionRequest exrequest;

    Console.WriteLine("getDashRow");
    int retval = 0;

    exrequest = new ExecutionRequest();
    exrequest.Function = "getMacRow";
    IList<object> parameters = new List<object>();
    parameters.Add(spreadsheetname);
    exrequest.Parameters = parameters;
    exrequest.DevMode = false;

    try
    {
        // run a Google Apps Script function on the online sheet to find number of rows (more efficient)
        runreq = scriptservice.Scripts.Run(exrequest, dashscriptid);

        // following line consumes the memory
        Operation op = runreq.Execute();

        retval = Convert.ToInt16(op.Response["result"]);
        parameters = null;
        exrequest = null;
        op = null;
    }
    catch (Exception ex)
    {
        Console.WriteLine("getDashRow: " + ex.Message);
    }

    return retval;
}

Solution

  • I solved this, installing Mono Preview v6.0.0.277 has resolved this problem, memory is now correctly freed up without manually calling GC.Collect().

    Related issue which led to the solution