HTTPS + HttpWebRequest oddness

Hi All,

I have an application running on a Fez Spider which makes an HTTPS get to a cloud based server. When setting up one of the devices I made a mistake configuring the update rate and it was making an HTTPS call every 10 seconds which was much much faster than intended. The misconfigured device kept on running out of memory and crashing so I built a test harness to explore what was going on. I found that an HTTP call was fine but it looks like an HTTPS call leaks round 45K on each call. I have tried forcing a GC and different ways of declaring the certificates etc. but no joy.

What am I doing wrong? Has anyone else encountered this?

Free memory for 10 calls
7120164
7074420
7029252
6984036
6938952
6893736
6848520
6803304
6758088
6712740

HTTPS Memory info – Start

Type 0F (STRING ): 3084 bytes
Type 11 (CLASS ): 14352 bytes
Type 12 (VALUETYPE ): 1512 bytes
Type 13 (SZARRAY ): 7860 bytes
Type 03 (U1 ): 3192 bytes
Type 04 (CHAR ): 852 bytes
Type 07 (I4 ): 1044 bytes
Type 0F (STRING ): 72 bytes
Type 11 (CLASS ): 2616 bytes
Type 12 (VALUETYPE ): 84 bytes
Type 15 (FREEBLOCK ): 7118664 bytes
Type 16 (CACHEDBLOCK ): 216 bytes
Type 17 (ASSEMBLY ): 32688 bytes
Type 18 (WEAKCLASS ): 96 bytes
Type 19 (REFLECTION ): 192 bytes
Type 1B (DELEGATE_HEAD ): 864 bytes
Type 1D (OBJECT_TO_EVENT ): 240 bytes
Type 1E (BINARY_BLOB_HEAD ): 152496 bytes
Type 1F (THREAD ): 1536 bytes
Type 20 (SUBTHREAD ): 144 bytes
Type 21 (STACK_FRAME ): 1632 bytes
Type 22 (TIMER_HEAD ): 216 bytes
Type 27 (FINALIZER_HEAD ): 144 bytes
Type 31 (IO_PORT ): 108 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 3552 bytes

HTTPS Memory info – Finish

Type 0F (STRING ): 3084 bytes
Type 11 (CLASS ): 14364 bytes
Type 12 (VALUETYPE ): 1512 bytes
Type 13 (SZARRAY ): 7860 bytes
Type 03 (U1 ): 3192 bytes
Type 04 (CHAR ): 852 bytes
Type 07 (I4 ): 1044 bytes
Type 0F (STRING ): 72 bytes
Type 11 (CLASS ): 2616 bytes
Type 12 (VALUETYPE ): 84 bytes
Type 15 (FREEBLOCK ): 6711372 bytes
Type 16 (CACHEDBLOCK ): 96 bytes
Type 17 (ASSEMBLY ): 32688 bytes
Type 18 (WEAKCLASS ): 96 bytes
Type 19 (REFLECTION ): 192 bytes
Type 1B (DELEGATE_HEAD ): 828 bytes
Type 1D (OBJECT_TO_EVENT ): 240 bytes
Type 1E (BINARY_BLOB_HEAD ): 559476 bytes
Type 1F (THREAD ): 1920 bytes
Type 20 (SUBTHREAD ): 192 bytes
Type 21 (STACK_FRAME ): 1632 bytes
Type 22 (TIMER_HEAD ): 216 bytes
Type 27 (FINALIZER_HEAD ): 168 bytes
Type 31 (IO_PORT ): 108 bytes
Type 34 (APPDOMAIN_HEAD ): 72 bytes
Type 36 (APPDOMAIN_ASSEMBLY ): 3552 bytes

void Timer_Tick(Gadgeteer.Timer timer)
      {
         Debug.Print("Timer_Tick");

         // Stop the code being re entered just incase
         if (alreadyRunning)
         {
            return;
         }
         alreadyRunning = true;


         try
         {
            //using (HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "http://www.google.co.nz"))
            using (HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(@ "https://www.google.co.nz"))
            {
               request.Method = "GET";

               // Try different ways of loading certificates attached to the rewquest
               request.HttpsAuthentCerts = caCerts;
               //request.HttpsAuthentCerts = caCertsRO;
               //request.HttpsAuthentCerts = new X509Certificate[] { new X509Certificate(Resources.GetBytes(Resources.BinaryResources.GeoTrustRoot)) };

               // Enable these options to suit your environment
               //request.Proxy = new WebProxy("myproxy.myorganisation.com", true);
               //request.Credentials = new NetworkCredential("myusername", "mytopsecretpassword"); 

               using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
               {
                  if (response.StatusCode == HttpStatusCode.OK)
                  {
                     PulseDebugLED();
                  }
                  Debug.Print(response.StatusDescription);
               }
            }
            Debug.Print("Memory: " + Microsoft.SPOT.Debug.GC(true).ToString());
         }
         catch (WebException we)
         {
            Debug.Print(we.Message);
         }
         finally
         {
            alreadyRunning = false;
         }
      }

The complete project is available from (http://devmobilenz.files.wordpress.com/2013/03/httpclientsample.zip) if anyone is interested.

Looks like a bug.