C # program memory allocation

Source: Internet
Author: User

C # program memory allocation

The amount of memory that the process can use is the maximum address assigned to the process by the operating system, and the general 32-bit operating system gives the user the maximum address of 3g(the operating system retains its own 1g), and Windows provides only a 2g address for the individual user for commercial purposes. The system configuration needs to be modified to use the 3g extension. There is an Intel 32-bit chip that actually supports 36-bit addressing (64G), but also for commercial purposes this feature is banned for most personal chips, and this prohibition may be physically limited to individual users who can hardly modify it.

And there is a difference between the physical available memory in the operating system and the actual memory consumption of the process, because there is the concept of virtual address and swap area, the process actually uses much more memory than the physical available memory, the simplest example is declaring a very large array but not storing anything, The physical memory consumed after the process starts may be small but the requested memory address is very large (the number of virtual memory), so it is possible to create a situation where the physical memory of the free physical memory is small but the memory is insufficient, because the process available address is fully allocated (many applications are useless.) So to Jit,lazy ...).

Sometimes, because of the problem with the code (rather than understanding the framework's memory usage mechanism) can also lead to outofmemory, the general use of large memory is the list and the HASHTABLE2 structure, and these 2 data structures are actually used as containers to store the elements of the , because the array is a fixed-length structure, so when the time limit to achieve dynamic expansion, dynamic expansion of the algorithm is twice times the current array length, the array itself is required for continuous memory as a guarantee, if too much memory fragmentation will cause no continuous memory available. NET's smart garbage collector has no way to completely avoid memory fragmentation (you can avoid fragmentation by preventing garbage collection or reducing the frequency of garbage collection, but you need to manually recycle to solve memory growth issues), so it's best to specify the maximum capacity limit in advance when using List or Hashtable. Avoid causing OutOfMemory to the back because of fragmentation problems. For example, the initial memory block is not large enough, later to allocate more memory, in the memory area behind this block is not so large contiguous free memory, it will be outofmemory.

Subtotal: Memory allocation for C # list

When the number of item elements of the List<t> object exceeds the number of capacity, the,list<t> object will re-request a chunk size of twice times the original capacity memory space, The current item element and the element to be added are then copied into the new memory space.

Knowing the principle of memory allocation, the next step is to use the optimal method according to these principles to ensure that the limited memory space can be used rationally. The main points to be summed up are as follows:
1. When instantiating an List<t> object, if you can predict the approximate number of its item element, you should set its capacity value to a minimum value close to the number of item elements when instantiating a List<t> object. This avoids the constant application of memory and element duplication when adding elements like list<t>.

2. When the number of item elements is much smaller than capacity due to the constant call to the Remove method, it can result in a waste of memory. You can now call the TrimExcess method to free up excess memory.

C # thread Pool http://wenku.baidu.com/view/a6f14375f46527d3240ce01b.html

=====================================

Chat question:memory Limits for 32-bit and 64-bit processes

During Our recent blog chat, there were a number of the 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 Runnin  G on. For 32-bit It also depends if the use is THE/3GB switch or not.

So I broke this and based on these things to the table below, this is the maximum amount of memory available for the proc ESS assuming you has that much RAM and pagefile space.

&NBSP;

32-bit os

64-bit OS

32-bit process

2 GB

4 GB

32-bit process WITH/3GB

3 GB

n/a

64-bit process

n/a

16 TB

These process numbers is contingent on what much RAM and disk space you have 4 GB of RAM (memory) and 4 GB Page File (virtual memory), the total memory of all running processes can ' t is greater then 8 GB.

Note:if you let Windows manage your pagefile size, when you hit this limit, Windows would try to grow your pagefile as Lon G as there is disk space available.

For the amount a. NET application would use before we can expect to see out of memory, those numbers is:

32-bit OS

64-bit OS

32-bit process

800-1200 MB

2800 MB

32-bit Process WITH/3GB

1800 MB

N/A

64-bit process

N/A

2800 MB If using a 4 GB process or more if more RAM (around 70% of RAM + pagefile)

Keep in mind that although a. NET process can grow this large, if the process was multiple GB in size, it can become very d  Ifficult for the Garage Collector to keep up with the memory as Generation 2 would become very large. I ' ll talk about the generations more in an upcoming post.

Hopefully that'll clear up how much memory each one uses.

Keep checking the recap-asp.net Blog Chat to see other topics that Tess or I write about.

Current Label: C # C # implements Golang's channelVisionwang 2013-12-03 21:18 read: 398 comments: 0. The GC summary in netVisionwang 2013-10-15 21:54 read: 165 comments: 0Cannot accurately sort by Pinyin in LINQVisionwang 2013-10-14 16:37 read: 42 comments: 0C # program memory allocationVisionwang 2013-04-24 23:08 read: 3126 comments: 0Overview of connection pooling under the. NET PlatformVisionwang 2012-11-16 23:34 read: 187 comments: 0

C # program memory allocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.