Skip to content

Commit

Permalink
Adding timing capabilities to clean memory (#5)
Browse files Browse the repository at this point in the history
Adding timing capabilities to clean memory
This will enable projects to time how long garbage collection is taking
  • Loading branch information
plivesey committed Jun 7, 2016
1 parent e122588 commit 7ce5a5e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ConsistencyManager/ConsistencyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public class ConsistencyManager {
*/
public var garbageCollectionInterval: NSTimeInterval = 300

// MARK: Constants

/**
This notification is fired whenever the asynchronous work of clean memory starts.
It's useful if you want to add timers to this work.
Called on an internal thread.
*/
public static let kCleanMemoryAsynchronousWorkStarted = "com.linkedin.consistencyManager.kCleanMemoryAsynchronousWorkStarted"

/**
This notification is fired whenever the asynchronous work of clean memory finishes.
It's useful if you want to add timers to this work.
Called on an internal thread.
*/
public static let kCleanMemoryAsynchronousWorkFinished = "com.linkedin.consistencyManager.kCleanMemoryAsynchronousWorkFinished"

// MARK: - Private ivars

/**
Expand Down Expand Up @@ -343,6 +359,7 @@ public class ConsistencyManager {
*/
public func cleanMemory() {
dispatch_async(dispatchQueue) {
NSNotificationCenter.defaultCenter().postNotificationName(ConsistencyManager.kCleanMemoryAsynchronousWorkStarted, object: self)
for (key, var listenersArray) in self.listeners {
listenersArray.prune()
// If the array has no elements now, let's remove it from the dictionary
Expand All @@ -353,6 +370,7 @@ public class ConsistencyManager {
self.listeners[key] = listenersArray
}
}
NSNotificationCenter.defaultCenter().postNotificationName(ConsistencyManager.kCleanMemoryAsynchronousWorkFinished, object: self)
}
// Remove any PausedListener structs from our local list if the internal listener is now nil
pausedListeners = self.pausedListeners.filter { $0.listener != nil }
Expand Down
20 changes: 20 additions & 0 deletions ConsistencyManagerTests/MemoryWarningTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ import XCTest

class MemoryWarningTests: ConsistencyManagerTestCase {

var cleanMemoryStartedTimes = [NSDate]()
var cleanMemoryFinishedTimes = [NSDate]()


func testMemoryWarning() {
let testStart = NSDate()

NSNotificationCenter.defaultCenter().addObserverForName(ConsistencyManager.kCleanMemoryAsynchronousWorkStarted, object: nil, queue: nil) { _ in
self.cleanMemoryStartedTimes.append(NSDate())
}

NSNotificationCenter.defaultCenter().addObserverForName(ConsistencyManager.kCleanMemoryAsynchronousWorkFinished, object: nil, queue: nil) { _ in
self.cleanMemoryFinishedTimes.append(NSDate())
}

let model = TestRequiredModel(id: "0", data: 0)

// Setting this up with a block ensures that the listener will be released and is out of scope.
Expand Down Expand Up @@ -42,5 +56,11 @@ class MemoryWarningTests: ConsistencyManagerTestCase {
XCTAssertEqual(listenersArray.count, 0)
}
// Else it's nil which is fine too

// Now, let's check we got the right start/finish times
XCTAssertEqual(cleanMemoryStartedTimes.count, 1)
XCTAssertEqual(cleanMemoryFinishedTimes.count, 1)
XCTAssertTrue(cleanMemoryStartedTimes[0].timeIntervalSince1970 <= cleanMemoryFinishedTimes[0].timeIntervalSince1970)
XCTAssertTrue(testStart.timeIntervalSince1970 <= cleanMemoryStartedTimes[0].timeIntervalSince1970)
}
}

0 comments on commit 7ce5a5e

Please sign in to comment.