非托管 VC++ 应用程序在 Windows 服务器上的内存消耗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unmanaged VC++ Application's memory consumption on windows server
提问by uzbones
OK, so I have a very large multi-threaded unmanaged c++ application (server) that runs on a windows 2003 server. It hosts sessions for 20-50 concurrent users doing all sorts of business logic... At times it can be using a very large amount of memory due to things like object/session caching due to users having large numbers of windows open in the clients (each window has a separate server 'session'.
好的,所以我有一个在 Windows 2003 服务器上运行的非常大的多线程非托管 C++ 应用程序(服务器)。它为 20-50 个执行各种业务逻辑的并发用户托管会话......有时,由于用户在客户端中打开了大量窗口,因此可能会由于对象/会话缓存等原因而使用非常大量的内存(每个窗口都有一个单独的服务器“会话”。
We routinely see consumption of more than 5-600 MB physical memory and 5-600 MB of virtual memory. Once it gets to this point we seem to start having 'out of memory' errors.
我们经常看到超过 5-600 MB 的物理内存和 5-600 MB 的虚拟内存的消耗。一旦达到这一点,我们似乎开始出现“内存不足”错误。
Now I know that Windows will start page-faulting when it feels it needs to free up physical memory, and also that win32 applications normally are only able to allocate up to a maximum of 4GB worth of memory, really only with 2GB of that available for actual use by the application for 'user-mode' address space, and even less of that after other libraries are loaded... I'm not sure if the 'user-mode' memory usage is what is reported on the Task Manager...
现在我知道 Windows 会在感觉需要释放物理内存时启动页面错误,而且 win32 应用程序通常只能分配最多 4GB 的内存,实际上只有 2GB 可用于应用程序对“用户模式”地址空间的实际使用,在加载其他库后甚至更少......我不确定“用户模式”内存使用情况是否是任务管理器上报告的内容。 ..
So anyway my real question is:
所以无论如何我真正的问题是:
How can I find out how much user-mode memory my application has access to, and how much has been used at any given time (preferably from outside of the application, i.e. some windows management tool)?
如何找出我的应用程序可以访问多少用户模式内存,以及在任何给定时间(最好从应用程序外部,即某些 Windows 管理工具)使用了多少内存?
[edit] After looking at the Process Explorer and the website, it looks like the value 'Virtual Size' is the value of how much memory the application has access to.
[编辑] 查看进程资源管理器和网站后,“虚拟大小”值似乎是应用程序可以访问的内存量的值。
回答by Mick
Sounds like a case for Process Explorer, a free utility from Microsoft SysInternals:
听起来像是Microsoft SysInternals 的免费实用程序 Process Explorer的案例:
(source: microsoft.com)
(来源:microsoft.com)
Description:
描述:
Ever wondered which program has a particular file or directory open? Now you can find out. Process Explorer shows you information about which handles and DLLs processes have opened or loaded.
The Process Explorer display consists of two sub-windows. The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that Process Explorer is in: if it is in handle mode you'll see the handles that the process selected in the top window has opened; if Process Explorer is in DLL mode you'll see the DLLs and memory-mapped files that the process has loaded. Process Explorer also has a powerful search capability that will quickly show you which processes have particular handles opened or DLLs loaded.
The unique capabilities of Process Explorer make it useful for tracking down DLL-version problems or handle leaks, and provide insight into the way Windows and applications work.
有没有想过哪个程序打开了特定的文件或目录?现在你可以知道了。进程资源管理器向您显示有关哪些句柄和 DLL 进程已打开或加载的信息。
Process Explorer 显示由两个子窗口组成。顶部窗口始终显示当前活动进程的列表,包括其拥有帐户的名称,而底部窗口中显示的信息取决于 Process Explorer 所处的模式:如果它处于句柄模式,您将看到处理在顶部窗口中选择的进程已经打开;如果进程资源管理器处于 DLL 模式,您将看到进程已加载的 DLL 和内存映射文件。Process Explorer 还具有强大的搜索功能,可以快速显示哪些进程打开了特定句柄或加载了 DLL。
Process Explorer 的独特功能使其可用于跟踪 DLL 版本问题或处理泄漏,并提供对 Windows 和应用程序工作方式的洞察。
If you are looking for more info in terms of terminal-server specific info, I've been following the blog of a programmer that is releasing a beta of a tool that I believe will fit your needs perfectly. It is an improved TSAdmin. He calls it TSAdminEx.
如果您正在寻找有关终端服务器特定信息的更多信息,我一直在关注一个程序员的博客,该博客发布了一个我认为将完美满足您需求的工具的测试版。它是一个改进的 TSAdmin。他称之为 TSAdminEx。
See below for a screenshot, and click here to learn more about it and to get the beta. It's free software, BTW.
请参阅下面的屏幕截图,并单击此处了解更多信息并获取测试版。这是免费软件,顺便说一句。
回答by sivabudh
I know you asked for preferably from outside of the application, but I was googling for how to find such information from within my own program and stumbled upon your post. So, this is to benefit people who want this information from withintheir program.
我知道你最好从应用程序外部要求,但我在谷歌上搜索如何从我自己的程序中找到这些信息并偶然发现了你的帖子。所以,这是受益谁想要从这个信息的人之内他们的计划。
unmanaged C++
非托管 C++
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
void PrintMemoryInfo( DWORD processID )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE,
processID );
if (NULL == hProcess)
return;
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
printf( "\tYour app's PEAK MEMORY CONSUMPTION: 0x%08X\n",
pmc.PeakWorkingSetSize );
printf( "\tYour app's CURRENT MEMORY CONSUMPTION: 0x%08X\n", pmc.WorkingSetSize );
printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakPagedPoolUsage );
printf( "\tQuotaPagedPoolUsage: 0x%08X\n",
pmc.QuotaPagedPoolUsage );
printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaPeakNonPagedPoolUsage );
printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n",
pmc.QuotaNonPagedPoolUsage );
printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage );
printf( "\tPeakPagefileUsage: 0x%08X\n",
pmc.PeakPagefileUsage );
}
CloseHandle( hProcess );
}
int main( )
{
PrintMemoryInfo( GetCurrentProcessId() );
return 0;
}
回答by Mick
You wrote:
你写了:
When your talking how much memory a win32 app can access they specifically call it 'user-mode' memory which I don't see as an option or at least I don't know what column it really is.
当您谈论 win32 应用程序可以访问多少内存时,他们专门将其称为“用户模式”内存,我不认为这是一个选项,或者至少我不知道它到底是什么列。
Have a look at this article(written by the creator of Process Explorer, Dr. Mark Russinovich).
看看这篇文章(由 Process Explorer 的创建者 Mark Russinovich 博士撰写)。
To be able to manage your Windows systems effectively you need to understand how Windows manages physical resources, such as CPUs and memory, as well as logical resources, such as virtual memory, handles, and window manager objects. Knowing the limits of those resources and how to track their usage enables you to attribute resource usage to the applications that consume them, effectively size a system for a particular workload, and identify applications that leak resources.
为了能够有效地管理您的 Windows 系统,您需要了解 Windows 如何管理物理资源(例如 CPU 和内存)以及逻辑资源(例如虚拟内存、句柄和窗口管理器对象)。了解这些资源的限制以及如何跟踪它们的使用情况使您能够将资源使用归因于使用它们的应用程序,有效地为特定工作负载调整系统大小,并识别泄漏资源的应用程序。