Click here to Skip to main content
12,760,727 members (29,236 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 7 Apr 2014

Reconsider Overriding UIView.Draw or UIView.DrawRect

, 31 Oct 2014 CPOL
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
Systems Engineer Clearpath Robotics
Canada Canada
Yvan Rodrigues has 25 years of experience in information systems and software development for the industry. He is Senior Systems Developer at Clearpath Robotics

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., 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.

You may also be interested in...


Comments and Discussions

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

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

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

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