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

  • Home
  • SEARCH
  • 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 6594557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:49:37+00:00 2026-05-25T17:49:37+00:00

Memory management is not something that most PHP developers ever need to think about.

  • 0

Memory management is not something that most PHP developers ever need to think about. I’m running into an issue where my command line script is running out of memory. It performs multiple iterations over a large array of objects, making multiple database requests per iteration. I’m sure that increasing the memory ceiling may be a short term fix, but I don’t think it’s an appropriate long-term solution. What should I be doing to make sure that my script is not using too much memory, and using memory efficiently?

  • 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-25T17:49:37+00:00Added an answer on May 25, 2026 at 5:49 pm

    The golden rule

    The number one thing to do when you encounter (or expect to encounter) memory pressure is: do not read massive amounts of data in memory at once if you intend to process them sequentially.

    Examples:

    • Do not fetch a large result set in memory as an array; instead, fetch each row in turn and process it before fetching the next
    • Do not read large text files in memory (e.g. with file); instead, read one line at a time

    This is not always the most convenient thing in PHP (arrays don’t cut it, and there is a lot of code that only works on arrays), but in recent versions and especially after the introduction of generators it’s easier than ever to stream your data instead of chunking it.

    Following this practice religiously will “automatically” take care of other things for you as well:

    • There is no longer any need to clean up resources with a big memory footprint by closing them and losing all references to them on purpose, because there will be no such resources to begin with
    • There is no longer a need to unset large variables after you are done with them, because there will be no such variables as well

    Other things to do

    • Be careful of creating closures inside loops; this should be easy to do, as creating such inside loops is a bad code smell. You can always lift the closure upwards and give it more parameters.
    • When expecting massive input, design your program and pick algorithms accordingly. For example, you can mergesort any amount of text files of any size using a constant amount of memory.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C++ memory management doubt, that's (obviously) related to references and pointers.
I know the basic principles about memory management (retain count, autorelease pools etc) in
I was reading the apple documentation for memory management , and came across something
Coming from Java and Python, I am not so well versed in memory management,
I have managed to get myself confused about some elements of memory management. I
i have some questions about objective-c's memory management, let's say: NSString * test= [[NSString
I'm doing my last bit of memory management tidying and there's something I don't
As a relative Objective-C beginner, I'm obviously still not grasping certain memory management rules.
I've read many different things about correct memory management for cocoa/objective-c For instance ive
I've read that this error comes from memory-management issues such as sending a message

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.