Windows Working Set memory

Source: Internet
Author: User

Windows Task Manager By default, the memory (private working Set) column is selected.


Memory-Working set: Is the sum of the amount of memory in a private working set and the amount of memory that the process is using and can be shared by other processes.
Memory-Peak Working set: is the maximum number of working set memory used by the process.
Memory-Working Set increment: is the amount of change in the working set memory used by the process.
Memory-Dedicated working set:-is a subset of the working set, which is a technical term that describes the amount of memory used by each process. A dedicated working set specifically describes the amount of memory that a process is using and cannot share with other processes.
Memory-Commit Size: Is the amount of virtual memory that is reserved for use by a process.
Memory-Paged Pool: is the amount of recognized virtual memory for a process that can be written to other storage media, such as a hard disk.
Memory-Non-paged pool: is the amount of recognized virtual memory for a process that cannot write to other storage media.


I. How the SetProcessWorkingSetSize works

Look at this API SetProcessWorkingSetSize

This is the exact words from MSDN.

Using the SetProcessWorkingSetSize function to set a application ' s minimum and maximum working set sizes does not guarant EE that the requested memory would be reserved, or that it'll remain resident at all times. When the application are idle, or a low-memory situation causes a demand for memory, the operating system can reduce the AP Plication ' s working set. An application can with the VirtualLock function to lock ranges of the application ' s virtual address space in memory; However, that can potentially degrade the performance of the system.

Use this function to set the minimum and maximum running space for the application, leaving only the required memory. When an application is idle or the system memory is too low, the operating system automatically calls this mechanism to set the application's memory. Applications can also use VirtualLock to lock a range of memory from being released by the system.

When you increase the working set size of a application, you is taking away physical memory from the rest of the system. This can degrade the performance of other applications and the system as a whole. It can also leads to failures of operations, require physical memory to be present; For example, creating processes, threads, and kernel pool. Thus, you must use the SetProcessWorkingSetSize function carefully. You must all consider the performance of the whole system when you were designing an application.

When you increase the running space to the application, the physical memory you are able to get depends on the system, which can cause other applications to degrade performance or the overall system performance degradation, which may also cause requests for physical memory operations to fail, such as: build processes, threads, kernel pools, you must use this function with care.

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

In fact, using this function does not improve performance, nor does it really save memory.

Because he's just temporarily moving the memory occupied by the application to virtual memory, which will be re-occupied once the application is activated or there is an action request. If you force this method to set the memory used by the program, it may decrease the system performance to some extent, because the system needs frequent paging between the memory and the hard disk.


BOOL SetProcessWorkingSetSize (
HANDLE hprocess,
size_t Dwminimumworkingsetsize,
size_t dwmaximumworkingsetsize
);


Set the 2 size_t parameter to 1, which allows the memory used by the process to be swapped to virtual memory, leaving only a fraction of the code

1. When our application has just been loaded, it can be used one time to put code that is not needed by the loading process into virtual memory, so that when the program is loaded, it will maintain a large amount of available memory.

2. You can use this command to swap memory to virtual memory when the program is running for a certain amount of time or when the program is about to be idle.

Two. Distinguish between physical memory, virtual memory, working Set (memory),

Following from: http://blog.joycode.com/qqchen/archive/2004/03/17/16434.aspx

This problem was met several times on the csdn, and every time I gave a simple answer: do not refer to the MEM Usage data of Task Manager, the size of that data has no direct impact on program performance.
Here are some ideas for me to analyze this question, hoping to help friends who are interested in this question.

Q:Is. NET Alone?
A:
nope! Saucer said before, this is not. NET problem, all Windows programs have similar behavior. For example, the following C program:
void Main {while (1);} Loop so we could see the task Manager
First run on my machine The Mem usage is 632K, the console is minimized and then recovered, and the mem usage becomes 36K. Obviously, this is not a. NET-specific issues, but Windows Memory management. So and. NET does not have much to do with the GC mechanism-although the form of the problem can easily be reminiscent of a GC.

