0

I try to post an object with one array inside and was wondering why i do get error at a certain dimension of the array. So i went on testing how big my array may be with different contents.

Here is the script i used:

$(document).ready(function(){
/* jQuery-Code from here*/


// creating an object with on single array (at the moment
var array = [];
var obj = {};
// i <= 459941 OK when "xxxxxxxxxxxxxxxx"
// i <= 518807 OK when "xxxxxxxx"
// i <= 536364 OK when "xxxx"
// i <= 545688 OK when "xx"
// i <= 555355 OK when ""
for (var i = 0; i < 555355; i++)
{
    array[i] = "";
}
obj["arr"] = array;

// Using the core $.ajax() method
$.ajax({

    // The URL for the request
    url: "test.php",

    // The data to send (will be converted to a query string)
    data: { new_name: obj},

    // Whether this is a POST or GET request
    type: "POST",

    // The type of data we expect back
    dataType : "json",

    // Code to run if the request succeeds;
    // the response is passed to the function
    success: function( json ) {
        //var result = JSON.parse(json);
        var size = sizeof(json);
        var i = json.arr;
        var size = sizeof(i);
        // do something with the array
        //alert (array[0] * array[1]);
    },
    // Code to run if the request fails; the raw request and
    // status codes are passed to the function
    error: function( xhr, status, errorThrown )
    {
        alert( "Sorry, there was a problem!" );
    },
    // Code to run regardless of success or failure
    complete: function( xhr, status )
    {
//      alert( "The request is complete!" );
    }
});

});

checking my PHP (XAMPP) i came to this result:

PHP upload_max_filesize

Also post_max_size and memory_limit is set to 128 MB

Here are my results:

No. Of Elements ¡ String             ¡ Bytes ¡ 128MB / Elements
----------------¡--------------------¡-------¡-----------------
459941          ¡ "xxxxxxxxxxxxxxxx" ¡ 32    ¡ 292
518807          ¡ "xxxxxxxx"         ¡ 16    ¡ 259
536364          ¡ "xxxx"             ¡ 8     ¡ 250
545688          ¡ "xx"               ¡ 4     ¡ 246
555355          ¡ ""                 ¡ 0     ¡ 242

So it seems, that for one empty array-index 242 Byte are needed. When i assume "xx" to each array-index 246 Bytes are needed, and so on....

So my question is: Why so much memory is needed only for the constructor?

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
fooSolver
  • 61
  • 1
  • 4
  • Here's one explanation: http://stackoverflow.com/questions/6336528/memory-optimization-in-php-array There are several posts here on SO about why PHP arrays are being a bit memory greedy. – M. Eriksson Jun 17 '16 at 13:25
  • @MagnusEriksson Thank you. I searched a lot through the web and on this site too, maybe not enough. Well Judy Array seems to be interesting, so i will check it. Otherwise i'd to reduce the array entries by creating longer strings... – fooSolver Jun 17 '16 at 14:15
  • Judy for JavaScrict can be find here: https://github.com/JerrySievert/node-judy – fooSolver Jun 17 '16 at 15:05

0 Answers0