I wrote it once before. I know it may be because the Session of the website has expired for a long time. After the submission, I have to log on again. After I log on, I will not be able to log on again, i'm crying. I have no school draft, and the statements are not fluent. Please forgive me a lot.
I have been busy working on projects recently, so I have no time to write a blog. Fortunately, the project is almost finished, and the mood is better. Yesterday we performed a large areaProgramTesting, in addition to some small bugs, the overall project is still satisfactory to us. But then we found that this project was actually "eat" the memory, which made us very depressed, so long time effort will not be wasted, right, it is important to know the performance of a system. Then we will monitor the memory for this project.
Note:Presentationhost.exe Process[Windows Presentation Foundation (WPF) host (presentationhost.exe) is an application that enables WPF applications to be hosted in compatible browsers (including Microsoft Internet Explorer 6 and later. (From msdn)]
At the beginning, the memory usage was 178628 kb.
I don't need to talk about the difference between and K in half an hour.
Afterwards, we discussed this issue and found that N + methods were not satisfactory. I saw a method on the Internet at noon and we decided to try it. The following occurs in APP. XAML:Code.
Code
[Dllimport ( " Kernel32.dll " )]
Public Static Extern Bool Setprocessworkingsetsize (intptr proc, Int Min, Int Max );
Public Void Fulshmemor ()
{
If (Environment. osversion. Platform = Platformid. win32nt)
Setprocessworkingsetsize (system. Diagnostics. process. getcurrentprocess (). handle, - 1 , - 1 );
}
Public APP ()
{
This . Exit + = New Exiteventhandler (app_exit );
VaR Timer = New Dispatchertimer {Interval = Timespan. fromminutes ( 20 };
Timer. tick + = (S, E) => Fulshmemor ();
Timer. Start ();
}
In applications, some functions are often used to release memory. In fact, the memory operation functions should be used with caution. For example, the setprocessworkingsetsize that people often think of is actually for Windows, the system will automatically release the memory when the program is idle (such as the program is minimized). When you release the memory, it will often cause some inexplicable memory errors, this causes unstable applications and systems.
Look at this API setprocessworkingsetsize
This is the original answer from msdn.
Using the setprocessworkingsetsize function 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 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 runtime space of the application, and only reserve the required memory. When the application is idle or the system memory is too low, the operating system will automatically call this mechanism to set the application memory. Applications can also use virtuallock to lock a certain range of memory from being released by 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 use the setprocessworkingsetsize function carefully. you must always consider the performance of the whole system when you are designing an application.
When you increase the runtime space for applications, the physical memory you can obtain depends on the system, which may cause other applications to degrade performance or the overall system performance, this may also cause the request to the physical memory operation to fail. For example, to create a process, thread, or kernel pool, you must use this function with caution.
The program executes memory release every 20 minutes. If the memory usage in 20 minutes is 301192k, the effect is obvious.
Let's take a look at some parameters of performance indicators. Lao Tzu TM went crazy at once. Although the presentationhost.exe program is contracted, the virtual memory is not released.
In fact, using this function does not improve performance or actually save memory.
Because it only temporarily moves the memory occupied by the application to the virtual memory. Once the application is activated or has an operation request, the memory will be occupied again. If you use this method to set the memory occupied by the program, the system performance may be reduced to some extent because the system needs to frequently perform page exchanges between the memory and the hard disk.
Bool setprocessworkingsetsize (
Handle hprocess,
Size_t dwminimumworkingsetsize,
Size_t dwmaximumworkingsetsize
);
Set the two size_t parameters to-1, that is, the memory used by the process can be switched to the virtual memory, only a small part of the code is retained.
The reason why the table calendar show can always keep the Minimum Memory is that the timer is used and the operation is not stopped. Therefore, the performance can be imagined. Although it is in exchange for the illusion of small memory, it is indeed a disaster for the system.
Of course, this function is not all right,
1. When our application is just loaded, you can use this operation once to put the code not required in the loading process into the virtual memory. In this way, after the program is loaded, maintain a large amount of available memory. VB
2. When the program runs for a certain period of time or the program is about to be idle, you can use this command to swap the occupied memory to the virtual memory.
In the end, I cannot use GC to forcibly recycle memory after calling the API. Although the results have been understood, I do not recommend this method. If you have a good way to release WPF memory, forget to share it.