0

I have a PHP script which reads data from a file and parse the strings into array like this:

    $handle = fopen('syndata.txt', 'r');
    while (!feof($handle)) {
     $data = fgets($handle);

     $convert = explode(",", $data);

     array_push($arrayStr, $convert);

} 
fclose($handle);

Where syndata.dat is a file of size 2.5 GB. When I run the script, it takes a few minutes to run and after that goves fatal error of "Out of Memory" on line $data = file_get_contents("syndata.dat"); .

I have tried everything increasing memory limit in php.ini file. Currently, memory_limiy parameter is set to the value -1 which means that unlimited memory. Still it gives fatal error. "Fatal error: Out of memory (allocated 520093696)(tried to allocate 519569408)"

Please help me resolving the issue.

sana
  • 410
  • 2
  • 6
  • 24
  • 1
    Pretty self explanatory.... you try to load a 2.5GB file into 520MB of memory, what do you expect to happen?.... Use a loop: read a line, process a line, read the next line, process that line, etc – Mark Baker Dec 24 '14 at 00:28
  • I am working on 64-bit system. – sana Dec 24 '14 at 00:41
  • But I have used memory_limit: -1 I expect the script to be working. OR please tell me where I should make changes to increase memory? @MarkBaker – sana Dec 24 '14 at 00:43
  • 1
    `memory_limit = -1` does not automagically make infinite memory space on your server. Your server still has hardware constraints. – zerkms Dec 24 '14 at 00:44
  • Okay, what should I do then ? Or how can I remove constraints ? :( @zerkms – sana Dec 24 '14 at 00:51
  • @sana: you have not explained the tasks, hence it's not possible to give any reasonable solution. – zerkms Dec 24 '14 at 00:52
  • 1
    You __DON'T__ remove that constraint.... as I've already said, you rewrite your code to loop through the file one line at a time – Mark Baker Dec 24 '14 at 00:59
  • Loop through code one line at a time... ??? How ? :( @MarkBaker – sana Dec 24 '14 at 01:00
  • Easiest way to loop through a file: `$handle = fopen('syndata.dat', 'r'); while (!feof($handle)) { $data = fgets($handle); //process data from line } fclose($handle);` There's plenty of examples in the [php docs](http://www.php.net/manual/en/function.fgets.php) – Mark Baker Dec 24 '14 at 01:03

0 Answers0