<p>How do I forcefully unload a <code>ByteArray</code> from memory using ActionScript 3?</p>

<p>I have tried the following:</p>

<pre><code>// First non-working solution
byteArray.length = 0;
byteArray = new ByteArray();

// Second non-working solution
for ( var i:int=0; i &lt; byteArray.length; i++ ) {
    byteArray[i] = null;
}
</code></pre>

[{'answer_id': 52, 'author': 'Jax', 'author_id': 23, 'author_profile': 'https://Stackoverflow.com/users/23', 'pm_score': 4, 'selected': False, 'text': '<p>Have a look at this article</p>\n\n<p><a href="http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html" rel="noreferrer">http://www.gskinner.com/blog/archives/2006/06/as3_resource_ma.html</a></p>\n\n<p>IANA actionscript programmer, however the feeling I\'m getting is that, because the garbage collector might not run when you want it to.</p>\n\n<p>Hence\n<a href="http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/" rel="noreferrer">http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/</a></p>\n\n<p>So I\'d recommend trying out their collection code and see if it helps</p>\n\n<pre><code>private var gcCount:int;\nprivate function startGCCycle():void{\n    gcCount = 0;\n    addEventListener(Event.ENTER_FRAME, doGC);\n}\nprivate function doGC(evt:Event):void{\n    flash.system.System.gc();\n    if(++gcCount &gt; 1){\n        removeEventListener(Event.ENTER_FRAME, doGC);\n        setTimeout(lastGC, 40);\n    }\n}\nprivate function lastGC():void{\n    flash.system.System.gc();\n}\n</code></pre>\n'}, {'answer_id': 53, 'author': 'Karl Seguin', 'author_id': 34, 'author_profile': 'https://Stackoverflow.com/users/34', 'pm_score': 5, 'selected': False, 'text': '<p>(I\'m not positive about this, but...)</p>\n<p>AS3 uses a non-deterministic garbage collection which means that dereferenced memory will be freed up whenever the runtime feels like it (typically not unless there\'s a reason to run, since it\'s an expensive operation to execute). This is the same approach used by most modern garbage collecting languages (like C# and Java as well).</p>\n<p>Assuming there are no other references to the memory pointed to by <code>byteArray</code> or the items within the array itself, the memory will be freed at some point after you exit the scope where <code>byteArray</code> is declared.</p>\n<p>You can force a garbage collection, though you really shouldn\'t. If you do, do it only for testing. If you do it in production, you\'ll hurt performance much more than help it.</p>\n<p>To force a GC, try (yes, twice):</p>\n<pre><code>flash.system.System.gc();\nflash.system.System.gc();\n</code></pre>\n<p><a href="http://www.craftymind.com/2008/04/09/kick-starting-the-garbage-collector-in-actionscript-3-with-air/" rel="nofollow noreferrer">You can read more here</a>.</p>\n'}, {'answer_id': 60, 'author': 'Redbaron', 'author_id': 41, 'author_profile': 'https://Stackoverflow.com/users/41', 'pm_score': 4, 'selected': False, 'text': "<p>Unfortunately when it comes to memory management in <em>Flash/actionscript</em> there isn't a whole lot you can do. ActionScript was designed to be easy to use (so they didn't want people to have to worry about memory management)</p>\n\n<p>The following is a workaround, instead of creating a <code>ByteArray</code> variable try this.</p>\n\n<pre><code>var byteObject:Object = new Object();\n\nbyteObject.byteArray = new ByteArray();\n\n...\n\n//Then when you are finished delete the variable from byteObject\ndelete byteObject.byteArray;\n</code></pre>\n\n<p>Where <code>byteArray</code> is a dynamic property of <code>byteObject</code>, you can free the memory that was allocated for it.</p>\n"}, {'answer_id': 81, 'author': 'Redbaron', 'author_id': 41, 'author_profile': 'https://Stackoverflow.com/users/41', 'pm_score': 4, 'selected': False, 'text': "<p>I believe you have answered your own question.</p>\n<p><code>System.totalMemory</code> gives you the total amount of memory being &quot;used&quot;, not allocated. It is accurate that your application may only be using 20 MB, but it has 5 MB that is free for future allocations.</p>\n<p>I'm not sure whether the Adobe docs would shed light on the way that it manages memory.</p>\n"}, {'answer_id': 11340, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 5, 'selected': False, 'text': "<p>I don't think you have anything to worry about. If <code>System.totalMemory</code> goes down you can relax. It may very well be the OS that doesn't reclaim the newly freed memory (in anticipation of the next time Flash Player will ask for more memory).</p>\n\n<p>Try doing something else that is very memory intensive and I'm sure that you'll notice that the memory allocated to Flash Player will decrease and be used for the other process instead.</p>\n\n<p>As I've understood it, memory management in modern OS's isn't intuitive from the perspective of looking at the amounts allocated to each process, or even the total amount allocated.</p>\n\n<p>When I've used my Mac for 5 minutes 95% of my 3 GB RAM is used, and it will stay that way, it never goes down. That's just the way the OS handles memory.</p>\n\n<p>As long as it's not needed elsewhere even processes that have quit still have memory assigned to them (this can make them launch quicker the next time, for example).</p>\n"}, {'answer_id': 81683, 'author': 'Pedro', 'author_id': 15524, 'author_profile': 'https://Stackoverflow.com/users/15524', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n<p>So, if I load say 20MB from MySQL, in the Task Manager the RAM for the application goes up by about 25MB. Then when I close the connection and try to dispose the ByteArray, the RAM never frees up. However, if I use System.totalMemory, flash player shows that the memory is being released, which is not the case.</p>\n<p>Is the flash player doing something like Java and reserving heap space and not releasing it until the app quits?</p>\n</blockquote>\n<p>Well yes and no, as you might have read from countless blog posts that the GC in AVM2 is optimistic and will work its own mysterious ways. So it does work a bit like Java and tries to reserve heap space. However if you let it long enough and start doing other operations that are consuming some significant memory, it will free that previous space. You can see this using the profiler overnight with some tests running on top of your app.</p>\n'}, {'answer_id': 175520, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<blockquote>\n  <p>So, if I load say 20MB from MySQL, in the Task Manager the RAM for the application goes up by about 25MB. Then when I close the connection and try to dispose the ByteArray, the RAM never frees up. However, if I use System.totalMemory, flash player shows that the memory is being released, which is not the case.</p>\n</blockquote>\n\n<p>The player is "releasing" the memory. If you minimize the window and restore it you should see that the memeory is now much closer to what System.totalMemory shows.</p>\n\n<p>You might also be interested in using FlexBuilder\'s profiling tools which can show you if you really have memory leaks.</p>\n'}, {'answer_id': 74515081, 'author': 'Manish', 'author_id': 3958207, 'author_profile': 'https://Stackoverflow.com/users/3958207', 'pm_score': 0, 'selected': False, 'text': '<p>Use <code>bytearray.clear()</code></p>\n<p>As per the <a href="https://airsdk.dev/reference/actionscript/3.0/" rel="nofollow noreferrer">Language Reference</a></p>\n<p>this</p>\n<blockquote>\n<p>Clears the contents of the byte array and resets the length and position properties to 0. Calling this method explicitly frees up the memory used by the ByteArray instance.</p>\n</blockquote>\n'}]
['https://Stackoverflow.com/questions/34', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']