1

So, I'm trying to parse a text file that has 24MB and 314134 lines. The problem is, I feel like my script is using way too much memory.

This is the code:

if(file_exists($filePath)) {
            $data = file_get_contents($filePath);

            $lines = explode("\n", $data);
            foreach ($lines as $line) {
                //Split the line.
                $spllitedLine = explode(';', utf8_encode($line));

                //Get the fields by their index.
                $localidade = !empty($spllitedLine[3]) ? $spllitedLine[3] : '';
                $codigo_postal = $spllitedLine[14] . '-' . $spllitedLine[15];

                $morada = (!empty($spllitedLine[5]) ? $spllitedLine[5] : ' ') . ' ' .
                    (!empty($spllitedLine[6]) ? $spllitedLine[6] : ' ') . ' ' .
                    (!empty($spllitedLine[7]) ? $spllitedLine[7] : ' ') . ' ' .
                    (!empty($spllitedLine[8]) ? $spllitedLine[8] : ' ') . ' ' .
                    (!empty($spllitedLine[9]) ? $spllitedLine[9] : '');

                //Create a new CTT location and save it to the Database.
                $location = new CttLocations();
                $location->address = preg_replace('/\s\s+/', ' ', $morada);
                $location->location = $localidade;
                $location->zipcode = $codigo_postal;
                $location->save(false);

                //Unset the variables to free space.
                unset($location);
                unset($line);
                unset($morada);
            }
        }

This is currently using 153MB of memory and it's not even in the half of the file. I've read that using fopen() fgets() and fclose() it's a better solution but I was using roughly the same amount of memory with those methods. What am I doing wrong? I thought by unsetting the variables I would free some much needed space. I think 150MB is WAY too much for an operation like this. Any thouhgts?

2
  • It is file I/O which can be and is really expensive. Commented May 12, 2015 at 11:46
  • My guess would be additional overhead by PHP and the explode statement. You store every line of the contents in an array. So thats 2x 24MB + overhead. Commented May 12, 2015 at 11:51

1 Answer 1

1

This :

 $data = file_get_contents($filePath);

Is way to heavy for big files.

This is how you read a file line by line :

$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
    }

    fclose($handle);
} else {
    // error opening the file.
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.