[🍎 macOS] macOS 운영체제 시스템 메모리 사용량 (macOS System Memory Using Resource)
by cy_mos2024. 8. 8.
반응형
앱 메모리 (App Memory): 현재 실행 중인 애플리케이션들이 사용하고 있는 메모리 공간입니다.
유선 메모리 (Wired Memory): macOS 운영체제가 시스템 작동에 필요한 공간이며, 해당 메모리 공간에는 캐시할 수 없으며 RAM에 유지되어 있어야 하기 때문에 다른 애플리케이션에서 사용할 수 없습니다.
압축 된 메모리 (Compressed Memory): 사용 빈도가 낮은 메모리 데이터를 압축하여 저장하는 메모리 공간이며, macOS 시스템에서 메모리 사용량이 많아질 때 macOS 운영체제 시스템은 자동으로 비활성 앱이나 데이터를 압축하여 여유 공간을 확보합니다.
* 사용 가능 한 메모리 공간 영역은 [앱 메모리 공간 (App Memory) + 유선 메모리 (Wired Memory) + 압축 된 메모리 (Compressed Memory)] - 전체 메모리 공간 (Total Memory)으로 구할 수 있습니다.
* macOS 운영체제 시스템에서는 효율적인 메모리 관리를 위하여 위의 메모리 공간들을 유기적으로 작동하여 최적의 성능을 내기위하여 작동합니다.
🗂️ [Swift] macOS 운영체제 시스템 메모리 사용량 가져오기
// MARK: - EnumpublicenumSKResourceDataUnitType: Double{
case byte =1// 1 Bytecase kiloByte =1024// 1024 Bytecase megaByte =1_048_576// 1024 * 1024 Bytecase gigaByte =1_073_741_824// 1024 * 1024 * 1024 Bytecase teraBtye =1_099_511_627_776// 1024 * 1024 * 1024 * 1024 Byte
}
// MARK: - StructpublicstructSKMemoryResourceInfo: Codable{
/// Using Total RAM `[% Percentage]`publiclet value: Double/// Efficiently your memory is serving your processing needs `[% Percentage]`publiclet pressureValue: Double/// The amount of memory being used by appspubliclet appValue: Double/// Memory required by the system to operate. This memory can’t be cached and must stay in RAM, so it’s not available to other appspubliclet wiredValue: Double/// The amount of memory that has been compressed to make more RAM availablepubliclet compressedValue: Double
}
// MARK: - Extension DoubleextensionDouble{
publicvar round2dp: Double { return (10.0*self).rounded() /10.0 }
}
finalfuncgetStatistics64() -> vm_statistics64 {
var size: mach_msg_type_number_t =UInt32(MemoryLayout<vm_statistics64_data_t>.size /MemoryLayout<integer_t>.size)
let hostInfo = vm_statistics64_t.allocate(capacity: 1)
let_= hostInfo.withMemoryRebound(to: integer_t.self, capacity: Int(size)) { (pointer) -> kern_return_t inreturn host_statistics64(mach_host_self(), HOST_VM_INFO64, pointer, &size)
}
let data = hostInfo.move()
hostInfo.deallocate()
return data
}
finalfuncgetSystemMemoryResource(type: SKResourceDataUnitType= .gigaByte) -> SKMemoryResourceInfo {
let maximumMemory =Double(ProcessInfo.processInfo.physicalMemory) / type.rawValue
let load = getStatistics64()
let unit =Double(vm_kernel_page_size) / type.rawValue
let active =Double(load.active_count) * unit
let speculative =Double(load.speculative_count) * unit
let inactive =Double(load.inactive_count) * unit
let wired =Double(load.wire_count) * unit
let compressed =Double(load.compressor_page_count) * unit
let purgeable =Double(load.purgeable_count) * unit
let external =Double(load.external_page_count) * unit
let using = active + inactive + speculative + wired + compressed - purgeable - external
returnSKMemoryResourceInfo(value: min(99.9, (100.0* using / maximumMemory).round2dp),
pressureValue: (100.0* (wired + compressed) / maximumMemory).round2dp,
appValue: (using - wired - compressed).round2dp,
wiredValue: wired.round2dp,
compressedValue: compressed.round2dp)
}
댓글