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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:18:17+00:00 2026-05-31T22:18:17+00:00

I am developing a command line script which runs in an infinite loop. After

  • 0

I am developing a command line script which runs in an infinite loop. After a while it causes a segmentation fault which I think is caused by memory leaks. I think I am correct, because after looking on the results produced by the ps command it looks like the memory used by the script constantly increases before the script crashes.

I have found this article, which states that one possible cause of memory leaks in command line php is the use of foreach loops, which create copies of arrays that are never unset. After some research it looks like that is the case. So I decided to replace all foreach loops with their for equivalents.

First question – is my reasoning correct?

Second one – what to do if I have an associative array to iterate and I would like to know the current key?

One way I can think of would be using array_walk(), other one – using combination of next() and key() functions in a for loop. Which approach wouldn’t leave me with memory leaks?

I will perform some tests and post the results after I’m done.

Secondary problem would be how to deal with iterable objects but that’s for later.

EDIT 1: There are some differing results from my tests so I’ll post something new after some more research.

  • 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-31T22:18:18+00:00Added an answer on May 31, 2026 at 10:18 pm

    Using next() and key() will not give you the memory leaks that foreach does. Foreach creates an “internal” copy of your array, and that copy is the memory leak problem. Using next() and key() does not create a copy – then you are working with the original data.

    Yet another suggestion on how to solve this is to use array_keys() like this:

    $keys = array_keys($assoc_array);
    for ($keyindex = 0; $keyindex < count($keys); $keyindex++) {
        $key = $keys[$keyindex];
        $val = $assoc_array[$key];
    
        /* Now you have $key and $val. */
    
    }
    

    However, I believe your suggestion with next() and key() will be the most effective one – and probably give you the prettiest code too! 🙂

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

Sidebar

Related Questions

I am developing a command line app which uses obj-c and c files together.
I have a little experience developing small command-line applications with Python. I want to
I'm developing a C++ command-line application in Visual Studio and need to debug it
What are your opinions on developing for the command line first, then adding a
I have a command line Ruby app I'm developing and I want to allow
Now, I'm developing a command-line application somewhat like bash. For implementing the autocomplete functionality,
I'm developing a tool that accesses SVN by running the command line SVN to
I'm developing a simple, stand-alone, command line Java application. The project is managed by
I'm developing a small utility script for trac which needs to know which is
I am developing small command line utilities using Vala on win32. Programs compiled using

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.