Announcement

Collapse
No announcement yet.

Does array.length = 0 free storage?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does array.length = 0 free storage?

    If I zero the number of elements in an array by setting its length to 0, does this also release the memory used by the pre-existing array elements?

  • #2
    If you are trying to release the memory used by the existing array, set the array equal to null. For example if your array had been almerger.length =100, I believe that by setting almerger = null; would effectively release that memory.

    Comment


    • #3
      Thanks for the suggestion. I tried this:
      var Araay = new Array();
      Array[100] = "Y";
      Araay = null;
      Array[100] = "Y";

      On the last statement, I get "Araay has no properties". It seems that setting the entire array to null loses its Array quality.

      But it's OK, I've found that by setting each of the individual existing elements to null avoids the "out of memory" problem that happens when I try using:
      Araay.length = 0

      Thanks!

      Comment


      • #4
        almerger
        Another thing you could try is to create a new Array with the same name and no parameters eg
        PHP Code:
        var myArray = new Array(1,2,3,4,5);
            
        debugPrint(myArray.length)
        myArray = new Array();
            
        debugPrint(myArray.length
        With regards to changing the length property that does not change the number of actual elements according to this document
        Alex

        Comment


        • #5
          Your idea seems to work fine, no memory problem, thanks! (BTW, I couldn't get the article link to work.)

          Comment


          • #6
            almerger
            The link should be working now.
            Alex

            Comment

            Working...
            X