Click here to Skip to main content
11,636,300 members (71,544 online)
Click here to Skip to main content

Reconsider Overriding UIView.Draw or UIView.DrawRect

, 31 Oct 2014 CPOL 6.8K 4
Rate this:
Please Sign up or sign in to vote.
There is a very high virtual memory penalty if you do.


I didn't think I was asking much. I wanted a UIView that could have a different (or no) border on each side.


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 too 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, I 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 not 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.

The Solution

As an experiment, I tried doing it the ugly way: I drew 4 rectangles, each one a subview, each with a length equal to a side and a width equal to the border width. 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), TinyMCE (JavaScript editor), and contributes to MySQL, Ghostscript, iTextSharp, Bacula, FreeBSD, and Xamarin.

Yvan's consumer-focused apps can be found in the Windows Store, Apple App Store, and Google Play marketplace.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Volynsky Alex24-Nov-14 11:34
professionalVolynsky Alex24-Nov-14 11:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 31 Oct 2014
Article Copyright 2014 by Yvan Rodrigues
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid