2016-11-09 1 views
0
func systemFreeMemorySize() -> UInt? 
{ 
    let HOST_VM_INFO_COUNT: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.size/MemoryLayout<integer_t>.size) 

    let host: host_t = mach_host_self() 
    var pageSize: vm_size_t = vm_size_t() 
    let hostPageSizeKernStatus: kern_return_t = host_page_size(host, &pageSize) 
    /* 
    guard hostPageSizeKernStatus == KERN_SUCCESS else { 
     NSLog("Error with host_page_size(): " + (String.fromCString(mach_error_string(hostPageSizeKernStatus)) ?? "unknown error")) 
     return nil 
    }*/ 
    var stats: vm_statistics_data_t = vm_statistics_data_t() 
    var count: mach_msg_type_number_t = HOST_VM_INFO_COUNT 

    let kernStatus: kern_return_t = withUnsafeMutablePointer(to: &stats) { 
     // *** Error on following line: 
     return host_statistics(host, HOST_VM_INFO, host_info_t($0), &count) 
    } 

    /* 
    guard kernStatus == KERN_SUCCESS else { 
     NSLog("Error with host_statistics(): " + (String.fromCString(mach_error_string(kernStatus)) ?? "unknown error")) 
     return nil 
    } */ 

    return UInt(stats.free_count) * UInt(pageSize) 
} 

Dans la ligne host_statistics de retour (hôte, HOST_VM_INFO, host_info_t (0 $), & nombre) il y a une erreur: 'init' est disponible: utilisation withMemoryRebound (to: capacity: _) 'pour afficher temporairement la mémoire comme un autre type compatible avec la mise en page.Comment réparer cette erreur (return host_statistics) dans Xcode 8.1? Je veux savoir la mémoire libre du dispositif

Comment résoudre ce problème?

Répondre

2

pour Swift 3.1. (Je glissai plus de code pour inspecter toutes les informations dont vous pourriez avoir besoin.)

func freeMemory()->UInt64 { 


    let host_port :mach_port_t = mach_host_self() 

    let HOST_BASIC_INFO_COUNT = MemoryLayout<host_basic_info>.stride/MemoryLayout<integer_t>.stride 
    var size = mach_msg_type_number_t(HOST_BASIC_INFO_COUNT) 
    var hostInfo = host_basic_info() 

    let status1 = withUnsafeMutablePointer(to: &hostInfo) { 
     $0.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { 
      host_info(host_port, Int32(HOST_BASIC_INFO), $0, &size) 
     } 
    } 

    print(status1, hostInfo) 

    // THE FIX: 

    var pageSize: vm_size_t = vm_size_t() 
    let hostPageSizeKernStatus: kern_return_t = host_page_size(host_port, &pageSize) 


    var vm_stat = vm_statistics_data_t() 
    let HOST_VM_INFO_COUNT = MemoryLayout<vm_statistics_data_t>.stride/MemoryLayout<integer_t>.stride 
    var size2 = mach_msg_type_number_t(HOST_VM_INFO_COUNT) 

    let status2 = withUnsafeMutablePointer(to: &vm_stat) { 
     $0.withMemoryRebound(to: integer_t.self, capacity: Int(size2)) { 
      host_statistics(host_port, Int32(HOST_VM_INFO), $0, &size2) 
      //C: 
      // (void) host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size); 

     } 
    } 

    print(status2, vm_stat) 

    let freePages = UInt64(vm_stat.free_count) 
    let result = freePages * UInt64(pageSize) 


    return result 
} 

O mon macbook je suis arrivé:

47483648, cpu_type: 7, cpu_subtype: 8, cpu_threadtype: 1, physical_cpu: 4, physical_cpu_max: 4, logical_cpu: 8, logical_cpu_max: 8, max_mem: 17179869184)

vm_statistics (free_count: 1240186, active_count: 1765236, inactive_count: 124649, wire_count: 684265, zero_fill_count: 921561165, réactivations: 4645359, pageins: 19628659, pages: 23555, erreurs: 1940619345, cow_faults: 94990152, recherche: 2563634, hits: 40, purgeable_count: 166562, purges: 4217152, specu lative_count: 306146)

et le résultat est: 5079801856

à-dire 5GB