Modify the process to occupy memory SetProcessWorkingSetSize function (many related articles worth learning)

Source: Internet
Author: User

Physical memory and virtual memory

Physical memory, in the application, naturally, as the name implies, physically, the actual memory inserted on the board is how big is how big. Looking at the machine configuration, this is the physical memory.

If you execute large or many programs, you will run out of physical memory. To solve this problem, Windows uses virtual memory technology, that is, to take out some of the hard disk space to act as memory, when the memory is exhausted, the computer will automatically call the hard disk to act as memory to alleviate the memory tension.

A program that inevitably uses virtual memory, because code that is not executed frequently or has not been executed for a long time, does not have to remain in physical memory, but is wasted, placed in virtual memory, and then transferred out when executing this part of the code.
The task Manager of Windows helps us to see the virtual memory of the process. Bring up Task Manager, click the menu "View"-"select column", in the window that appears, tick "virtual memory size

How much virtual memory should a program use? It doesn't have to be, but it should be best for the right fit with virtual memory.
The following will expose a program that looks like a lot of images, lots of running libraries, and why it "consumes" less than 1 MB of memory.

Turns out to be the SetProcessWorkingSetSize function

In the project to optimize the performance of the program, found that the SetProcessWorkingSetSize () method to reduce memory a lot, so consulted the relevant information as follows.

How a SetProcessWorkingSetSize works

Following from: http://blog.csdn.net/zlt982001/archive/2005/08/28/466879.aspx

So why would my program be able to move memory to virtual memory?

In fact, you can also, try to minimize a program to the taskbar, and then see the task Manager, see no, your program occupies a sudden decrease in the actual memory, it does not seem that I have a way to compress memory, but the operating system itself has this mechanism, that is, when the program is not used (minimized), The operating system calls some commands to move the memory used by the program to virtual memory, leaving only a fraction of the regular code

So we see this situation, and the memory is shrinking.

So: What commands does the system call? Can you release the memory without shrinking the form?

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

and the desktop calendar show is able to always keep the minimum memory, is because the use of the timer, keep doing the operation, so the performance can be imagined, although in exchange for the illusion of small memory, the system is indeed a disaster.

Of course, the function is not without a single virgin,

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. VB especially

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.

Finally, attach the API code called by VB

Option Explicit
Private Declare Function setprocessworkingsetsize Lib "kernel32" (ByVal hprocess as Long, ByVal dwminimumworkingsetsize as Long, ByVal dwmaximumworkingsetsize as long) as long
Private Declare Function getcurrentprocess Lib "kernel32" () as Long

SetProcessWorkingSetSize GetCurrentProcess,-1,-1

Put the memory used by the current process to 0, please place it in the appropriate location

Two distinguish between physical memory, virtual memory, working Set (memory), 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:  answer this question is not easy. 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 a certain time, the physical memory that a process can directly access (without a fault of the pages) is called the working Set of the process, whereas a process that is actually allocated (commit) from the 4G address space 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 USAG E The 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 has 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 much easier and more accurate to use Perfomence monitor with the. NET related counter.

Http://www.xuebuyuan.com/244651.html

Http://www.cnblogs.com/kex1n/p/4043901.html

Http://www.yunsec.net/a/security/advanced/hacker/2010/0511/3836.html

Http://www.cnblogs.com/chasetheexcellence/archive/2012/11/09/os_SetProcessWorkingSetSize.html

Modify the process to occupy memory SetProcessWorkingSetSize function (many related articles worth learning)

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.