【问题标题】:How do you simulate low memory conditions under macOS?如何在 macOS 下模拟低内存条件?
【发布时间】:2019-06-29 02:58:08
【问题描述】:

在开发和调试 macOS 应用程序时,有没有办法模拟系统范围的“低内存”事件或通知?

iOS 模拟器有这样的功能,但我似乎无法在 macOS 下找到一个等价物。

我有兴趣模拟内存不足的情况,看看NSPurgeableDataIOSurface 之类的类如何处理被清除。

(除了编​​写一个不断分配内存的简单应用程序......)

【问题讨论】:

    标签: xcode macos cocoa memory-management appkit


    【解决方案1】:

    macOS 上用于模拟低内存条件的工具是 memory_pressure(示例):

    sudo memory_pressure -S -l critical
    

    上述命令模拟 (-S) 临界级别 (-lcritical) 的内存压力。

    姓名
    memory_pressure -- 应用真实或模拟内存压力的工具 系统。

    概要
    memory_pressure [-l 级别] | [-ppercent_free] | [-S-l级别]

    选项
    -l 应用真实或模拟内存压力(如果在旁边指定 在系统上模拟参数),直到生成相应的低内存通知。支持的值为“警告”和“关键”。

     -p <percent_free> Allocate memory till the available memory in the system
     is <percent_free> of total memory. If the percentage of available memory
     to total memory on the system drops, the tool will free memory till
     either the desired percentage is achieved or it runs out of memory to
     free.
    
     -S Simulate memory pressure on the system by placing it artificially for
     <sleep_seconds> duration at the "warn" or "critical" level.
    
     -s <sleep_seconds> Duration to wait before allocating or freeing memory
     if applying real pressure. In case of simulating memory pressure, this is
     the duration the system will be maintained at an artifical memory level.
    

    描述
    一种在系统上应用真实或模拟内存压力的工具

    另请参阅
    vm_stat(1)

    【讨论】:

      【解决方案2】:

      从代码中,您可以使用setrlimit 函数来控制应用程序中的系统资源消耗。

      或者,ulimit 允许控制从终端启动的资源

      【讨论】:

      • 有趣。 setrlimit 似乎只控制给定应用程序的资源使用。这足以让系统从采用NSDiscardableContent 的类中驱逐数据吗?在我的应用程序中,我跨进程边界共享IOSurfaces。手册页指出setrlimit 也会影响每个子进程,但我想知道 macOS 如何决定哪个进程“拥有” IOSurface 以及何时可以清除它。我真的希望能够正常运行我的应用程序,分配通常的表面和数据,然后触发资源限制。 setrlimit 会追溯应用限制吗?
      • 我对 IOSurface 了解不多。也许您可以设置一个非常低的限制并进行实验,以了解setrlimit 将如何影响您的应用程序。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-27
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多