Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3610196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:43:19+00:00 2026-05-18T21:43:19+00:00

I have a PHP script that imports CSV files and goes through tens of

  • 0

I have a PHP script that imports CSV files and goes through tens of thousands of iterations. As the script runs over a course of hours, the memory use goes up and up, and if the file is big enough, the script uses up so much memory that the whole machine grinds to a halt.

Right now the only technique I’m using is to unset() everything I can when I’m done with it. I’ve tried to isolate the part that’s using the most memory, but it seems like every function in my script is just one more straw on the camel’s back, and using “as little memory as possible.”

So what can I do?

I’ve tried looking into benchmarking/profiling tools but I haven’t found anything good. I’m on a Windows machine, SSHing into a Linux box.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T21:43:20+00:00Added an answer on May 18, 2026 at 9:43 pm

    Ok, since you’re looking for techniques, let me list some…

    1. Don’t read files, stream them

    Rather than calling $data = file_get_contents($file), open it with fopen and only read the data you need at that point in time (fgets or fgetcsv, etc). It’ll be a touch slower, but it’ll use FAR less memory.

    2. Upgrade to 5.3.4

    If you’re still on PHP 5.2.x, memory will be greatly conserved by upgrading to 5.3.x (latest 5.3.4). It includes a garbage collector that will clean up freed memory after a while.

    3. Don’t use anything in the global scope

    Don’t store any information in the global scope. it’s never cleaned until the end of execution, so it could be a memory leak in and of itself.

    4. Don’t pass around references

    PHP uses copy-on-right. Passing around references only increases the chances that unset won’t get all of them (because you forgot to unset one of the references). Instead, just pass around the actual variables.

    5. Profile the code

    Profile your code. Add debug hooks to the start and end of each function call, and then log them watching the memory usage at the entrance and exit of every function. Take the diffs of these and you’ll know how much memory is being used by each function. Take the biggest offenders (those that are called a lot, or use a lot of memory) and clean them up… (lowest hanging fruit).

    6. Use a different language

    While you can do this with PHP (I have and do quite often), realize it may not be the best tool for the job. Other languages were designed for this exact problem, so why not use one of them (Python or Perl for example)…

    7. Use Scratch Files

    If you need to keep track of a lot of data, don’t store it all in memory the entire time. Create scratch files (temporary files) to store the data when you’re not explicitly using it. Load the file only when you’re going to use that specific data, and then re-save it and get rid of the variables.

    8. Extreme cases only: don’t use large arrays!

    If you need to keep track of a large number of integers (or other simple data types), don’t store them in an array! The zval (internal data structure) has a fair bit of overhead. Instead, if you REALLY need to store a LARGE number of integers (hundreds of thousands or millions), use a string. For a 1 byte int, ord($numbers[$n]) will get the value of the $n index, and $numbers[$n] = chr($value); will set it. For multy-byte ints, you’d need to do $n * $b to get the start of the sequence where $b is the number of bytes. I stress that this should only be used in the extreme case where you need to store a TON of data. In reality, this would be better served by a scratch file or an actual database (Temporary Table likely), so it may not be a great idea…

    Good Luck…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following situation. I have a PHP script that imports a CSV
I have a PHP script that runs as a CGI program and the HTTP
I have a PHP script that initialises an image gallery. It loops through all
I am trying to download some files. I have written a php script that
I have a PHP script that needs to determine if it's been executed via
I have a PHP script that can encode a PNG image to a Base64
I have a PHP script that sends critical e-mails. I know how to check
I have a PHP script that processes file uploads. The script tries to organise
I have a PHP script that pushes the headers to allow a file to
Specifically I have a PHP command-line script that at a certain point requires input

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.