How to find available memory in iPhone programmatically?

前端 未结 3 1073
猫巷女王i
猫巷女王i 2020-12-13 10:19

I\'d like to know how to find programmatically available memory in iPhone from Objective-C?

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 10:57

    You can use the Mach call host_info(host, flavor, host_info, host_info_count). If you call it with flavor=HOST_BASIC_INFO, the buffer host_info points to is filled with a struct host_basic_info, what looks like that:

    struct host_basic_info {
        integer_t               max_cpus;               /* max number of CPUs possible */
        integer_t               avail_cpus;             /* number of CPUs now available */
        natural_t               memory_size;            /* size of memory in bytes, capped at 2 GB */
        cpu_type_t              cpu_type;               /* cpu type */
        cpu_subtype_t           cpu_subtype;            /* cpu subtype */
        cpu_threadtype_t        cpu_threadtype;         /* cpu threadtype */
        integer_t               physical_cpu;           /* number of physical CPUs now available */
        integer_t               physical_cpu_max;       /* max number of physical CPUs possible */
        integer_t               logical_cpu;            /* number of logical cpu now available */
        integer_t               logical_cpu_max;        /* max number of physical CPUs possible */
        uint64_t                max_mem;                /* actual size of physical memory */
    }
    

    From this structure, you can get the memory size.

提交回复
热议问题