<?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>progress.solvepoint.com &#187; Memory</title>
	<atom:link href="http://progress.solvepoint.com/tag/memory/feed/" rel="self" type="application/rss+xml" />
	<link>http://progress.solvepoint.com</link>
	<description>Progress Blog - OpenEdge, WebSpeed, Sonic, 4GL, ABL, QAD, Epicor, and all things Progress</description>
	<lastBuildDate>Sat, 15 May 2010 18:29:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OpenEdge ABL Memptr Pitfalls</title>
		<link>http://progress.solvepoint.com/2007/11/30/openedge-abl-memptr-pitfalls/</link>
		<comments>http://progress.solvepoint.com/2007/11/30/openedge-abl-memptr-pitfalls/#comments</comments>
		<pubDate>Fri, 30 Nov 2007 22:18:54 +0000</pubDate>
		<dc:creator>JohnK</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[Memory]]></category>

		<guid isPermaLink="false">http://progress.solvepoint.com/2007/11/30/openedge-abl-memptr-pitfalls/</guid>
		<description><![CDATA[Memptr is a very powerful datatype in the ABL/4GL.  It allows the programmer to store any type of data including binary.  However, as with all dynamic objects in Progress, one must be careful when using it.
Pitfall number one:  Scope. Memptrs do not follow the rules of scope to which 4GL programmers have [...]]]></description>
			<content:encoded><![CDATA[<p>Memptr is a very powerful datatype in the ABL/4GL.  It allows the programmer to store any type of data including binary.  However, as with all dynamic objects in Progress, one must be careful when using it.</p>
<p><strong>Pitfall number one</strong>:  Scope. Memptrs do not follow the rules of scope to which 4GL programmers have become accustomed.</p>
<p>What does this mean?  Why do I care?  Well, I&#8217;ll tell you. Since Memptrs do not follow the rules of scope, when a variable holding a Memptr HANDLE goes out of scope the associated memory is NOT released.  You care because if this happens you now have a memory leak.  Every time your program is executed it will leak memory equal to the amount allocated to your memptr.</p>
<p><strong>Pitfall number two</strong> is the allocation process.  Probably 99% of code you find will do this:</p>
<blockquote></blockquote>
<blockquote>
<pre><font color="#8b0000">/* define a memptr variable */</font></pre>
<pre><font color="#8b0000">def var m as memptr no-undo.</font></pre>
<pre><font color="#8b0000">/* Allocate the memory */</font></pre>
<pre><font color="#8b0000">set-size(m) = 1024.</font></pre>
<pre><font color="#8b0000">/* now go ahead and start using it... */</font></pre>
<blockquote></blockquote>
<pre><font color="#8b0000">...</font></pre>
</blockquote>
<p>See anything wrong with the above?  If not, don&#8217;t blame yourself.  You are used to Progress doing this for you, but in this case, it does not.  What am I referring to?  You C programmers will know!  The memory that has been allocated to m has not yet been &#8220;initialized&#8221;.  This means the memory will contain random data:  whatever happened to be in there before the allocation.  If you are using put-string before your first get-string (without the numbytes parameter) then you have nothing to worry about since put-string automatically puts a NULL (0) as the next byte after the string and get-string will only read up to that NULL.  But for other operations like put/get-byte or put/get-bytes or put/get-string with the numbytes parameter, grabbing random uninitialized data out of memory could bite you, so beware.</p>
<p><strong>The final pitfall </strong>is also related to allocation.  In your code, you may define a memptr at the beginning of a procedure and then use it in several places throughout the code.   In each use you will want to allocate the appropriate amount of memory.  So you may code something like this:</p>
<blockquote>
<pre><font color="#8b0000">def var m as memptr no-undo.</font></pre>
<pre><font color="#8b0000">set-size(m) = 128.</font></pre>
<pre><font color="#8b0000">/* do stuff with it here... */</font></pre>
<pre><font color="#8b0000">set-size(m) = 1024.</font></pre>
<pre><font color="#8b0000">/* do other stuff with it here ... */</font></pre>
<pre><font color="#8b0000">and so on...</font></pre>
<pre><font color="#8b0000">/* now we clean up and return */</font></pre>
<pre><font color="#8b0000">set-size(m) = 0.</font></pre>
<pre><font color="#8b0000">return.</font></pre>
</blockquote>
<p>This looks great right?  We are allocating and cleaning up just as we should, right?  Well, yes and no.  The pitfall is that the second set-size where we allocate 1024 bytes doesn&#8217;t actually allocate anything.  It essentially does nothing at all.  AND it does not raise an error condition.  So now we have a potential bug if the code attempts to put more than 128 bytes into that memptr.</p>
<p>This is solved by setting the memptr to 0 first.</p>
<p>Moral of the story, memptrs need special care as they do not particpate in conventional scoping and cannot be resized until they are cleared.</p>
<p>Hope this helps to save you some time in your coding efforts!</p>
]]></content:encoded>
			<wfw:commentRss>http://progress.solvepoint.com/2007/11/30/openedge-abl-memptr-pitfalls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenEdge Memory Management Anti-pattern</title>
		<link>http://progress.solvepoint.com/2007/11/30/openedge-anti-pattern/</link>
		<comments>http://progress.solvepoint.com/2007/11/30/openedge-anti-pattern/#comments</comments>
		<pubDate>Fri, 30 Nov 2007 17:56:31 +0000</pubDate>
		<dc:creator>JohnK</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[Anti-patterns]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://progress.solvepoint.com/2007/11/30/openedge-anti-pattern/</guid>
		<description><![CDATA[Hello all,
I&#8217;ve recently been reviewing some Progress 4GL and have found an all too common anti-pattern related to memory management.
When a variable is defined, the Progress runtime client (Virtual Machine) allocates memory at runtime for that variable.   Once the variable is out of scope, the memory is released and everyone is happy.  [...]]]></description>
			<content:encoded><![CDATA[<p>Hello all,<br />
I&#8217;ve recently been reviewing some Progress 4GL and have found an all too common anti-pattern related to memory management.</p>
<p>When a variable is defined, the Progress runtime client (Virtual Machine) allocates memory at runtime for that variable.   Once the variable is out of scope, the memory is released and everyone is happy.   Progress programmers have grown comfortable with this design and obliviously define variables whenever they are needed knowing that they will be de-allocated automagically by the Progress VM.</p>
<p>Then came dynamic objects.</p>
<p>Progress programmers were overjoyed!  They could now create temp-tables, buttons, queries all on the fly at runtime.  No more convoluted if-then statements or .i&#8217;s or having to code a different &#8220;for each&#8221; for every combination of where clause.</p>
<p>However, as with any power bestowing feature, there is a dark side to this wonderful new world of dynamic 4gl:  memory management.  Most programmers never really stopped to consider the fact that if something is created dynamically at runtime, the VM has no way of knowing the <u>scope</u>.  It cannot tell <u>when</u> to release the memory required for the dynamic object.  REMEMBER:  the scope of the variable you happen to assign the object to HAS NO BEARING on the scope of the OBJECT since it can be passed around.  In other words, the scope of the variable holding the HANDLE to the object is NOT bound to the OBJECT itself.  The scope of ALL OBJECTS are always at the SESSION.  This applies to GUI widgets, dynamic queries, temp-tables, etc.</p>
<p>Java (and other VM&#8217;s) solve this through the use of a separate execution thread running concurrently called a Garbage Collector.  Its job is to scan memory and find dynamic objects that are no longer &#8220;reachable&#8221; and release their memory.  Unfortunately, the Progress VM has no such thread/concept.</p>
<p>To add insult to injury, not only does this leak memory but it also causes progressively worse performance:  The more widgets in memory, the more time it takes to create another widget.  Here are three examples (run on a 2.1ghz processor):</p>
<h3>Button Handles</h3>
<p><img src="http://progress.solvepoint.com/wp-content/uploads/2007/12/create1000buttonhandles.gif" alt="Create 1000 Button Handles" /></p>
<blockquote>
<pre>Minimum memory required/lost per Button Handle: 512 bytes</pre>
</blockquote>
<h3>Query Handles</h3>
<p><img src="http://progress.solvepoint.com/wp-content/uploads/2007/12/create1000queryhandles.gif" alt="Create 1000 Query Handles" /></p>
<blockquote>
<pre>Minimum memory required/lost per Query Handle: 1024 bytes</pre>
</blockquote>
<h3>Temp-Table Handles</h3>
<p><img src="http://progress.solvepoint.com/wp-content/uploads/2007/12/create1000temptablehandles.gif" alt="Create 1000 Temp-Table Handles" /></p>
<blockquote>
<pre>Min. memory required/lost per Temp-Table Handle: 512 bytes</pre>
</blockquote>
<p>So, as you can see, from both a memory and CPU footprint standpoints, it is very important to be sure to clean up your objects.</p>
<p>This may seem like a large number of handles, but remember two important points:<br />
1. This is at the <u>session</u> level.  This means that if a.p calls b.p which creates objects then those will exist for the life of the session: THERE IS NO SCOPE other than SESSION FOR DYNAMIC OBJECTS and they are NEVER automatically reclaimed!<br />
2. If the programs are running as part of a long-running session such as AppServer, EagleIQ server or Webspeed, then you have to consider the cumulative affect over days, weeks or months.<br />
Also note that if it is a temp-table, it could potentially have a much larger memory footprint.</p>
<p>So, what must be done?<br />
It is up to the Progress programmer to clean up each and every dynamic object created using the &#8220;delete object&#8221; command.<br />
It may be appropriate to create a widget-pool in which to assign your objects so you can just delete the pool and all the objects within will be released as well.  In fact, if you create a non-persistent widget-pool, it will be automatically deleted when it goes out of scope.  Creating the object into a non-persistent pool will make it behave as if it were scoped at the level that the widget-pool is created:  in effect, making it behave as if it were statically defined.</p>
<p>If you don&#8217;t use a non-persistent widget-pool, then It is also important to be sure the &#8220;clean up&#8221; code is executed even when there is an error.  For example, the following will bleed memory if an error condition is raised within the blah blah:</p>
<pre><font color="#8b0000">      procedure doQuery:
          def var qh as handle.
          create query qh.
          /* so some business logic here */
          do while true:
               blah blah
          end.
          delete object qh.
     end.</font></pre>
<p>However,  if you create a non-persistent widget pool, then it is automatically deleted when it goes out of scope.  So the following will not leak memory even if an error condition happens:</p>
<blockquote>
<pre><font color="#8b0000">procedure doQuery:</font>
<font color="#8b0000">    def var qh as handle.</font>
<font color="#8b0000">    create widget-pool "wp".</font>
<font color="#8b0000">    create query qh in widget-pool "wp".</font>
<font color="#8b0000">    /* so some business logic here */</font>
<font color="#8b0000">    do while true:</font>
<font color="#8b0000">         blah blah</font>
<font color="#8b0000">    end.</font>
<font color="#8b0000">    delete object qh.</font>
<font color="#8b0000">end.</font></pre>
</blockquote>
<p>The widget-pool may be defined at the .p level as well.  In this case the pool is deleted when the .p is exited.</p>
<p>Oh, by the way, persistent procedures and memptr&#8217;s  are two other constructs that have a session level scope.  However, they <u>cannot</u> be part of a widget-pool and therefore must be handled individually.</p>
]]></content:encoded>
			<wfw:commentRss>http://progress.solvepoint.com/2007/11/30/openedge-anti-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

