<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Memory &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/memory/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Thu, 02 May 2024 15:01:57 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Avoiding Memory Leaks with WeakRef</title>
		<link>https://blog.xojo.com/2024/05/07/avoiding-memory-leaks-with-weakref/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Tue, 07 May 2024 16:11:00 +0000</pubDate>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Cast]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[WeakRef]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=12861</guid>

					<description><![CDATA[Is not that common of an issue, but it happens - when searching for memory leaks, remember to check any cross-referenced objects. Cross-referenced objects are, well, just that, objects that need to reference each other. For example, when "ClassA" needs to know about "ClassB", and "ClassB" needs to know about "ClassA". Continue reading to see how this can cause memory leaks in your projects and how to use Xojo's WeakRef to fix it.]]></description>
										<content:encoded><![CDATA[
<p>This is not that common of an issue, but&nbsp;it happens &#8211; when searching for memory leaks, remember to check any cross-referenced objects. Cross-referenced objects are, well, just that, objects that need to reference each other. For example, when &#8220;ClassA&#8221; needs to know about &#8220;ClassB&#8221;, and &#8220;ClassB&#8221; needs to know about &#8220;ClassA&#8221;. Continue reading to see how this can cause memory leaks in your projects and how to use Xojo&#8217;s WeakRef to fix it.</p>



<span id="more-12861"></span>



<p>If you&#8217;re dealing with a situation in your code where two classes need to reference each other, then you could be leaking memory if you haven&#8217;t taken steps to prevent that.</p>



<p>Let&#8217;s look at this with a simple example.</p>



<ul class="wp-block-list">
<li>Create a new Xojo project and add two new classes to it, ClassA and ClassB.</li>



<li>Add a ClassBReference As ClassB property to ClassA.</li>



<li>Add a ClassAReference As ClassA property to ClassB.</li>



<li>Add a Constructor to ClassB using the following signature:</li>
</ul>



<pre id="xojo" class="wp-block-code"><code>Constructor(reference As ClassA)</code></pre>



<p>And add this line of code to the method:</p>



<pre id="xojo" class="wp-block-code"><code>Me.ClassAReference = reference</code></pre>



<p>Next, add the Destructor method to both classes and type &#8220;Break&#8221; as the only line of code for them. This will allow us to see if they leak memory or not. For example, if when debugging the example project the Debugger doesn&#8217;t reach the Break lines, that would mean that the Destructor has not been called and, thus, the objects are not released from memory. They are leaking!</p>



<ul class="wp-block-list">
<li>Now add the Opening event handler to Window1 and type these lines into it:</li>
</ul>



<pre id="xojo" class="wp-block-code"><code>CreateReferences
Break</code></pre>



<p>As you can see, it calls the CreateReferences method and then breaks into the debugger.</p>



<ul class="wp-block-list">
<li>Now add the CreateReferences method to Window1 and type these lines of code in the associated Code Editor:</li>
</ul>



<pre id="xojo" class="wp-block-code"><code>Var ca As New ClassA
Var cb As New ClassB(ca)

ca.ClassBReference = cb</code></pre>



<p>Now it&#8217;s clear what is going on here: ClassB instance is keeping a &#8220;hard&#8221; reference to ClassA (via the ca instance), and then the ca reference is keeping a &#8220;hard&#8221; reference to the classB instance cb.</p>



<p>Run the project. Is the debugger stopping in the Break lines of ClassA and ClassB Destructors? Nope. They are leaking because they are using hard cross-references with each other.</p>



<p>You can see it more clearly when accessing Global &gt; Runtime &gt; Contents in the Debugger panel. Both objects are still alive:</p>


<div class="wp-block-image is-style-default">
<figure class="aligncenter"><img fetchpriority="high" decoding="async" width="1250" height="132" src="https://blog.xojo.com/wp-content/uploads/2024/04/Screenshot-2024-04-03-at-12.14.16.png" alt="" class="wp-image-12862" srcset="https://blog.xojo.com/wp-content/uploads/2024/04/Screenshot-2024-04-03-at-12.14.16.png 1250w, https://blog.xojo.com/wp-content/uploads/2024/04/Screenshot-2024-04-03-at-12.14.16-300x32.png 300w, https://blog.xojo.com/wp-content/uploads/2024/04/Screenshot-2024-04-03-at-12.14.16-1024x108.png 1024w, https://blog.xojo.com/wp-content/uploads/2024/04/Screenshot-2024-04-03-at-12.14.16-768x81.png 768w" sizes="(max-width: 1250px) 100vw, 1250px" /></figure>
</div>


<h3 class="wp-block-heading">WeakRef to the Rescue!</h3>



<p>But let&#8217;s say that your app design needs to keep these references. How do we solve this situation? The answer is using <a href="https://en.wikipedia.org/wiki/Weak_reference">weak references</a> instead of hard references, and the Xojo language has a class for it: <a href="https://documentation.xojo.com/api/language/weakref.html#weakref">WeakRef</a>.</p>



<p>Let&#8217;s change ClassA and ClassB properties so their type is a WeakRef. Setting their definition as:</p>



<pre id="xojo" class="wp-block-code"><code>ClassBReference As WeakRef
ClassAReference As WeakRef</code></pre>



<p>And let&#8217;s change the ClassB Constructor code so it creates a new WeakRef instance from the received object, instead of assigning it directly to the property.</p>



<pre id="xojo" class="wp-block-code"><code>ClassAReference = New WeakRef(reference)</code></pre>



<p>And let&#8217;s change the code in the CreateReferences method to:</p>



<pre id="xojo" class="wp-block-code"><code>Var ca As New ClassA<br>Var cb As New Classb(ca)<br><br>ca.ClassBReference = New WeakRef(cb)</code></pre>



<p>Run the example project again. Now you will see that the Debugger stops at the &#8220;Break&#8221; lines for each Class Destructor. Which means they are correctly released from memory. Problem solved!</p>



<h3 class="wp-block-heading">WeakRef Memory Care</h3>



<p>When using WeakRef objects through the code, keep in mind that accessing the real object they are referencing is done through the WeakRef.Value property in addition to <a href="https://documentation.xojo.com/getting_started/object-oriented_programming/advanced_oop_features.html#getting-started-object-oriented-programming-advanced-oop-features-casting">Cast</a> to the original Class.</p>



<p>To see it more clearly, add the &#8220;SayHello&#8221; method to ClassB and type this line of code into the associated code editor:</p>



<pre id="xojo" class="wp-block-code"><code>MessageBox("Hi From ClassB Instance")</code></pre>



<p>Then, select the CreateReferences method from Window1 and add the following line of code at the end:</p>



<pre id="xojo" class="wp-block-code"><code>ClassB(ca.ClassBReference.Value).SayHello</code></pre>



<p>First, we are accessing the ClassBRefence property from the ca instance; that gives us access to the WeakRef instance property, so we need to access its Value property in order to get access to the real object instance we are really interested (a ClassB instance).</p>



<p>But, because the Value property returns a generic Object, we need to Cast it to the class instance we expect (and should know) that is referenced by the Value property; in this case a &#8220;ClassB&#8221; instance. To do this, we are using the Cast expression:</p>



<pre id="xojo" class="wp-block-code"><code>ClassB(ca.ClassBReference.Value)</code></pre>



<p>Next, and because we did Cast it to ClassB, we can continue using the dot notation in order to call any of the methods / properties exposed by ClassB, in this case the &#8220;SayHello&#8221; method.</p>



<p>Run the app again and now the ClassB instance referenced by the ClassA instance is displaying the MessageBox.</p>



<p>In addition, when accessing a hard referenced object in your class properties it can be good to do a sanity check like:</p>



<pre id="xojo" class="wp-block-code"><code>If ca.ClassBReference &lt;&gt; Nil Then
  ca.ClassBReference.SayHello
End If</code></pre>



<p>But when using WeakRefs you need to do a double check. First against the WeakRef itself, because it can be a Nil object! And second, against the object referenced by the WeakRef itself. So the previous check will read now:</p>



<pre id="xojo" class="wp-block-code"><code>If ca.ClassBReference &lt;&gt; Nil And ca.ClassBReference.Value &lt;&gt; Nil then<br>  ClassB(ClassBReference.Value).SayHello<br>End If</code></pre>



<h3 class="wp-block-heading">Clearing It!</h3>



<p>All in all, cross-referencing objects among class instances is probably not the best design, but sometimes it is needed. In case your app design needs to follow such a path, take care and use weak references (with Xojo&#8217;s WeakRef class) instead of hard references.</p>



<p>Keep it in memory (or not, if it is leaking)… and happy coding with Xojo!</p>



<p><em>Javier Menendez is an engineer at Xojo and has been using Xojo since 1998. He lives in Castellón</em>, <em>Spain and hosts regular Xojo hangouts en español. Ask Javier questions on Twitter at <a href="https://twitter.com/xojoes" target="_blank" rel="noreferrer noopener">@XojoES</a> or on the <a href="https://forum.xojo.com/u/javier_menendez/summary" target="_blank" rel="noreferrer noopener">Xojo Forum</a>.</em></p>



<ul class="wp-block-social-links has-normal-icon-size is-content-justification-center is-layout-flex wp-container-core-social-links-is-layout-16018d1d wp-block-social-links-is-layout-flex"><li class="wp-social-link wp-social-link-facebook  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.facebook.com/goxojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12 2C6.5 2 2 6.5 2 12c0 5 3.7 9.1 8.4 9.9v-7H7.9V12h2.5V9.8c0-2.5 1.5-3.9 3.8-3.9 1.1 0 2.2.2 2.2.2v2.5h-1.3c-1.2 0-1.6.8-1.6 1.6V12h2.8l-.4 2.9h-2.3v7C18.3 21.1 22 17 22 12c0-5.5-4.5-10-10-10z"></path></svg><span class="wp-block-social-link-label screen-reader-text">Facebook</span></a></li>

<li class="wp-social-link wp-social-link-x  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://x.com/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M13.982 10.622 20.54 3h-1.554l-5.693 6.618L8.745 3H3.5l6.876 10.007L3.5 21h1.554l6.012-6.989L15.868 21h5.245l-7.131-10.378Zm-2.128 2.474-.697-.997-5.543-7.93H8l4.474 6.4.697.996 5.815 8.318h-2.387l-4.745-6.787Z" /></svg><span class="wp-block-social-link-label screen-reader-text">X</span></a></li>

<li class="wp-social-link wp-social-link-linkedin  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.linkedin.com/company/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M19.7,3H4.3C3.582,3,3,3.582,3,4.3v15.4C3,20.418,3.582,21,4.3,21h15.4c0.718,0,1.3-0.582,1.3-1.3V4.3 C21,3.582,20.418,3,19.7,3z M8.339,18.338H5.667v-8.59h2.672V18.338z M7.004,8.574c-0.857,0-1.549-0.694-1.549-1.548 c0-0.855,0.691-1.548,1.549-1.548c0.854,0,1.547,0.694,1.547,1.548C8.551,7.881,7.858,8.574,7.004,8.574z M18.339,18.338h-2.669 v-4.177c0-0.996-0.017-2.278-1.387-2.278c-1.389,0-1.601,1.086-1.601,2.206v4.249h-2.667v-8.59h2.559v1.174h0.037 c0.356-0.675,1.227-1.387,2.526-1.387c2.703,0,3.203,1.779,3.203,4.092V18.338z"></path></svg><span class="wp-block-social-link-label screen-reader-text">LinkedIn</span></a></li>

<li class="wp-social-link wp-social-link-github  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://github.com/topics/xojo" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M12,2C6.477,2,2,6.477,2,12c0,4.419,2.865,8.166,6.839,9.489c0.5,0.09,0.682-0.218,0.682-0.484 c0-0.236-0.009-0.866-0.014-1.699c-2.782,0.602-3.369-1.34-3.369-1.34c-0.455-1.157-1.11-1.465-1.11-1.465 c-0.909-0.62,0.069-0.608,0.069-0.608c1.004,0.071,1.532,1.03,1.532,1.03c0.891,1.529,2.341,1.089,2.91,0.833 c0.091-0.647,0.349-1.086,0.635-1.337c-2.22-0.251-4.555-1.111-4.555-4.943c0-1.091,0.39-1.984,1.03-2.682 C6.546,8.54,6.202,7.524,6.746,6.148c0,0,0.84-0.269,2.75,1.025C10.295,6.95,11.15,6.84,12,6.836 c0.85,0.004,1.705,0.114,2.504,0.336c1.909-1.294,2.748-1.025,2.748-1.025c0.546,1.376,0.202,2.394,0.1,2.646 c0.64,0.699,1.026,1.591,1.026,2.682c0,3.841-2.337,4.687-4.565,4.935c0.359,0.307,0.679,0.917,0.679,1.852 c0,1.335-0.012,2.415-0.012,2.741c0,0.269,0.18,0.579,0.688,0.481C19.138,20.161,22,16.416,22,12C22,6.477,17.523,2,12,2z"></path></svg><span class="wp-block-social-link-label screen-reader-text">GitHub</span></a></li>

<li class="wp-social-link wp-social-link-youtube  wp-block-social-link"><a rel="noopener nofollow" target="_blank" href="https://www.youtube.com/c/XojoInc" class="wp-block-social-link-anchor"><svg width="24" height="24" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false"><path d="M21.8,8.001c0,0-0.195-1.378-0.795-1.985c-0.76-0.797-1.613-0.801-2.004-0.847c-2.799-0.202-6.997-0.202-6.997-0.202 h-0.009c0,0-4.198,0-6.997,0.202C4.608,5.216,3.756,5.22,2.995,6.016C2.395,6.623,2.2,8.001,2.2,8.001S2,9.62,2,11.238v1.517 c0,1.618,0.2,3.237,0.2,3.237s0.195,1.378,0.795,1.985c0.761,0.797,1.76,0.771,2.205,0.855c1.6,0.153,6.8,0.201,6.8,0.201 s4.203-0.006,7.001-0.209c0.391-0.047,1.243-0.051,2.004-0.847c0.6-0.607,0.795-1.985,0.795-1.985s0.2-1.618,0.2-3.237v-1.517 C22,9.62,21.8,8.001,21.8,8.001z M9.935,14.594l-0.001-5.62l5.404,2.82L9.935,14.594z"></path></svg><span class="wp-block-social-link-label screen-reader-text">YouTube</span></a></li></ul>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>WeakRef and Memory Management</title>
		<link>https://blog.xojo.com/2016/04/20/weakref-and-memory-management/</link>
		
		<dc:creator><![CDATA[Javier Menendez]]></dc:creator>
		<pubDate>Wed, 20 Apr 2016 00:00:00 +0000</pubDate>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[AprendeXojo]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[Object-Oriented]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2016/04/20/weakref-and-memory-management/</guid>

					<description><![CDATA[Sometimes apps can simply reach unstable situations by their very nature. Keep memory management under control with the help of the WeakRef class.]]></description>
										<content:encoded><![CDATA[<p>Object Oriented Programming with Xojo, and in this case Event Oriented Programming as well, is simply wonderful. You create objects (Instances) from the defined classes that act as templates and just let them roll. From there, the interactions made by the user are those that determine how objects interact with each other via sending messages to each other (method calls), the access to properties and also the execution of events.</p>
<p>However, sometimes the combination can simply reach unstable situations by the very nature of our applications and here is where failures can arise in memory management. Fortunately, we can keep this under control with the help of the <a href="http://developer.xojo.com/xojo-core-weakref" target="_blank" rel="noopener">WeakRef class</a>.<br />
<span id="more-281"></span><br />
This is, in fact, what happened to me recently while implementing a new feature in <a href="http://www.aprendexojo.com/software/snippery" target="_blank" rel="noopener">Snippery</a> my productivity application where one of those unlikely but possible cases one of the objects left in an unstable state. What does this mean? The referenced Control object stored in a property was not marked as nil once it was out of scope because its containing window was closed, so it was simply unsafe (an dangerous!) to check or access its properties and methods through a simple verification of the type:</p>
<p><span style="font-family: 'courier new', courier;">if miPropiedad &lt;&gt; nil then</span></p>
<p>Despite the object referenced through the property would have been released, this condition will execute the code inside the block resulting in a unexpectedly exit from the app! In fact inspecting the object via Xojo&#8217;s debugger shows that the reference is still there but it inner structure is void.</p>
<p>So executing the previous code will bring a beautiful error message from OS X:</p>
<p><span style="font-family: 'courier new', courier;">EXC_BAD_ACCESS (SIGSEGV)</span></p>
<p>That is an attempt to access a memory vector where we expected to find a data structure that is not there anymore.</p>
<p>I have to say that after further research on the subject I have found that this kind of error does not always occur and that it may depend on the type of object that we maintain the reference to, or even the property we want to access, or the method we try to call when the objects is in this &#8220;unstable&#8221; state. In the best of the cases, simply nothing happens. In the worst case, your application will suffer an unexpected exit you will not be able to trap using the Try &#8230; Catch block or exception handling mechanism offered by the Xojo language.</p>
<p>If you want to see for yourself what I&#8217;m talking about, you can download the <a href="https://www.dropbox.com/s/l1az8r9jq0q2aps/WeakRef%20Example.zip?dl=0">example application</a> that reproduces this kind of problem related with memory management.</p>
<p><strong>WeakRef to the rescue!</strong></p>
<p>How can we solve these kinds of situations? Easy! The WeakRef class allows code to maintain weak references to objects. When you wrap the object you&#8217;re interested in an instance of the class WeakRef, we will ensure that such reference is released (and thus becomes nil) through proper memory management. So, problem solved! From this point on we can perform and trust a conditional test of the reference against &#8216;nil&#8217; before trying to access any of the properties of the object itself.</p>
<p>In the case of our sample application, we can fix the problem with the following changes:</p>
<p>1. We define the property SourceControl Worker class to be of the WeakRef Data Type<br />
2. We make the following changes to Worker class constructor (where the referenced Control instance is now wrapped into a WeakRef instance):</p>
<p><span style="font-family: 'courier new', courier;">sourceControl = new weakref( c )</span></p>
<p>3. We modify the conditional block of code for the Worker method to:</p>
<p><span style="font-family: 'courier new', courier;">if sourceControl.Value &lt;&gt; nil then</span><br />
<span style="font-family: 'courier new', courier;">  textArea(sourceControl).<wbr />SelLength = 0</span><br />
<span style="font-family: 'courier new', courier;">  textArea(sourceControl).Text = &#8220;This is a test&#8221;</span><br />
<span style="font-family: 'courier new', courier;">end if</span></p>
<p>Note that in this case we Cast SourceControl to TextArea, so  we can work with the properties and methods of the control that we know are actually a control of TextArea type.</p>
<p>Make the changes, run the app again and you&#8217;ll notice that this time the app will continue running without errors once all the windows are closed.</p>
<p>You can watch <a href="https://youtu.be/5-8iwlvNSsY" target="_blank" rel="noopener">the video</a> (in Spanish only) that talks you though this example.</p>
<p><em>Javier RodrÃ­guez has been the Xojo Spanish Evangelist since 2008, he&#8217;s also a Developer, Consultant and Trainer who has used Xojo since 1998. He is in charge of AprendeXojo.com and the developer behind the GuancheMOS plug-in for Xojo Developers and the Snippery app among others.</em></p>
<p><span style="text-decoration: underline;"><em><a href="http://www.aprendexojo.com/2016/04/weakref-y-la-gestion-de-memoria/" target="_blank" rel="noopener">*Read this post in Spanish</a>.</em></span></p>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212" class="hs-cta-wrapper"> <span id="hs-cta-abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212" class="hs-cta-node hs-cta-abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://developer.xojo.com/international-resources" target="_blank" rel="noopener"><img decoding="async" id="hs-cta-img-abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212" class="hs-cta-img aligncenter" style="border-width: 0px; margin: 0 auto; display: block; margin-top: 20px; margin-bottom: 20px;" src="https://blog.xojo.com/wp-content/uploads/2016/04/abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212.png" alt="International Resources Xojo" width="417" height="229" align="middle" /></a> </span><script src="https://js.hscta.net/cta/current.js" charset="utf-8">// <![CDATA[
<script type="text/javascript"><![CDATA[ hbspt.cta.load(608515, 'abfc1e1f-d1ee-4de3-bd9e-7bfe6930b212', {}); // ]]&gt;</script></span><br />
<!-- end HubSpot Call-to-Action Code --></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Sorry, You&#8217;ve Run Out of Memory</title>
		<link>https://blog.xojo.com/2016/03/04/sorry-youve-run-out-of-memory/</link>
		
		<dc:creator><![CDATA[Geoff Perlman]]></dc:creator>
		<pubDate>Fri, 04 Mar 2016 00:00:00 +0000</pubDate>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Xojo Cloud]]></category>
		<category><![CDATA[Memory]]></category>
		<guid isPermaLink="false">http://blogtemp.xojo.com/2016/03/04/sorry-youve-run-out-of-memory/</guid>

					<description><![CDATA[Tips all developers need to know to manage memory in your apps from desktop to web to mobile.]]></description>
										<content:encoded><![CDATA[<p>Most of us build apps without thinking too much about how much memory the app will need. Sure sometimes you end up creating an app that is a real memory buster but that&#8217;s unusual. With virtual memory, gone are the days when your app would just run out of memory and crash, or are they?</p>
<p><span id="more-320"></span></p>
<p>Client apps for the desktop and mobile are pretty straight-forward in terms of both determining how much memory they will use and testing the app since only one person uses the app at a time.  Even if the app does run out of memory, it only affects one user. When building web apps, web services or really just about any app that&#8217;s going to run on a server in the cloud or otherwise, the risk is far greater. If the app crashes, every user is potentially affected.  Determining the memory needed is more difficult as well. The primary reason is that you will have potentially many users accessing the app at the same time and each using varying amounts of memory. The situation is made worse by the fact that most modern operating systems no longer manage memory behind the scenes in the same simplistic way they once did.</p>
<h2>Getting on TOP of It</h2>
<p>The most common server operating system is of course Linux and the tool of choice for knowing how your apps are behaving is the UNIX app know as <em>TOP</em>. TOP provides a lot of interesting though potentially confusing and even misleading information.</p>
<p><img decoding="async" style="width: 640px;" title="TOP.png" src="https://blog.xojo.com/wp-content/uploads/2016/03/TOP.pngt1466486449161ampwidth640" sizes="(max-width: 640px) 100vw, 640px" alt="TOP.png" width="640" /></p>
<p>Here TOP is showing us information about the server in general such as the average load, the number of tasks, how busy the CPU is, how much memory the server has and how much is available, etc. Below that, TOP breaks down information about each process, some of which are apps while others (in this case) are connections to an app. The first three processes in this case are web apps.  The most important columns are VIRT, RES and %CPU.</p>
<p><strong>VIRT</strong> (Virtual Memory) indicates how much virtual memory the app is using for data. For example, the EEWeb-Dev app is using 81504K (81.5Mb). This value will go up and down as your app uses more or less memory for data.</p>
<p><strong>RES</strong> (Resident Memory) indicates phyiscal memory used by the app/process.</p>
<p><strong>%CPU</strong> indicates how much of the CPU the app is using at this moment. It&#8217;s not unusual for a web app to occasionally use a lot of CPU and then a moment later use very little.</p>
<p><strong>Note</strong>: Different Linux distributions display VIRT and RES differently. CentOS, for example, displays these values  in K unless there&#8217;s not enough room. So 81504 is 81.5 Megabytes. 41m is 41 Megabytes. <strong>To determine how much memory your app is using, you&#8217;d add VIRT and RES together.</strong></p>
<p>Considering all the information available, you&#8217;d think it would be easy enough to determine how much memory your app is using. After all, TOP shows you how much memory is free. Isn&#8217;t that enough? Unfortunately, it&#8217;s not that simple. You might think that as soon as your app stops using memory (for a user connection for example) that it&#8217;s instantly free and available for other apps to use. Nothing could be further from the truth. The OS has a lot of priorities only one of which is dealing with memory that has suddenly become available. Like you, it prioritizes based upon need. If there&#8217;s plenty of free memory already, it may not bother cleaning up memory freed by your apps for some time. If another app suddenly needs a lot of memory and the free memory won&#8217;t suffice, the OS will immediately kick in and collect all the freed memory from various apps. However, if nothing puts pressure on the OS to do this, it could be anywhere from a few seconds to more than an hour before the OS cleans memory up. This means simply monitoring TOP for a few minutes isn&#8217;t going to be enough.</p>
<p>If you&#8217;re using <a href="https://documentation.xojo.com/topics/xojo_cloud/introduction_to_xojo_cloud.html">Xojo Cloud</a> you don&#8217;t have to worry about any of this. Starting with Xojo 2018 Release 1, <a href="https://documentation.xojo.com/topics/xojo_cloud/introduction_to_xojo_cloud.html_General_Information#Statistics">Xojo has built-in server statistics</a> that you can easily display to show CPU, memory, network and disk statistics for the entire server and individual apps.<img decoding="async" class="alignnone wp-image-5468" src="https://blog.xojo.com/wp-content/uploads/2016/03/XojoCloudStats.png" alt="" width="600" height="379" /></p>
<h2>Leaking Memory</h2>
<p>If you have the kind of app that gets used only during specific hours, you could take a look at memory usage before everyone starts using it, again when everyone is working and finally an hour after they finish. Unless your app is designed to hold on to information in memory even after users have shut down for the day, the memory available should have returned to approximately what they were when you started. If your app is still using a lot of memory long after all your users have quit, you&#8217;ve probably got a memory leak.<br />
<img decoding="async" style="width: 640px; margin: 0px auto 10px; display: block;" title="Leak.jpeg" src="https://blog.xojo.com/wp-content/uploads/2016/03/Leak.jpegt1466486449161ampwidth640" sizes="(max-width: 640px) 100vw, 640px" alt="Leak.jpeg" width="640" /><br />
A memory leak occurs when memory is allocated for something but then never released. If a leaking function is called over and over, the amount of memory being used continues to climb potentially until there&#8217;s none left and the application crashes. The most common reason for this is your code has created some kind of data object and stored a reference to it somewhere but the reference never goes out of scope. Say you&#8217;re storing the reference in some global part of your application code. Unless something clears the reference, the memory will continue to be used indefinitely.</p>
<h2>How Much Memory do I Need?</h2>
<p>If your app or apps are behaving themselves, the memory used goes up and comes back down later on. In that case, the question is, how much total memory do you really need? In computing, we are used to very black and white answers. In this case, the answer is not so satisfying. If your server runs out of memory, the client is going to get disconnected and ideally, provided an appropriate error message. Web apps made in <a href="http://www.xojo.com" target="_blank" rel="noopener">Xojo</a> for example, display such a message to the end user indicating that an Out of Memory Exception has occurred. In this case, when a user reports this to you, you&#8217;ll need to determine if your app is leaking memory or simply needs more of it. Now, you&#8217;re probably thinking, &#8220;Is there any way for my app to know that it&#8217;s about to run out of memory?&#8221; Not really. It&#8217;s using memory from a pool available to all apps. Even if your app could monitor how much memory is free, it doesn&#8217;t know if other apps or the OS itself are about to make more available. Once the Out of Memory Exception (or the equivalent error message in the language you are using) occurs, there&#8217;s usually not enough memory left to confidentially do anything but crash.</p>
<p>The good news is that there&#8217;s a relatively painless remedy. Memory is cheap. If you have your own server, buying more RAM is an inexpensive solution. It&#8217;s probably worth buying a lot more than you think you will need. It&#8217;s the lowest cost insurance policy you will ever buy. If you are using a cloud hosting solution (like <a href="http://www.xojo.com/xojocloud" target="_blank" rel="noopener">Xojo Cloud</a> for example), then getting more memory involves upgrading your server. In the case of <a href="http://www.xojo.com/cloud/" target="_blank" rel="noopener">Xojo Cloud</a> and most cloud-based solutions, this is very simple, usually involving shutting down the server for a few minutes then bringing it back up again with more memory allocated.</p>
<h2>The Solution: Trial and Error</h2>
<p>The best preemptive measure is to find your app&#8217;s memory high-water mark. Monitor the memory when usage peaks, ideally over several hours, to see how close you are getting to running out of memory. This will at least give you some rough sense for your situation allowing you to remedy it before your users crash the app and cause a lot of headaches for each other and for you.</p>
<p>Determining how much memory your app needs, at least for most people, is a question of trial and error. This is because operating systems don&#8217;t free up memory instantly making it difficult to judge how much memory the app is really using at any given moment. The good news is that despite this, adding more RAM to a server is cheap and easy.</p>
<p><!--HubSpot Call-to-Action Code --> <span id="hs-cta-wrapper-ba92317e-008d-4cab-85c4-c301dd8f0dd8" class="hs-cta-wrapper"> <span id="hs-cta-ba92317e-008d-4cab-85c4-c301dd8f0dd8" class="hs-cta-node hs-cta-ba92317e-008d-4cab-85c4-c301dd8f0dd8"><br />
<!-- [if lte IE 8]>


<div id="hs-cta-ie-element"></div>


<![endif]--> <a href="http://www.xojo.com/download/"><img loading="lazy" decoding="async" id="hs-cta-img-ba92317e-008d-4cab-85c4-c301dd8f0dd8" class="hs-cta-img aligncenter" style="border-width: 0px; margin: 0 auto; display: block; margin-top: 20px; margin-bottom: 20px;" src="https://blog.xojo.com/wp-content/uploads/2014/03/ba92317e-008d-4cab-85c4-c301dd8f0dd8.png" alt="Download Xojo" width="180" height="47" align="middle" /></a></span></span> <!-- end HubSpot Call-to-Action Code --></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
