<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">You’re almost certainly seeing side effects of not keeping the instance of TestClass alive long enough for the unsafe pointer to still refer to an allocated malloc block. &nbsp;A quick REPL session illustrates this fact:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp;&nbsp;1&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">class TestClass {&nbsp;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp; 2. </span><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; let a = 0&nbsp;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp; 3. </span><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; let b: Int? = nil&nbsp;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(169, 169, 169);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; 4. </span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">}</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp;&nbsp;5&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">import Foundation</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp; 6&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">malloc_size(unsafeAddressOf(TestClass()))&nbsp;</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">$R0: Int = 0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp; 7&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">let x = TestClass()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">x: TestClass = {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; a = 0</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; b = nil</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #a9a9a9" class="">&nbsp; 8&gt; </span><span style="font-variant-ligatures: no-common-ligatures" class="">malloc_size(unsafeAddressOf(x))</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(52, 187, 199);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">$R1: Int = 48</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></span></div><div class="">On line 6 the result of the TestClass() expression is being used only to call unsafeAddressOf, and then it’s being freed before the call to malloc_size. &nbsp;On line 8 the variable x is retaining the instance so the malloc’ed region can be measured.</div><div class=""><br class=""></div><div class="">Of course all of this relies on the implementation assumption that allocated objects go on malloc’s heap and is subject to implementation details about object layout. &nbsp;So while this may be instructive now it’s certainly no guarantee of how things will work in the future.</div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><br class=""></span></div><div class="">
<div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="" style="font-family: LucidaGrande; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><font color="#424242" class="" style="font-family: 'Lucida Grande'; font-size: x-small;">Kate Stone</font><span class="" style="font-family: 'Lucida Grande'; font-size: x-small;">&nbsp;</span><font color="#009193" class="" style="font-family: 'Lucida Grande'; font-size: x-small;"><a href="mailto:k8stone@apple.com" class="">k8stone@apple.com</a></font></div><div class="" style="font-family: Times; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><font face="Lucida Grande" size="1" class=""><font color="#009193" class=""></font>&nbsp;Xcode&nbsp;<font color="#424242" class="">Low Level Tools</font></font></div></div></div></div></div></div>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Mar 23, 2016, at 5:13 PM, Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com" class="">howard.lovatt@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">@Kate,<div class=""><br class=""></div><div class="">I don't seem to be able to get `malloc_...` to work. EG:</div><div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="">class TestClass {<br class="">&nbsp; &nbsp; let a = 0<br class="">&nbsp; &nbsp; let b: Int? = nil<br class="">}<br class="">@_silgen_name("swift_class_getInstanceExtents") func swift_class_getInstanceExtents(theClass: AnyClass) -&gt; (negative: UInt, positive: UInt)<br class="">print("swift_class_getInstanceExtents = \(swift_class_getInstanceExtents(TestClass))")<br class="">let requiredSize = malloc_size(unsafeAddressOf(TestClass()))<br class="">print("malloc_size = \(requiredSize)")<br class="">print("malloc_good_size = \(malloc_good_size(requiredSize))")<br class=""></blockquote><br class="">Prints:<div class=""><br class=""></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class="">swift_class_getInstanceExtents = (0, 33)<br class="">malloc_size = 0<br class="">malloc_good_size = 16<br class=""></blockquote><br class=""><div class="">The `swift_class_getInstanceExtents` seems correct to me: 16 bytes for class overhead + 16 bytes for `a` and `b` + 1 byte because `b` is an optional = 33 bytes.</div><div class=""><br class=""></div><div class="">Not sure what `malloc_...` is giving?</div></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 24 March 2016 at 10:39, Kate Stone <span dir="ltr" class="">&lt;<a href="mailto:k8stone@apple.com" target="_blank" class="">k8stone@apple.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">I definitely concur that tools like Instruments are the best way to understand the impact of decisions on memory across the board.&nbsp; For fine-grained inquiries about memory use you can also rely on malloc family functions to make inquiries:<div class=""><br class=""></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px" class=""><div class="">let required_size = malloc_size(unsafeAddressOf(<i class="">object_reference</i>))</div><div class="">let actual_size = malloc_good_size(required_size)</div></blockquote><div class=""><br class=""><div class="">
<div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><div style="letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; word-wrap: break-word;" class=""><div style="font-family:LucidaGrande;word-wrap:break-word" class=""><div style="word-wrap:break-word" class=""><font color="#424242" style="font-family:'Lucida Grande';font-size:x-small" class="">Kate Stone</font><span style="font-family:'Lucida Grande';font-size:x-small" class="">&nbsp;</span><font color="#009193" style="font-family:'Lucida Grande';font-size:x-small" class=""><a href="mailto:k8stone@apple.com" target="_blank" class="">k8stone@apple.com</a></font></div><div style="font-family:Times;word-wrap:break-word" class=""><font face="Lucida Grande" size="1" class=""><font color="#009193" class=""></font>&nbsp;Xcode&nbsp;<font color="#424242" class="">Low Level Tools</font></font></div></div></div></div></div></div>
</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Mar 23, 2016, at 3:59 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""></div></div><div class=""><div class=""><div class="h5"><div dir="ltr" class="">Thanks, I will give that a try</div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="">&nbsp; -- Howard.<br class=""></div></div>
<br class=""><div class="gmail_quote">On 24 March 2016 at 03:17, Jens Alfke <span dir="ltr" class="">&lt;<a href="mailto:jens@mooseyard.com" target="_blank" class="">jens@mooseyard.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Mar 22, 2016, at 11:04 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class=""><div class=""><div dir="ltr" style="font-family:Alegreya-Regular;font-size:15px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" class="">I am writing custom collection classes and trying to assess which one is better, both in terms of performance and memory usage. Won't be used in 'real' code, just to guide development.</div></div></blockquote></div><br class=""><div class="">You might consider using heap profiling tools too, like (on Mac OS) the Instruments app or the `heap` command-line tool. If you use these while running a benchmark app using your API, it can show you how much total heap space gets used.</div><div class=""><br class=""></div><div class="">Actual heap usage can differ from the raw “sizeof” a data type, since allocators will often round up block sizes or return a somewhat larger block than necessary. Heap fragmentation can also increase memory usage beyond what you’d expect, and different allocation patterns can affect fragmentation.</div><span class=""><font color="#888888" class=""><div class=""><br class=""></div><div class="">—Jens</div></font></span></div></blockquote></div><br class=""></div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>