skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search