Leaks Like a Sieve | Rixstep Developers Workshop (2024)

Home » Learning Curve » Developers Workshop

Was Woz right? After all, memory management is simple - isn't it?


Get It

Try It

Apple's macOS code leaks like a sieve. Lots of it does. Not all of it - lots of it. Leaks bad.

Not so terribly far back in the mists of time, we took our own collection, the ACP - including 70+ Cocoa utilities - and ran them through leak tests.

leaks(1) BSD General Commands Manual leaks(1)

NAME

leaks - Search a process's memory for unreferenced malloc buffers

DESCRIPTION

leaks identifies leaked memory - memory that the application has allocated, but has been lost and cannot be freed. Specifically, leaks examines a specified process's memory for values that may be pointers to malloc-allocated buffers. Any buffer reachable from a pointer in writable global memory (e.g., __DATA segments), a register, or on the stack is assumed to be memory in use. Any buffer reachable from a pointer in a reachable malloc-allocated buffer is also assumed to be in use. The buffers which are not reachable are leaks; the buffers could never be freed because no pointer exists in memory to the buffer, and thus free() could never be called for these buffers. Such buffers waste memory; removing them can reduce swapping and memory usage. Leaks are particularly dangerous for long-running programs, for eventually the leaks could fill memory and cause the application to crash.

There have been other tools available of course, but /usr/bin/leaks is the yardstick.

Our own leaks project took well over half a year to complete - getting rid of all leaks until /usr/bin/leaks turned up nada on each and every one. Some leaks were recognisable after a time. Others were more difficult to figure out. But we got them all.

The rules of memory management in Objective-C are eminently straightforward. They're 'black and white'. They should be learned. Unfortunately, Apple caved in there for a while and implemented automatic garbage collection. So that programmers who were too lame to learn elementary memory management fundamentals didn't have to learn after all. (Then of course they outlawed automatic garbage collection some time later, so programs built with it suddenly refused to run. Oh well. Typical Apple.)

A bit of explanation here. It's not always your own code that's the culprit behind a leak. In our case, it rarely was. The culprit is most often the supporting system frameworks supplied by Apple. These Apple frameworks are almost always the culprits.

So the leak hunters start out on a lugubrious process to find alternative ways to achieve the same thing that's already in their own code, but doing in a different way, invoking different APIs. And, yes, it can be done. It often takes time, patience, and ingenuity. But it can be done.

Unfortunately, the conscientiousness and pride that people can take in their own products is not necessarily a sentiment shared by corporate rank and file. Steve Wozniak caused quite the stir when he said that Apple code ranked easily as the worst he'd seen.

So what happens, if you completely clean your code of all leaks - all of them - and run them all through /usr/bin/leaks, and they all get perfect report cards... and then Apple come out with a new version of their OS and /usr/bin/leaks suddenly reports new leaks all over the place? Seriously now: who's the odd man out? You got it.

But that wonderful experience was back in the says of Sierra and High Sierra. One such experience is more than enough. Now they've gifted us with Dark Mode Mojave and the unstable Catalina, and the situation is - perhaps not unexpectedly - even worse. Frankly, there's no point in worrying about leaks if Apple's programmers don't seem to care themselves.

Of course an OS should be free of leaks. Of course it should. But Apple's OS is not. Apple's OS is at the other end of the spectrum. The needle's pinned at the wrong end.

Not all modules suffer, to be honest. But if the core frameworks leak, then almost all software will leak, and Apple's core frameworks leak a lot.

But let's take a look at Apple's own code, let's take a look at Apple software, running on Apple frameworks, and see what we find. There's a lot of variation here. Apple applications are of course maintained by different groups, so results will vary - and that is the key. Some of these modules are well sealed, as reported by their own version of /usr/bin/leaks, and some are not. You'll quickly discern patterns.

To run leaks on any program you have on your computer, just go to a command line and type in 'leaks' plus the name (eg Mail, Safari) of the program you want to audit.

Leaks Like a Sieve | Rixstep Developers Workshop (2)

Activity Monitor

Activity Monitor is a complex application. There's a lot going on there. You don't need to do much other than start it up. Yet Activity Monitor comes with a clean bill of health.

Process: Activity Monitor [21817]Load Address: 0x107429000Identifier: Activity MonitorVersion: ???Code Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaks----Process 21817: 49730 nodes malloced for 7812 KBProcess 21817: 0 leaks for 0 total leaked bytes.

App Store

App Store is an app you'd expect to leak a lot. You'd be right.

Process: App Store [21824]Load Address: 0x10873d000Identifier: com.apple.AppStoreCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 119.3MPhysical footprint (peak): 182.3M----Process 21824: 370882 nodes malloced for 147252 KBProcess 21824: 93 leaks for 7024 total leaked bytes.

