gpt4 book ai didi

delphi - 如何为我的应用程序保留内存并保留指定的剩余量?

转载 作者:行者123 更新时间:2023-12-03 14:46:19 31 4
gpt4 key购买 nike

我正在计划一个应用程序,该应用程序将涉及一次加载许多图片,因此需要大量内存。例如,我可能会同时创建 50 个图像对象,总共占用 1GB RAM。但是当用户要加载另外 20 张图片时,我想确保该内存量已被保留并准备就绪。

现在这部分看起来可能与正常情况有些倒退。我不需要指定我的应用程序应保留多少内存,而是需要指定为其他应用程序保留多少内存,并根据此规范定期调整我的应用程序的内存。我必须说我从来没有使用过保留内存,尤其是不知道如何保留剩余的可用内存。

例如,如果计算机有 2048 MB RAM,并且该选项设置为为其他应用程序保留 50 MB 可用空间,并且其他应用程序已经使用了 10MB RAM,那么它应该保留 2048-50 -10 = 1988 MB 用于我的应用程序。

我预见到的麻烦是假设用户打开另一个需要 1GB 的应用程序。我的应用程序必须捕捉到这一点并缩小自身。

这听起来像是一个可行的方法吗?基本上,我需要确保在任何给定时间保留尽可能多的内存,同时为其他应用程序留下相当多的可用内存。如果我这样做,会对性能产生重大影响,还是不会产生太大影响?我可能会快速加载和卸载图像,并且我不希望它按需保留/释放此内存,我希望它保持保留状态。

最佳答案

+1 Sertac 提到 SQL Server 如何分配它所需的内存,但在 Windows 提示时释放内存。

应用程序可以通过使用 CreateMemoryResourceNotification 接收 Window 的投诉:

hLowMemory := CreateMemoryResourceNotification(LowMemoryResourceNotification);

Applications can use memory resource notification events to scale the memory usage as appropriate. If available memory is low, the application can reduce its working set. If available memory is high, the application can allocate more memory.

Any thread of the calling process can specify the memory resource notification handle in a call to the QueryMemoryResourceNotification function or one of the wait functions. The state of the object is signaled when the specified memory condition exists. This is a system-wide event, so all applications receive notification when the object is signaled. Note that there is a range of memory availability where neither the LowMemoryResourceNotification or HighMemoryResourceNotification object is signaled. In this case, applications should attempt to keep the memory use constant.

但还值得一提的是,您也可以分配您需要的内存。您的操作系统有一组非常复杂的算法,可以在内存压力较高时交换出最少使用的内存。您只需分配所需的所有内存即可利用这一点。当 Windows 开始运行速度较低时,它会找到使用最少的内存页面并将它们交换到磁盘。 (This is how a well-known reverse proxy works)。

剩下的唯一一件事是决定当 Windows 表示 RAM 不足时是否要释放一些图像。但如果您不使用内存,它将被换出到磁盘。

关于delphi - 如何为我的应用程序保留内存并保留指定的剩余量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621043/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com