0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift:macのメモリパフォーマンスを取得する

Last updated at Posted at 2019-06-23

#ソースと使用例

ソース
import Darwin

public class Memory {
    
    private let gigaByte: Double = 1073741824
    private let hostVmInfo64Count: mach_msg_type_number_t!
    private let hostBasicInfoCount: mach_msg_type_number_t!
    
    init() {
        hostVmInfo64Count = UInt32(MemoryLayout<vm_statistics64_data_t>.size / MemoryLayout<integer_t>.size)
        hostBasicInfoCount = UInt32(MemoryLayout<host_basic_info_data_t>.size / MemoryLayout<integer_t>.size)
    }
    
    public var maxMemory: Double {
        var size: mach_msg_type_number_t = hostBasicInfoCount
        let hostInfo = host_basic_info_t.allocate(capacity: 1)
        let _ = hostInfo.withMemoryRebound(to: integer_t.self, capacity: Int()) { (pointer) -> kern_return_t in
            return host_info(mach_host_self(), HOST_BASIC_INFO, pointer, &size)
        }
        let data = hostInfo.move()
        hostInfo.deallocate()
        return Double(data.max_mem) / gigaByte
    }
    
    private func vmStatistics64() -> vm_statistics64 {
        var size: mach_msg_type_number_t = hostVmInfo64Count
        let hostInfo = vm_statistics64_t.allocate(capacity: 1)
        let _ = hostInfo.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { (pointer) -> kern_return_t in
            return host_statistics64(mach_host_self(), HOST_VM_INFO64, pointer, &size)
        }
        let data = hostInfo.move()
        hostInfo.deallocate()
        return data
    }
    
    public func usageMemory() -> (app: Double, wired: Double, compressed : Double, cache: Double, pressure: Double) {
        let load = vmStatistics64()

        let active      = Double(load.active_count) * Double(PAGE_SIZE) / gigaByte
        let speculative = Double(load.speculative_count) * Double(PAGE_SIZE) / gigaByte
        let inactive    = Double(load.inactive_count) * Double(PAGE_SIZE) / gigaByte
        let wired       = Double(load.wire_count) * Double(PAGE_SIZE) / gigaByte
        let compressed  = Double(load.compressor_page_count) * Double(PAGE_SIZE) / gigaByte
        let purgeable   = Double(load.purgeable_count) * Double(PAGE_SIZE) / gigaByte
        let external    = Double(load.external_page_count) * Double(PAGE_SIZE) / gigaByte
        
        let using        = active + inactive + speculative + wired + compressed - purgeable - external
        let app          = using - wired - compressed
        let cache        = purgeable + external
        let pressure     = 100.0 * (wired + compressed) / maxMemory
        
        return (app, wired, compressed, cache, pressure)
    }
    
}
使用例
let memory = Memory()
Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (t) in
    let res = memory.usageMemory()
    // 単位はGB
    Swift.print("物理メモリ: \(memory.maxMemory)")
    Swift.print("アプリケーションメモリ: \(res.app)")
    Swift.print("確保されているメモリ: \(res.wired)")
    Swift.print("圧縮: \(res.compressed)")
    Swift.print("使用済みメモリ: \(res.app + res.wired + res.compressed)")
    Swift.print("キャッシュされたファイル: \(res.cache)")
    Swift.print("メモリプレッシャー: \(round(10.0 * res.pressure) / 10.0)%")
}

スワップ使用領域の算出方法だけわからなかった...

#参考
获取Mac端的内存使用情况

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?