From ddf2620ff044d0f953a8293366e64ae381f7063e Mon Sep 17 00:00:00 2001 From: Maciek Grzybowski Date: Thu, 16 Jul 2020 11:54:07 +0200 Subject: [PATCH] RUMM-610 Optimize memory usage when doing intensive logging --- Sources/Datadog/Core/Persistence/FilesOrchestrator.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Sources/Datadog/Core/Persistence/FilesOrchestrator.swift b/Sources/Datadog/Core/Persistence/FilesOrchestrator.swift index 2fef0aebdc..bbe8c55736 100644 --- a/Sources/Datadog/Core/Persistence/FilesOrchestrator.swift +++ b/Sources/Datadog/Core/Persistence/FilesOrchestrator.swift @@ -36,14 +36,18 @@ internal class FilesOrchestrator { throw InternalError(description: "data exceeds the maximum size of \(performance.maxObjectSize) bytes.") } - try purgeFilesDirectoryIfNeeded() - let lastWritableFileOrNil = reuseLastWritableFileIfPossible(writeSize: writeSize) if let lastWritableFile = lastWritableFileOrNil { // if last writable file can be reused lastWritableFileUsesCount += 1 return lastWritableFile } else { + // NOTE: RUMM-610 As purging files directory is a memory-expensive operation, do it only when we know + // that a new file will be created. With SDK's `PerformancePreset` this gives + // the process enough time to not over-allocate internal `_FileCache` and `_NSFastEnumerationEnumerator` + // objects, resulting with a flat allocations graph in a long term. + try purgeFilesDirectoryIfNeeded() + let newFileName = fileNameFrom(fileCreationDate: dateProvider.currentDate()) let newFile = try directory.createFile(named: newFileName) lastWritableFileName = newFile.name