{"id":38203,"date":"2004-08-10T07:00:00","date_gmt":"2004-08-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/08\/10\/myth-without-3gb-a-single-program-cant-allocate-more-than-2gb-of-virtual-memory\/"},"modified":"2004-08-10T07:00:00","modified_gmt":"2004-08-10T07:00:00","slug":"myth-without-3gb-a-single-program-cant-allocate-more-than-2gb-of-virtual-memory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040810-00\/?p=38203\/","title":{"rendered":"Myth: Without \/3GB a single program can&#8217;t allocate more than 2GB of virtual memory"},"content":{"rendered":"<p><P>Virtual memory is not virtual address space (part 2). <\/P>\n<P>This myth is being perpetuated <A href=\"http:\/\/channels.lockergnome.com\/it\/archives\/20040806_when_is_4gb_of_ram_not_really_4gb_of_ram.phtml\">even as I write this series of articles<\/A>. <\/P>\n<P>The user-mode virtual address space is normally 2GB, but that doesn&#8217;t limit you to 2GB of virtual memory. You can allocate memory without it being mapped into your virtual address space. (Those who grew up with <A href=\"http:\/\/www.pcguide.com\/ref\/ram\/umaEMS-c.html\">Expanded Memory<\/A> or other forms of bank-switched memory are well-familiar with this technique.) <\/P><PRE>HANDLE h = CreateFileMapping(INVALID_HANDLE_VALUE, 0,\n                             PAGE_READWRITE, 1, 0, NULL);\n<\/PRE>\n<P>Provided you have enough physical memory and\/or swap file space, that 4GB memory allocation will succeed. <\/P>\n<P>Of course, you can&#8217;t map it all into memory at once on a 32-bit machine, but you can do it in pieces. Let&#8217;s read a byte from this memory. <\/P><PRE>BYTE ReadByte(HANDLE h, DWORD offset)\n{\n SYSTEM_INFO si;\n GetSystemInfo(&amp;si);\n DWORD chunkOffset = offset % si.dwAllocationGranularity;\n DWORD chunkStart = offset &#8211; chunkOffset;\n LPBYTE pb = (LPBYTE*)MapViewOfFile(h, FILE_MAP_READ, 0,\n      chunkStart, chunkOffset + sizeof(BYTE));\n BYTE b = pb[chunkOffset];\n UnmapViewOfFile(pb);\n return b;\n}\n<\/PRE>\n<P>Of course, in a real program, you would have error checking and probably a caching layer in order to avoid spending all your time mapping and unmapping instead of actually doing work. <\/P>\n<P>The point is that virtual address space is not virtual memory. As we have seen earlier, <A href=\"http:\/\/weblogs.asp.net\/oldnewthing\/archive\/2003\/10\/07\/55194.aspx\">you can map the same memory to multiple addresses<\/A>, so the one-to-one mapping between virtual memory and virtual address space has already been violated. Here we showed that just because you allocated memory doesn&#8217;t mean that it has to occupy any space in your virtual address space at all. <\/P>\n<P>\n[Updated: 10:37am, fix minor typos reported in comments.]\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Virtual memory is not virtual address space (part 2). This myth is being perpetuated even as I write this series of articles. The user-mode virtual address space is normally 2GB, but that doesn&#8217;t limit you to 2GB of virtual memory. You can allocate memory without it being mapped into your virtual address space. (Those who [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[26],"class_list":["post-38203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Virtual memory is not virtual address space (part 2). This myth is being perpetuated even as I write this series of articles. The user-mode virtual address space is normally 2GB, but that doesn&#8217;t limit you to 2GB of virtual memory. You can allocate memory without it being mapped into your virtual address space. (Those who [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=38203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=38203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}