zoukankan      html  css  js  c++  java
  • Truncating ByteArray Does Not Dispose Contents, Free Up Memory

    http://www.ghostwire.com/blog/archives/as3-truncating-bytearray-does-not-dispose-contents-free-up-memory/

    When targeting Flash Player 10 or AIR 1.5, you can use the clear() method of the ByteArray class to explicitly clear the contents of the byte array and free up the memory otherwise used by the bytes. The length andposition properties are reset to zero after calling the clear() method.

    Unfortunately, when targeting Flash Player 9, this clear() method is not available. If you are using a ByteArrayobject as a data store, keeping a reference to the object and therefore not allowing the object to be garbage collected, do take note that there is no way to clear the contents. This means that the size ofByteArray objects can only be enlarged and never shrunk.

    It is important to note that while you can truncate the ByteArray to zero byte by setting its length property to zero, this will not dispose the contents or free up the memory used.

    This is actually a rather odd implementation. After you truncate a ByteArray, the bytes stay in memory and is in fact restorable simply by setting the length property back to a non-zero value.

    var bytes:ByteArray = new ByteArray();
    bytes.writeUTFBytes("ABCDE");
    trace(bytes[0]); // 65
    trace(bytes[1]); // 66
    trace(bytes[2]); // 67
    trace(bytes[3]); // 68
    trace(bytes[4]); // 69
    bytes.length = 0;
    trace(bytes[0]); // undefined
    trace(bytes[1]); // undefined
    trace(bytes[2]); // undefined
    trace(bytes[3]); // undefined
    trace(bytes[4]); // undefined
    bytes.length = 5;
    trace(bytes[0]); // 65 ** what the ??? necromancy at work ??? **
    trace(bytes[1]); // 66
    trace(bytes[2]); // 67
    trace(bytes[3]); // 68
    trace(bytes[4]); // 69

    In my opinion, this implementation is flawed. The Flash Player really should clear the data once the array of bytes is truncated; this should not require a new API method clear() to accomplish. To introduce a new API method that does what setting the length property to zero should have done in the first place really baffles me.

    Anyway, rant aside, so that is that – when you truncate a ByteArray, remember that the data is not lost. When targeting Flash Player 10 or AIR 1.5, do remember to call the clear() method instead of attempting to clear the contents by truncating.


    Zerofying Bytes
    Since the data stays in memory, I reckon it could sometimes be useful to erase the data explicitly, especially if it contains sensitive data:

    function zerofy(bytes:ByteArray):void
    {
    	if (bytes)
    	{
    		var j:int = bytes.length;
    		while (j--)
    		{
    			bytes[j] = 0;
    		}
    	}
    }

    I am not sure if the clear() method in FP10 API does this (or something similar to erase the data in memory) – I sure hope it does.

    Be Sociable, Share!
  • 相关阅读:
    052-90
    052-89
    052-88
    052-87
    052-86
    html5的manifest
    js中数字转金钱格式
    CSS复合样式
    资料
    异步
  • 原文地址:https://www.cnblogs.com/chenhongyu/p/3316033.html
Copyright © 2011-2022 走看看