1

I know this has been asked multiple times, and I spent already multiple days to have the exact code for that, but seems I am still far away and I need help.

I use the below code,

    /**
    
      TOTAL DEVICE RAM MEMORY
     
    **/
    let total_bytes = Float(ProcessInfo.processInfo.physicalMemory)
    let total_megabytes = total_bytes / 1024.0 / 1024.0

    /**
    
      FREE DEVICE RAM MEMORY
     
    **/
    
    
    var usedMemory: Int64 = 0
    var totalUsedMemoryInMB: Float = 0
    var freeMemoryInMB: Float = 0
    let hostPort: mach_port_t = mach_host_self()
    var host_size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout<vm_statistics_data_t>.stride / MemoryLayout<integer_t>.stride)
    var pagesize:vm_size_t = 0
    host_page_size(hostPort, &pagesize)
    var vmStat: vm_statistics = vm_statistics_data_t()
    let capacity = MemoryLayout.size(ofValue: vmStat) / MemoryLayout<Int32>.stride

    let status: kern_return_t = withUnsafeMutableBytes(of: &vmStat) {
    let boundPtr = $0.baseAddress?.bindMemory( to: Int32.self, capacity: capacity )
               return host_statistics(hostPort, HOST_VM_INFO, boundPtr, &host_size)
    }
          
    if status == KERN_SUCCESS {
    usedMemory = (Int64)((vm_size_t)(vmStat.active_count + vmStat.inactive_count + vmStat.wire_count) * pagesize)
    totalUsedMemoryInMB = (Float)( usedMemory / 1024 / 1024 )
    freeMemoryInMB = total_megabytes - totalUsedMemoryInMB
    print("free memory: \(freeMemoryInMB)")
    }

And I got the below results (real devices)

iPhone XR enter image description here

free memory: 817.9844

Difference of about 150MB

iPhone13 Pro Max enter image description here

free memory: 1384.2031

Difference of about 700MB

iPad 2021 enter image description here

free memory: 830.9375

Difference of about 170MB

I also used the below variants, with even worst results

//usedMemory = (Int64)((vm_size_t)(vmStat.active_count + vmStat.inactive_count + vmStat.wire_count + vmStat.free_count) * pagesize)

//usedMemory = (Int64)((vm_size_t)(vmStat.active_count + vmStat.wire_count) * pagesize)
 
//usedMemory = (Int64)((vm_size_t)(vmStat.inactive_count + vmStat.wire_count) * pagesize)
 
//usedMemory = (Int64)((vm_size_t)(vmStat.active_count + vmStat.inactive_count ) * pagesize) 

A difference of about 100 MB is ok, but really do not understand why it is function of the device and I am not sure how can I can have a reliable value. If that is not possible the difference between the real and the one got by the code for each device will be consistant so that I can pad it to get the real value?

My app is using scenekit and is hangry of resources, need to remove details once I am exsausting the memory.

Any help is appreciated.

czane
  • 392
  • 1
  • 6
  • 18
  • It might be better to consider _why_ you are "exhausting the memory" and not do that. – matt Oct 27 '22 at 18:33
  • Did you ever find a solution? I am currently also investigating a bug that I think is related to memory and I'd love to print current consumption to hopefully more easily find the bug... – Georg Apr 11 '23 at 11:59
  • Updated with a solution. To me it has been quite accurate for a while now. HTH – czane Apr 16 '23 at 16:06

2 Answers2

1

I hope this method will help you -

var physicalMemory: UInt64 {
    return (ProcessInfo().physicalMemory / 1024) / 1024 // in MB
}    

func deviceRemainingFreeSpace() -> Int64? {
    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
    guard
        let systemAttributes = try? FileManager.default.attributesOfFileSystem(forPath: documentDirectory),
        let freeSize = systemAttributes[.systemFreeSize] as? NSNumber
    else {
        return nil
    }
    return (freeSize.int64Value / 1024) / 1024 // in MB
}
Abhishek
  • 61
  • 4
  • the question is regarding RAM memory. – czane Oct 27 '22 at 19:57
  • I have shared a property which will return actual Physical Memory of Device i.e. ProcessInfo().physicalMemory // in Bytes – Abhishek Oct 28 '22 at 06:23
  • Please read carefully the Question: Get total free memory. Total memory count is already in the code provided in the question. – czane Oct 28 '22 at 06:56
0

sorry for the delay forgot about this.

I got it resolved via support.

/**
        
FREE MEMORY OF THE PHONE ( FOR THE APP )

https://developer.apple.com/documentation/os/3191911-os_proc_available_memory

https://stackoverflow.com/questions/67406682/how-to-call-size-t-os-proc-available-memoryvoid-with-swift

**/
        


       
        
let availableMemoryForApp = os_proc_available_memory() / 1024 / 1024
czane
  • 392
  • 1
  • 6
  • 18