Click here to Skip to main content
Click here to Skip to main content
Go to top

Reconsider overriding UIView.Draw or UIView.DrawRect.

, 7 Apr 2014
Rate this:
Please Sign up or sign in to vote.
There is a very high virtual memory penalty if you do.


This problem has been haunting me for about two months. I am creating an Outlook-style calendar component for iOS. If you consider such a control, you'll realize it is made up of hundreds of child controls:

In fact the grid alone in week view requires 7 days × 24 hours × 4 divisions per hour = 672 views.

After a couple of weeks I concluded that I needed to dump iOS's AutoLayout feature in favour of writing my own alternative. Its algorithms are way to slow to handle that number of views, and it would take up to 10 seconds or more to build and render the calendar.

After all memory leaks were taken care of there remained a new problem. Even though memory on the heap was being freed, there remained a huge allocation of virtual memory (114MB shown):

After weeks of searching for a solution a read a comment that said that the 2012 WWDC session 242: iOS App Performance: Memory by Morgan Grainger was useful. In that presentation, Morgan referenced another session, #238 iOS App Performance: Graphics and Animations. About 2/3 through this session the presenter mentions that overriding DrawRect comes with a high VM penalty. Even if your override looks like:

override void DrawRect(RectangleF rect)

The OS will allocate VM for drawing. The reason I was user-drawing is that iOS does no provide a way to draw borders in which the sides of the border are different colour or width. I was drawing these manually using CoreGraphics. Instead of doing it this way I drew 4 rectangles, each with a length equal to a side and a width equal to the border width. Ugly. The difference in VM usage is astonishing:

It doesn't even register.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Yvan Rodrigues
President Red Cell Innovation Inc.
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the manufacturing sector. He runs Red Cell Innovation Inc./L'innovation de Globules Rouges, a consulting company focusing on efficiency and automation of manufacturing and business processes for small businesses, healthcare, and the public sector. He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).
Yvan draws on experience at Mabel's Labels Inc. as Manager of Systems and Development, and the University of Waterloo as Information Systems Manager.
Yvan supports open-source software. He is a committer for SharpKit (C# to Javascript cross-compiler) and WebIssues (Issue/Ticket Management System), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, MonoTouch, and Mono for Android.
Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.
Follow on   Google+   LinkedIn

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 7 Apr 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid