Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Oct 18, 2023
1 parent 19158fd commit efb8dec
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
17 changes: 11 additions & 6 deletions iOSClient/Media/NCMediaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import LRUCache
import NextcloudKit

class NCMediaManager {

Expand Down Expand Up @@ -36,7 +37,8 @@ class NCMediaManager {
var files: [FileInfo] = []

let startDate = Date()
print("--------- start ThumbnailLRUCache image process ---------")
let startMemory = NCUtility.shared.getMemoryUsedAndDeviceTotalInMegabytes()
NextcloudKit.shared.nkCommonInstance.writeLog("--------- start ThumbnailLRUCache image process ---------")

// Get files only image / video
if let enumerator = manager.enumerator(at: URL(fileURLWithPath: directory), includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {
Expand All @@ -56,8 +58,8 @@ class NCMediaManager {
// Sort for most recent
files.sort(by: { $0.date > $1.date })
if let firstDate = files.first?.date, let lastDate = files.last?.date {
print("First date: \(firstDate)")
print("Last date: \(lastDate)")
NextcloudKit.shared.nkCommonInstance.writeLog("First date: \(firstDate)")
NextcloudKit.shared.nkCommonInstance.writeLog("Last date: \(lastDate)")
}

// Insert in cache
Expand All @@ -72,9 +74,12 @@ class NCMediaManager {

let endDate = Date()
let diffDate = endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
print("Counter process: \(cache.count)")
print("Time process: \(diffDate)")
print("--------- stop ThumbnailLRUCache image process ---------")
let endMemory = NCUtility.shared.getMemoryUsedAndDeviceTotalInMegabytes()
let usedMemory = endMemory.0 - startMemory.0
NextcloudKit.shared.nkCommonInstance.writeLog("Counter process: \(cache.count)")
NextcloudKit.shared.nkCommonInstance.writeLog("Time process: \(diffDate)")
NextcloudKit.shared.nkCommonInstance.writeLog("Memory used for cache in MB: \(usedMemory)")
NextcloudKit.shared.nkCommonInstance.writeLog("--------- stop ThumbnailLRUCache image process ---------")
}

func getImage(ocId: String) -> UIImage? {
Expand Down
32 changes: 32 additions & 0 deletions iOSClient/Utility/NCUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,36 @@ class NCUtility: NSObject {
}
}
}

// https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget/19692719#19692719
// https://stackoverflow.com/questions/27556807/swift-pointer-problems-with-mach-task-basic-info/27559770#27559770

func getMemoryUsedAndDeviceTotalInMegabytes() -> (Float, Float) {

var usedmegabytes: Float = 0

let totalbytes = Float(ProcessInfo.processInfo.physicalMemory)
let totalmegabytes = totalbytes / 1024.0 / 1024.0

var info = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 4

let kerr: kern_return_t = withUnsafeMutablePointer(to: &info) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(
mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count
)
}
}

if kerr == KERN_SUCCESS {
let usedbytes: Float = Float(info.resident_size)
usedmegabytes = usedbytes / 1024.0 / 1024.0
}

return (usedmegabytes, totalmegabytes)
}
}

0 comments on commit efb8dec

Please sign in to comment.