{"id":724,"date":"2013-09-12T14:53:11","date_gmt":"2013-09-12T14:53:11","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=724"},"modified":"2014-01-26T12:38:51","modified_gmt":"2014-01-26T12:38:51","slug":"memory-limits-for-a-32-bit-net-application","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/memory-limits-for-a-32-bit-net-application\/","title":{"rendered":"Memory limits for a 32-bit .NET application"},"content":{"rendered":"<p>I&#8217;m currently working on an application which is deployed to Windows XP (yes some corporates are still on XP), so whilst I hope this post will be useless soon, I still currently need to worry about memory usage on a 32-bit machine.<\/p>\n<p>Whilst Windows XP has a 4GB memory limit, not all of this is available to your application. An application\/process on a 32-bit version of windows has 2GB of available memory to use (if using the \/3G switch on Windows Server 2003 then you can get 3GB). You may start to see Out of Memory exceptions (obviously depending upon what you&#8217;re trying to allocate and garbage collection etc.) at around 1.2GB.<\/p>\n<p>Whilst this application is currently running on a 32-bit version of Windows it may move to a 64-bit version soon. If it has to remain as a 32-bit application it will have double the amount of memory available. So a 32-bit application on a 64-bit version of Windows will have 4GB available and Out of memory exceptions may occur in the region of 2.8GB memory usage.<\/p>\n<p>Here&#8217;s a little bit of code that allows us to get the memory usage for a process<\/p>\n<p>public class MemoryInformation<br \/>\n{<br \/>\n   [DllImport(&#8220;KERNEL32.DLL&#8221;)]<br \/>\n   private static extern int OpenProcess(uint dwDesiredAccess,<br \/>\n                                   int bInheritHandle, uint dwProcessId);<br \/>\n   [DllImport(&#8220;KERNEL32.DLL&#8221;)]<br \/>\n   private static extern int CloseHandle(int handle);<\/p>\n<p>   [StructLayout(LayoutKind.Sequential)]<br \/>\n   private class PROCESS_MEMORY_COUNTERS<br \/>\n   {<br \/>\n      public int cb;<br \/>\n      public int PageFaultCount;<br \/>\n      public int PeakWorkingSetSize;<br \/>\n      public int WorkingSetSize;<br \/>\n      public int QuotaPeakPagedPoolUsage;<br \/>\n      public int QuotaPagedPoolUsage;<br \/>\n      public int QuotaPeakNonPagedPoolUsage;<br \/>\n      public int QuotaNonPagedPoolUsage;<br \/>\n      public int PagefileUsage;<br \/>\n      public int PeakPagefileUsage;<br \/>\n   }<\/p>\n<p>   [DllImport(&#8220;psapi.dll&#8221;)]<br \/>\n   private static extern int GetProcessMemoryInfo(int hProcess,<br \/>\n                        [Out] PROCESS_MEMORY_COUNTERS counters, int size);<\/p>\n<p>   public static long GetMemoryUsageForProcess(long pid)<br \/>\n   {<br \/>\n      long mem = 0;<br \/>\n      int pHandle = OpenProcess(0x0400 | 0x0010, 0, (uint)pid);<br \/>\n      try<br \/>\n      {<br \/>\n         PROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS();<br \/>\n         if (GetProcessMemoryInfo(pHandle, pmc, 40) != 0)<br \/>\n            mem = pmc.WorkingSetSize;<br \/>\n      }<br \/>\n      finally<br \/>\n      {<br \/>\n         CloseHandle(pHandle);<br \/>\n      }<br \/>\n      return mem;<br \/>\n   }<\/p>\n<p>   public static string ToString(long numOfBytes)<br \/>\n   {<br \/>\n      double bytes = numOfBytes;<br \/>\n      if (bytes < 1024)\n         return bytes.ToString();\n\n      bytes \/= 1024;\n      if (bytes < 1024)\n      {\n         return bytes.ToString(\"#.# KB\");\n      }\n      else\n      {\n         bytes \/= 1024;\n         if (bytes < 1024)\n         {\n            return bytes.ToString(\"#.# MB\");\n         }\n         else\n         {\n            bytes \/= 1024;\n            return bytes.ToString(\"#.# GB\");\n         }\n      }\n   }\n\n   public static string GetFormattedMemoryUsageForProcess(long pid)\n   {\n      return ToString(GetMemoryUsageForProcess(pid));\n   }\n}\n[\/code]\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently working on an application which is deployed to Windows XP (yes some corporates are still on XP), so whilst I hope this post will be useless soon, I still currently need to worry about memory usage on a 32-bit machine. Whilst Windows XP has a 4GB memory limit, not all of this is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[53],"tags":[],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-windows"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":1193,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/724\/revisions\/1193"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}