C # Call the API to adjust the physical memory occupied by the application

Source: Internet
Author: User

After the. net application is started, it will occupy a large amount of physical memory. Some customers cannot tolerate this situation, so virtual memory should be used to replace the physical memory when necessary.
Reference
Using System. Runtime. InteropServices;

Specific Method:

[DllImport ("kernel32.dll")]
Private static extern bool SetProcessWorkingSetSize (
IntPtr process,
Int minSize,
Int maxSize
);

Private static void FlushMemory ()
{
GC. Collect ();
GC. WaitForPendingFinalizers ();
If (Environment. OSVersion. Platform = PlatformID. Win32NT)
SetProcessWorkingSetSize (System. Diagnostics. Process. GetCurrentProcess (). Handle, 20, 20 );
}

The process parameter is the process handle.
MinSize min Memory Page
MaxSize maximum Memory Page

After this method is executed, the memory usage is adjusted only once, and the physical memory usage will continue. It must be executed after large-scale memory operations.
Note: This method may result in performance loss. If you have high performance requirements, discard this method.

SetProcessWorkingSetSize

TheSetProcessWorkingSetSizeFunction sets the minimum and maximum working set sizes for the specified process.

 

BOOL SetProcessWorkingSetSize(HANDLE hProcess,  SIZE_T dwMinimumWorkingSetSize,  SIZE_T dwMaximumWorkingSetSize);

 

Parameters
HProcess
[In] Handle to the process whose working set sizes is to be set.

The handle must have the PROCESS_SET_QUOTA access right. For more information, see Process Security and Access Rights.

DwMinimumWorkingSetSize
[In] Minimum working set size for the process, in bytes. The virtual memory manager attempts to keep at least this much memory resident in the process whenever the process is active.

This parameter must be greater than zero but less than or equal to the maximum working set size. the default size is 50 pages (for example, this is 204,800 bytes on systems with a 4 K page size ). if the value is greater than zero but less than 20 pages, the minimum value is set to 20 pages.

If bothDwMinimumWorkingSetSizeAndDwMaximumWorkingSetSizeHave the value (SIZE_T)-1, the function temporarily trims the working set of the specified process to zero. This essential swaps the process out of physical RAM memory.

DwMaximumWorkingSetSize
[In] Maximum working set size for the process, in bytes. the virtual memory manager attempts to keep no more than this much memory resident in the process whenever the process is active and available memory is low.

This parameter must be greater than or equal to 13 pages (for example, 53,248 on systems with a 4 K page size ), and less than the system-wide maximum (number of available pages minus 512 pages ). the default size is 345 pages (for example, this is 1,413,120 bytes on systems with a 4 K page size ).

If bothDwMinimumWorkingSetSizeAndDwMaximumWorkingSetSizeHave the value (SIZE_T)-1, the function temporarily trims the working set of the specified process to zero. This essential swaps the process out of physical RAM memory.

Return Values

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. Call To obtain extended error information.

Remarks

The working set of a process is the set of memory pages currently visible to the process in physical RAM memory. these pages are resident and available for an application to use without triggering a page fault. the minimum and maximum working set sizes affect the virtual memory paging behavior of a process.

The working set of the specified process can be emptied by specifying the value (SIZE_T)-1 for both the minimum and maximum working set sizes.

If the values of eitherDwMinimumWorkingSetSizeOrDwMaximumWorkingSetSizeAre greater than the process 'current working set sizes, the specified process must have the SE_INC_BASE_PRIORITY_NAME privilege. users in the Administrators and Power Users groups generally have this privilege. for more information about security privileges, see .

The operating system allocates working set sizes on a first-come, first-served basis. for example, if an application successfully sets 40 megabytes as its minimum working set size on a 64-megabyte system, and a second application requests a 40-megabyte working set size, the operating system denies the second application's request.

UsingSetProcessWorkingSetSizeFunction to set an application's minimum and maximum working set sizes does not guarantee that the requested memory will be reserved, or that it will remain resident at all times. when the application is idle, or a low-memory situation causes a demand for memory, the operating system can reduce the application's working set. an application can use Function to lock ranges of the application's virtual address space in memory; however, that can potentially degrade the performance of the system.

When you increase the working set size of an application, you are 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 lead to failures of operations that require physical memory to be present; for example, creating processes, threads, and kernel pool. thus, you must useSetProcessWorkingSetSizeFunction carefully. You must always consider the performance of the whole system when you are designing an application.

Requirements

Client Requires Windows XP, Windows 2000 Professional, or Windows NT Workstation 3.5 and later.
Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server 3.5 and later.
Header

Declared in Winbase. h; include Windows. h.

Library

Link to Kernel32.lib.

DLL Requires Kernel32.dll.
See Also

GetProcessWorkingSetSize, Process and Thread Functions, Process Working Set, Processes,SetProcessWorkingSetSizeEx,

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.