Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NR-364636 fixing the persistent store memory leak #345

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Agent/Analytics/NRMAAnalytics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ - (id) initWithSessionStartTimeMS:(long long) sessionStartTime {

PersistentEventStore *eventStore = [[PersistentEventStore alloc] initWithFilename:filename andMinimumDelay:.025];

_eventManager = [[NRMAEventManager alloc] initWithPersistentStore:eventStore];
_eventManager = [[NRMAEventManager alloc] initWithPersistentStore:[eventStore autorelease]];
_attributeValidator = [[NRMAAttributeValidator alloc] init];
_sessionAttributeManager = [[NRMASAM alloc] initWithAttributeValidator:_attributeValidator];

Expand Down
2 changes: 1 addition & 1 deletion Agent/Analytics/NRMAEventManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ - (BOOL)didReachMaxQueueTime:(NSTimeInterval)currentTimeMilliseconds {
}

NSTimeInterval oldestEventAge = currentTimeMilliseconds - oldestEventTimestamp;
return (oldestEventAge / 1000) + kBufferTimeSecondsLeeway >= maxBufferTimeSeconds;
return (oldestEventAge / kDefaultBufferSize) + kBufferTimeSecondsLeeway >= maxBufferTimeSeconds;
}

- (NSUInteger)getEvictionIndex {
Expand Down
27 changes: 23 additions & 4 deletions Agent/Analytics/PersistentEventStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,34 @@ - (nonnull instancetype)initWithFilename:(NSString *)filename
return self;
}

- (void) dealloc {
if(self.pendingBlock){
dispatch_block_cancel(self.pendingBlock);
}
}

- (void)performWrite:(void (^)(void))writeBlock {
__weak PersistentEventStore *weakSelf = self;
dispatch_async(self.writeQueue, ^{
if (self.pendingBlock != nil) {
dispatch_block_cancel(self.pendingBlock);
__strong PersistentEventStore *strongSelf = weakSelf;
if (!strongSelf) { // Ensure strongSelf is not nil
NRLOG_WARNING(@"A block was scheduled but PersistentEventStore was deallocated before running");
return;
}

self.pendingBlock = dispatch_block_create(0, writeBlock);
if (strongSelf.pendingBlock != nil) {
dispatch_block_cancel(strongSelf.pendingBlock);
}

strongSelf.pendingBlock = dispatch_block_create(0, writeBlock);

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self->_minimumDelay * NSEC_PER_SEC)), self->_writeQueue, self.pendingBlock);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(strongSelf->_minimumDelay * NSEC_PER_SEC)), strongSelf->_writeQueue, ^{
__strong PersistentEventStore *innerStrongSelf = weakSelf;
if (innerStrongSelf && innerStrongSelf.pendingBlock) {
innerStrongSelf.pendingBlock();
innerStrongSelf.pendingBlock = nil; // Release the block after execution
}
});
});
}

Expand Down
Loading