Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- Subject: Lost memory, GCD, dispatch sources, Cocoa bindings & User interface
- From: Jean Suisse <email@hidden>
- Date: Wed, 16 Sep 2015 13:00:19 +0200
Dear All,
I have an app that uses Cocoa and dispatch sources leaking a lot (ca. 5GB / 17h). The origin seems to come from a combination of dispatch sources, Bindings & Core Animation. Below is a brief description of the app, what issues I encounter and the related code.
DESCRIPTION OF THE APPLICATION
I have a scientific data acquisition App that is expected to run continuously for a year and collect data every second from 144 sensors. The app is written in Objective-C, targets MAC OS, and uses GCD a lot. Basically, I start several dispatch queues running blocks to deal with scientific instruments connected to different data buses. I use appropriate synchronization tools or locking mechanisms to run everything safely and deal with concurrency.
ISSUES
At first, I did bind the 115 textfields (most having number formatters) on the main view directly to the properties of the class instances representing different physical scientific instruments. The issue was that it caused the UI being refreshed from various threads while my app was operating (timer dispatch sources manipulating the objects to collect data from instruments causing UI refresh through bindings). I got the message "CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces” a lot.
So, I made a simple function (refreshUI) which transfers the data from non-bound properties of the objects to bound-properties of the objects like this :
// [myInstrument updateUI];
- (void)updateUI
{
assert([NSThread isMainThread]);
// LEFT: property of the object tied to the UI through bindings made in IB
// RIGHT: as a quick fix: variable member of the object updated using a background thread.
self.lastPT100Temperature = self->l_PT100Temperature;
self.lastPAAtmosphericPressure = self->l_PAAtmosphericPressure;
// etc...
}
This function is called once every second on the main thread like this :
- (void)startUIAutoUpdate
{
// @property (strong) dispatch_source_t uiRefreshTimer;
self.uiRefreshTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(self.uiRefreshTimer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0.25 * NSEC_PER_SEC);
__weak typeof(self) weakself = self;
dispatch_source_set_event_handler(self.uiRefreshTimer,
^{
@autoreleasepool
{
__strong typeof(weakself) strongself = weakself;
[strongself.myInstrument updateUI];
// etc...
}
};
dispatch_resume(self.uiRefreshTimer);
}
OBSERVATIONS
The app starts by taking around 25 MB of memory (reported by Activity monitor). After 17h, I see around 5GB of memory used. After a few days… the app crashes. On the other hand, if I comment the family of lines "[strongself.myInstrument updateUI]", the app can run for a month using only 25-35 MB of memory. Data are still collected every second and stored on disk of course, but not shown on screen.
If I prevent my app from trying to communicate with the instruments (by not instantiating the objects representing the communication interface) I can make it run faster and take thousands of dummy data points a second (and store them on the disk). The leaking issue is a little increased, but not by a factor of 1000. Only by a factor of 30 to 60. This is consistent with a UI refresh issue.
Additionally, I was unable to find any leaks with Instruments (but I barely know how to use it) and the amount of memory reported by Instruments is far less than the one reported by Activity monitor (but still, the system starts swapping a lot after a while, so Activity monitor must be right). But interestingly, when switching apps with CMD-TAB, Instruments shows memory jumps (memory brutally decreases).
QUESTION
Well, I feel a little lost at this point. I lack the know-how to further investigate the issue. Does anyone know what to try / measure / observe to get more clues?
Best Regards,
Jean
-----------
Jean Suisse
Institut de Chimie Moléculaire de l’Université de Bourgogne
(ICMUB) — UMR 6302
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden