C#程序最大可使用的内存
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#程序最大可使用的内存
进程可使用内存数就是操作系统给进程分配的最大地址,一般的32位操作系统提供给用户地址最大都是3g(操作系统自己保留1g),windows由于商业目的,对于个人用户只提供了2g地址,要使用3g 扩展需要对系统配置作修改。
还有就是intel的32位芯片实际上是支持36位寻址能力的(64g),但是也是出于商业目的对于大部分个人芯片都禁止了这个功能,这个禁止可能是物理上的限制个人用户几乎无法修改。
而且在操作系统中物理可用内存和进程实际占用内存是有区别的,因为有虚拟地址和交换区这种概念,进程实际使用的内存量远远大于物理可用内存,最简单的一个例子就是声明一个非常大的数组但不存放任何东西,进程启动后占用的物理内存可能很小但是申请的内存地址却是非常大了(虚拟内存数),所以就可能产生这么一种情况:空闲物理内存很多进程所占物理内存很小但却报了内存不够,因为进程可用地址已经完全被分配完了(申请很多,却没有用。
所以要JIT,lazy...)。
====================================
有些时候由于代码的问题(确切地说是不了解framework的内存使用机制)也会导致outofmemory,一般占用大内存的情况就是list 和hashtable2种结构,而这2种数据结构实际上都是使用数组作为容器存放元素的,由于数组是定长结构所以当达到上限时需要做动态扩容,动态扩容的算法都是2倍当前数组长度,数组本身又是需要连续内存作为保证,如果内存碎片过多就会导致没有连续内存可用,.net 的智能垃圾回收器也没办法完全避免内存碎片(可以通过禁止垃圾回收或者降低垃圾回收频率来避免碎片,但是需要手工回收来解决内存增长问题),所以在使用list或者hashtable的时候最好能事先指定需要的最大容量上限,避免到后面因为碎片问题导致outofmemory。
比如申请的初始内存块不够大,后期要分配更大内存,在本块内存区后面没有这么大的连续空闲内存,就会outofmemory。
=====================================
Chat Question: Memory Limits for 32-bit and 64-bit processes
During our recent blog chat, there were a number of topics that were asked about and I am going to expand on some of them. The first one is the memory limits for different processes.
This really depends on a few different things. The architecture of the process (32-bit or 64-bit) and also the architecture of the Operating System the process is running on. For 32-bit it also depends if you use the /3GB switch or not.
So I broke this up based on these things into the table below, this is the maximum amount of memory available for the process assuming you have that much RAM and Pagefile space.
These process numbers are contingent on how much RAM and disk space you have, so if you have 4 GB of RAM(内存) and 4 GB Pagefile(虚拟内存), the total memory of all running processes can’t be gre ater then 8 GB.
Note: If you let Windows manage your Pagefile size, when you hit this limit, Windows will try to grow your Pagefile as long as there is disk space available.
For the amount a .NET application will use before we can expect to see out of memory, those numbers are:
Keep in mind that although a .NET process can grow this large, if the process is multiple GB in size, it can become very
difficult for the Garage Collector to keep up with the memory as Generation 2 will become very large. I’ll talk about the generations more in an upcoming post.
Hopefully that will clear up how much memory each one uses.
Keep checking the RECAP- Blog Chat to see other topics that Tess or I write about.。