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 7458325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:25:59+00:00 2026-05-29T13:25:59+00:00

I have an array that I’m appending to in a loop: array_push($obj->{$type.’listings’}, $listing); The

  • 0

I have an array that I’m appending to in a loop:

array_push($obj->{$type.'listings'}, $listing);

The loop makes a bunch of remote calls and pulls in data that I want to store in a file and use as a sort of cache.

While running this “cache routine”, if I serialize this array it reports to be just over 5MB. To create the 5MB array, the script exhausts 100MB of memory. I’m being very careful to unset variables to free memory as I can. I’ve went as far as commenting everything out and function by function uncommented to see where the memory build up is. It certainly happens as I push to the array. If I don’t use the array_push, then using memory_get_usage() and gc_collect_cycles() I can see a rather steady amount of memory used with some spikes, but it frees itself as it moves along.

As soon as I push to the array, it gets all crazy and the memory usage just piles up.

Are there any ideas on how to handle a situation like this?

Can I not build large arrays like this with PHP?

Is there a way to flush the array to a file while I’m building it? And lets assume that I could get it built and stored in a file. When I want to use it, will i use the same amount of resources once pulled from the file….or is the memory exhaustion just happening because it’s dynamically being added to as it’s being built?

Is this something that I should just consider using SQL for? and storing each run of the loop in a separate row?

Just for kicks, I added: ini_set('memory_limit', '-1'); just to see if it would run.

It did, and memory usage peaked at just over 100 MB, with a serialized object size of just over 8MB. Now, this isn’t really helpful as I am just processing a sample of data and this could grow to be much bigger.

Anyway, any thoughts you might have on optimizing a situation like this would be great.

  • 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-29T13:25:59+00:00Added an answer on May 29, 2026 at 1:25 pm

    For 5MB of array PHP doens’t use 100MB for sure.

    Use http://it.php.net/memory_get_usage in your script to see where it grows

    Consider I build in PHP arrays of millions (2 millions) of entry without problems. (memory usage less than 200MB)

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

Sidebar

Related Questions

I have an array that gets processed through a foreach loop. foreach($images as $imageChoices)
I have the following array that I need to recursively loop through and remove
i have an array that looks like this: foreach($obj as $key => $value) {
I have an array that contains a list of vertices which I copy to
I have an array that looks like: $fields = array( f1 => array(test), f2
I have an array that has keys and values. For eg: Array ( [name]
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have an array that is a member of a structure: $self->{myArray} = [value1,
I have a array that looks like this Array ( [provider] => Array (
I have an array that contains the RGB colour values for each pixel in

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.