Jump to content

Need help with cutting down an array


Zie

Recommended Posts

I am currently downloading a file every few hours with updated game information for an online, sort of text based game. I am trying to get the information out of the gz file and keep getting "Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 71 bytes)" every way I try. The output of this file is very large. What is the best way of splitting this data into sections (if there is a way), without actually messing up the data? The information I want from the file is on a line-by-line basis. There's about 60,000 lines in the file.

Link to comment
Share on other sites

Use fgets to read the file line-by-line. If you save the lines or read in the entire file at once you'll run into problems allocating that much space. I ran this program on a 1.3MB file with 10,000 lines and then a 2.7MB file with 20,000 lines and it didn't use any more memory for the larger file:

<?php$mem1 = memory_get_usage();$peak1 = memory_get_peak_usage();$fh = fopen('test.txt', 'rb');$n = 0;while (!feof($fh)){  $line = fgets($fh);  $n++;}$mem2 = memory_get_usage();$peak2 = memory_get_peak_usage();fclose($fh);printf("Lines read: %d<br><br>Start mem: %d<br>Start peak: %d<br><br>End mem: %d<br>End Peak: %d<br><br>Change mem: %d<br>Change peak: %d", $n, $mem1, $peak1, $mem2, $peak2, $mem2 - $mem1, $peak2 - $peak1);?>

Lines read: 10000Start mem: 53504Start peak: 55408End mem: 64056End Peak: 64056Change mem: 10552Change peak: 8648
Lines read: 20000Start mem: 53504Start peak: 55408End mem: 64056End Peak: 64056Change mem: 10552Change peak: 8648
Link to comment
Share on other sites

-> Zie I am trying to get the information out of the gz Try first to 'tar' the files and then to 'gzip' , and then you should have no problems with downloading .I hope this helps . Please let me know if you do not understand .

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...