Nat! bio photo

Nat!

Senior Mull

Twitter Github Twitch

retain/release one more time + data

In the previous entry I made an assumption about the lifetime of objects in a Objective-C program (See: Probabilities). This entry will provide some data, that make this assumption more plausible and to refine it.

So here are the results of two experiments. I wrote some code, that measured the number of releases on NSObject based objects. I then dropped that into the Apple developer examples Sketch and TextEdit. I ran both for a short while I did some manipulations like loading files, editing, copy/pasting et.c and then on Quit, my code collected and printed the statistics data.

Sketch TextEdit
Download Sketch.txt Download TextEdit.txt
total object count = 26065

1       14016   0.537733
2       9598    0.368233
3       1090    0.041819
4       412     0.015807
5       145     0.005563
6       46      0.001765
7       67      0.002570
8       44      0.001688
9       236     0.009054
10      36      0.001381
total object count = 13652

1       7056    0,516847
2       4420    0,323762
3       1033    0,075667
4       241     0,017653
5       167     0,012233
6       11      0,000806
7       128     0,009376
8       62      0,004541
9       165     0,012086
10      38      0,002783

The first column has the release count number. The second column shows the number of objects. Finally the third row contains the weight of the release count number (#objects / total). In this table I only show the first 10 release count statistics, to keep it simple.

So for example for Sketch, there were 1090 objects that got 3 releases before they dealloced. (This implies they were retained twice). So because 26065 objects existed during this run of Sketch, the probability that an object would die with 3 releases is 0.041819.

What it means

Clearly the assumption was much too conservative. Instead of a gaussian-ish distribution, the distribution looks more like a logarithmic curve. In these scenarios 90% of all objects never get more than three releases. Half of them are never retained even once.

Let's redo the math for Sketch, with the new numbers, collapsing the releases from 6 to the maximum release count into 6 to keep it simple and recognizable:

# releases (n) percentage factor
1 53% 0.53
2 37% 0.37
3 4% 0.04
4 2% 0.02
5 1% 0.01
6 3% 0.03
= factor( n) * tM * n
= 0.53 * 1 + 0.37 * 2 + 0.04 * 3 + 0.02 * 4 + 0.01 * 5 + 0.03 * 6
= 0.53 + 0.74 + 0.12 + 0.08 + 0.05 + 0.18
= 1.7
= factor( n) *  (tS * n + tM * (n - 1))
= factor( n) * (4/3 * n - 1)

= 0.53*0.33+0.37*1.66+0.04*3+0.02*4.33+0.01*5.66+0.03*7
= 0.17 + 0.61 + 0.12 + 0.087 + 0.057 + 0.21
= 1,25

Final Words

"old" is now estimated to be (1.7 - 1.25) * 100 / 1.25 = 36 % slower than "new" in Sketch, if tS is 1/3 of tM. The previous estimate was 13.7%.