The leaks report for App Store is 168 lines long.

Disk Utility

Disk Utility is a crucial app. You need it to not leak. And for the most part it doesn't.

Process: Disk Utility [21857]Load Address: 0x101f9d000Identifier: com.apple.DiskUtilityCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 33.3MPhysical footprint (peak): 37.8M----Process 21857: 61129 nodes malloced for 21170 KBProcess 21857: 1 leak for 64 total leaked bytes.

Dock

Your Dock is the most central part of your user experience. Together with Finder, it replaces NeXT's old 'Workspace'. Your Dock is always running. You really need your Dock to not leak memory. Unfortunately, it does - it leaks a lot.

Process: Dock [19762]Load Address: 0x107e70000Identifier: com.apple.dockCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 17.8MPhysical footprint (peak): 31.6M----Process 19762: 50249 nodes malloced for 46063 KBProcess 19762: 198 leaks for 21104 total leaked bytes.

Mail

You'd expect Mail to leak a lot. And maybe it does with extended use. But not on this test run.

Process: Mail [21089]Load Address: 0x1065a7000Identifier: com.apple.mailCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 47.2MPhysical footprint (peak): 49.3M----Process 21089: 174023 nodes malloced for 35667 KBProcess 21089: 2 leaks for 96 total leaked bytes.

Preview

A real workhorse utility. It's perhaps not crucial that it not leak. But it didn't leak at all on this test run.

Process: Preview [21832]Load Address: 0x10d2f1000Identifier: com.apple.PreviewCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 24.6MPhysical footprint (peak): 25.2M----Process 21832: 77149 nodes malloced for 11700 KBProcess 21832: 0 leaks for 0 total leaked bytes.

QuickTime Player

Process: QuickTime Player [21848]Load Address: 0x100821000Identifier: com.apple.QuickTimePlayerXCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 33.0MPhysical footprint (peak): 33.3M----Process 21848: 71743 nodes malloced for 16728 KBProcess 21848: 13 leaks for 736 total leaked bytes.

Safari

Ah. Here we go. The app that makes all the others look bad. Safari has been accused of a lot of things in the past, and mostly justifiably. And don't even begin to look at the filesystem havoc.

Process: Safari [8697]Load Address: 0x105317000Identifier: com.apple.SafariCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 258.5MPhysical footprint (peak): 292.5M----Process 8697: 993923 nodes malloced for 288649 KBProcess 8697: 19019 leaks for 3574928 total leaked bytes.

Safari is leaking over three and one half MB. Its leaks report is 22,610 lines long.

Xcode

This was a surprise.

Process: Xcode [21072]Load Address: 0x103413000Identifier: com.apple.dt.XcodeCode Type: X86-64Parent Process: ??? [1]Analysis Tool: /usr/bin/leaksPhysical footprint: 78.1MPhysical footprint (peak): 78.2M----Process 21072: 366244 nodes malloced for 33983 KBProcess 21072: 0 leaks for 0 total leaked bytes.

About Rixstep

Stockholm/London-based Rixstep are a constellation of programmers and support staff from Radsoft Laboratories who tired of Windows vulnerabilities, Linux driver issues, and cursing x86 hardware all day long. Rixstep have many years of experience behind their efforts, with teaching and consulting credentials from the likes of British Aerospace, General Electric, Lockheed Martin, Lloyds TSB, SAAB Defence Systems, British Broadcasting Corporation, Barclays Bank, IBM, Microsoft, and Sony/Ericsson.

Rixstep and Radsoft products are or have been in use by Sweden's Royal Mail, Sony/Ericsson, the US Department of Defense, the offices of the US Supreme Court, the Government of Western Australia, the German Federal Police, Verizon Wireless, Los Alamos National Laboratory, Microsoft Corporation, the New York Times, Apple Inc, Oxford University, and hundreds of research institutes around the globe.

See here.

All Content and Software Copyright © Rixstep. All Rights Reserved.

CONTACT INFO:John CattelinMedia Contactcontact@rixstep.comPURCHASE INFO:ACP/Xfile licencesUser/Family/Businesshttp://rixstep.com/buy
Leaks Like a Sieve | Rixstep Developers Workshop (2024)
Top Articles
Latest Posts
Article information

Author: Edwin Metz

Last Updated:

Views: 6320

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Edwin Metz

Birthday: 1997-04-16

Address: 51593 Leanne Light, Kuphalmouth, DE 50012-5183

Phone: +639107620957

Job: Corporate Banking Technician

Hobby: Reading, scrapbook, role-playing games, Fishing, Fishing, Scuba diving, Beekeeping

Introduction: My name is Edwin Metz, I am a fair, energetic, helpful, brave, outstanding, nice, helpful person who loves writing and wants to share my knowledge and understanding with you.