c# - 如何在 iOS 上的 Unity3d 中获取 "free"内存量?

标签 c# ios objective-c unity3d

我有一个问题:如果我尝试使用 Texture2D.ReadPixels 创建屏幕截图,游戏在某些设备上崩溃(最著名的是 iPod 4G)。据推测,它发生是因为内存不足。在创建屏幕截图之前,我想检测我是否可以安全地分配所需的内存量,并在我认为我会崩溃时向玩家显示警告。

但是,纹理等资源似乎是在 Mono VM 之外管理的。 System.GC.GetTotalMemory 返回 9mb,当我有大到 16mb 的 map 集时。所以,看来我必须为此编写一个插件。

(有一段描述我没有收到低内存警告,但似乎我对此有误,并且在 Objective-C 级别上,警告被成功引发)。

如何在不崩溃的情况下获得可以分配的“空闲”内存量?是否有其他方法可以实现我想要的功能?

最佳答案

解释您自己的答案:这不是 objective-C 代码,而是普通的旧 C,使用 Mach API 以从内核获取统计信息。 Mach API 是由 XNU 导出的低级 API,XNU 是一个混合内核,由顶层(导出 BSD API,就像我们从 UN*X 了解和喜爱的常用系统调用)和底层 - Mach 组成。 该代码使用 Mach“主机”抽象,它(除其他外)提供有关操作系统级别资源利用率的统计信息。

具体来说,这是一个完整的注释:

#import <mach/mach.h>       // Required for generic Mach typedefs, like the mach_port_t
#import <mach/mach_host.h>  // Required for the host abstraction APIs.

extern "C" // C, rather than objective-c
{

    const int HWUtils_getFreeMemory()
    {
        mach_port_t host_port;             
        mach_msg_type_number_t host_size;
        vm_size_t pagesize;

        // First, get a reference to the host port. Any task can do that, and this
        // requires no privileges
        host_port = mach_host_self();
        host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);

        // Get host page size - usually 4K
        host_page_size(host_port, &pagesize);

        vm_statistics_data_t vm_stat;

        // Call host_statistics, requesting VM information. 

        if (host_statistics(host_port,  // As obtained from mach_host_self()
             HOST_VM_INFO,              // Flavor - many more in <mach/host_info.h>(host_info_t)&vm_stat,                  // OUT - this will be populated
             &host_size)                // in/out - sizeof(vm_stat). 
              != KERN_SUCCESS)         
            NSLog(@"Failed to fetch vm statistics"); // log error

        /* Stats in bytes */
        // Calculating total and used just to show all available functionality

        // This part is basic math. Counts are all in pages, so multiply by pagesize
        // Memory used is sum of active pages, (resident, used)
        //                       inactive,      (resident, but not recently used)
        //                   and wired (locked in memory, e.g. kernel)

        natural_t mem_used = (vm_stat.active_count +
                              vm_stat.inactive_count +
                              vm_stat.wire_count) * pagesize;
        natural_t mem_free = vm_stat.free_count * pagesize;
        natural_t mem_total = mem_used + mem_free;
        NSLog(@"used: %u free: %u total: %u", mem_used, mem_free, mem_total);


        return (int) mem_free;
    }

关于c# - 如何在 iOS 上的 Unity3d 中获取 "free"内存量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975772/

相关文章:

c# - 这个简单的 Log4Net 包装器可以改进吗?

c# - 代码在本地 Windows 2008 上运行正常,但无法在 Azure 云上的应用程序数据中创建子文件夹

ios - 即使应用程序被终止,如何继续进行 iOS 位置跟踪?

c# - 将日期从回历日期转换为公历日期时,字符串未被识别为有效的日期时间

ios - 在多个对象上渲染多个纹理的问题

ios - Invalid Bundle Info.plist 为 CFBundleExecutable 键指定了一个不存在的文件

Objective-C:从 NSString 生成 NSArray

iphone - subarrayWithRange 具有大数据

ios - Objective-C如何在ARC代码中释放自己

c# - 如何并行运行一组函数并等待完成后的结果?