在最小化窗口时,为什么内存占用率可以减少那么多

jmz2010 2009-08-09 12:24:26
Delphi程序一启动占用很多内存,最小化后占内存剧减,再还原,内存增大,但始终大不到开始运行的时候那么
大,请问这是为什么?
举个例子来说:一程序启动的时候点内存5M,最小化后800K,再还原时变成1.5M,怎么也大不到5M了。

请问:在最小化窗口时,为什么内存占用率可以减少那么多,并且再次还原窗口后,内存也没有刚启动程序时占的大了?到底最小化窗口时,执行了什么清理内存的代码,可不可以把最小化时Delphi清理内存的代码单独写出来供我们使用,以减小程序内存占用率吗?
...全文
585 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
射天狼 2009-08-10
  • 打赏
  • 举报
回复
这是Windows的机制,与Delphi程序无关,系统中的程序都是这样。

天狼工作室 http://www.j2soft.cn/
jmz2010 2009-08-10
  • 打赏
  • 举报
回复
当程序切换到非活动状态,操作系统会自动减少其占用的物理内存。总的内存占用量是不会减少的(减少的部分都转移到页面文件中去了)。

这个我不同意,因为我测试了,把窗口焦点从当前窗口移到别的窗口后,当前窗口的内存并没有显示减少。在资源管理器中。只有当最小化了程序,才能看出当前窗口内存减少了许多。
jmz2010 2009-08-10
  • 打赏
  • 举报
回复
窗口最小化时,Delphi真的是调用了SetProcessWorkingSetSize 吗,还是Windows系统调用的,被你说晕了?
pathletboy 2009-08-09
  • 打赏
  • 举报
回复
因为最小化时候系统调用了
SetProcessWorkingSetSize

The SetProcessWorkingSetSize function sets the minimum and maximum working set sizes for a specified process.

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 size of the working set of a process is specified in bytes. The minimum and maximum working set sizes affect the virtual memory paging behavior of a process.

BOOL SetProcessWorkingSetSize(

HANDLE hProcess, // open handle to the process of interest
DWORD dwMinimumWorkingSetSize, // specifies minimum working set size
DWORD dwMaximumWorkingSetSize // specifies maximum working set size
);


Parameters

hProcess

An open handle to the process whose working set sizes will be set.
Windows NT: The handle must have PROCESS_SET_QUOTA access rights. For more information, see Process Objects.

dwMinimumWorkingSetSize

Specifies a minimum working set size for the process. The virtual memory manager attempts to keep at least this much memory resident in the process whenever the process is active.
If both dwMinimumWorkingSetSize and dwMaximumWorkingSetSize have the value 0xffffffff, the function temporarily trims the working set of the specified process to zero. This essentially swaps the process out of physical RAM memory.

dwMaximumWorkingSetSize

Specifies a maximum working set size for the process. The virtual memory manager attempts to keep no more than this much memory resident in the process whenever the process is active and memory is in short supply.
If both dwMinimumWorkingSetSize and dwMaximumWorkingSetSize have the value 0xffffffff, the function temporarily trims the working set of the specified process to zero. This essentially 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 GetLastError to obtain extended error information.

Remarks

The working set of the specified process can be emptied by specifying the value 0xffffffff for both the minimum and maximum working set sizes.
If the values of either dwMinimumWorkingSetSize or dwMaximumWorkingSetSize are 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 Privileges.

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

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.
lhy 2009-08-09
  • 打赏
  • 举报
回复
可能是显示部分的内存释放了好多。
jmz2010 2009-08-09
  • 打赏
  • 举报
回复
原来如此,呵呵。还有别的意见和办法吗
heikeyanxi 2009-08-09
  • 打赏
  • 举报
回复
任务管理器显示的是物理内存占用量,不包括交换到页面文件的内存.当程序切换到非活动状态,操作系统会自动减少其占用的物理内存。总的内存占用量是不会减少的(减少的部分都转移到页面文件中去了)。

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