Q:How much memory does my program use?
A:
It's not easy to answer this question. Let's take a look at some basic concepts of operating system virtual memory management: Each Windows process has a 4G address space, but your machine obviously doesn't have 4G of physical memory. In a multitasking environment, the total amount of memory used by all processes can exceed the physical memory of the computer. In a particular case, a part of the process may be deleted from physical memory and be persisted in the file of the hard disk (pagefile), when the process tries to access the memory that is swapped into the pagefile, the system will produce a page fault (fault), The Windows Memory Manager will then be responsible for re-entering the corresponding memory page from the hard disk into physical memory.
At some time, a process can directly access the physical memory (without a fault of the pages) called the working Set of the process, and a process from the address space of 4G is actually allocated (commit), accessible memory is called committed Virtual memory. Committed VMS may exist in page file, WorkingSet must be in physical memory.
So to answer the above questions, ask a question: What ' re you talking about? Physical memory or Committed memory?

Q:What is this "Mem Usage" data?
A:
 from Task Manager help:in Task Manager, the current working Set of a process, in kilobytes. 
Mem The usage name is somewhat misleading. It only represents the physical memory currently occupied by this process, which is workingset. WorkingSet does not represent all of the virtual memory that the process is currently "occupying", and the process may have a portion of the data being exchanged to pagefile. This data is loaded into physical memory only when it is accessed.
Task Manager has another column of data: VM Size, which represents a process-allocated virtual memory (Committed Visual Memory)-The actual definition is more complex than this, but this definition is sufficient for the problem we are currently analyzing. As an example of the preceding C program, the size of the VM before and after the minimization is 176K, and there is no change.
So, the conclusion is simple: When a Windows program is minimized, the Windows Memory Manager minimizes the workingset of the process (either based on FIFO or least recently with LRU), swapping most of the data into pagefile. This is easy to understand: we always want to make more physical memory available for foreground applications, which can provide better performance. When the program is restored from the minimized, Windows does not fully load all the virtual memory of the program, just loads the necessary parts. This is also easy to understand: The code of the program startup phase is usually rarely accessed after startup (yes. NET program, in particular, to a module such as fusion after the normal loading of the program if not used reflection usually not used.

Q:so, do we want a smaller workingset, or a larger one?
A:
 it depends. Conventional Wisdom tells Us:the smaller, the better. But it is not so simple in the problem of the virtual existence. If the workingset is too small, there will be a lot of missing pages during the program running, which can seriously affect the performance of the program. On the other hand, WorkingSet is wasting "valuable" physical memory, reducing the performance of the entire system. Typically (unless you are a performance-sensitive application and you have a good knowledge of the memory management of Windows), it is recommended that you do not resize the workingset yourself in your program, but instead hand this task to the Windows Memory manager. The method of adjustment saucer is mentioned:  setprocessworkingsetsize ();

Q:final Question, Does My program really occupy that much physical memory?
A:
the problem looks a little bit--that number is clearly written in Task Manager.
sam1111 The result of the Vadump check shows that the main reason for the decrease in process workingset is that many DLLs are not loaded into physical memory when they are restored from minimization. We know that one of the features of the DLL is code sharing, in NTDLL.DLL, for example, almost all applications of the entire Windows system (specifically, all programs of the WIN32 subsystem) need to refer to NTDLL.DLL, if each person, the light this file occupies a few 10 megabytes of memory. The solution for Windows is to save only one copy of NTDLL.DLL in physical memory, and all programs referencing this DLL map this copy to their own memory space, sharing the NTDLL.DLL snippet (the data segment of each process is still separate). So while the size of the NTDLL.DLL is calculated in the WorkingSet of your program, removing the reference to the DLL from your program does not really release much physical memory-you don't have to, others are still using it!
So, your program's "exclusive" physical memory is far from what mem usage says, and it needs to deduct a lot of the shared Code Page from the Mem Usage (vadump).

Conclusion? Do not refer to the Mem Usage data for Task Manager, which has no direct impact on program performance. It is easier and more accurate to use Perfomence monitor with the. NET related counter.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Windows Working Set memory

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.