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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:53:23+00:00 2026-06-12T00:53:23+00:00

I have a script, written in PHP that uses the AWS Dynamo PHP API.

  • 0

I have a script, written in PHP that uses the AWS Dynamo PHP API. It runs a long loop where it pulls lots of data from dynamo and then it processes it.

When I watch the process using ‘top’ I can see the memory usage used by the ‘php’ process

Inside my script’s loop I print the result of memory_get_usage(true)

When I run my test these two value are not even remotely similar…

Should they be? If not why not?

In my test I have a server with 1.7gb of ram, and I have set my php.ini’s memory_limit to 64M. I also call gc_enable() at the start of my script, and between each loop call gc_collect_cycles() in the hope of forcing a garbage collection.

When I watch my php script using ‘top’ I can see the %MEM going up and up, until it eventually gets over 95% and linux kills the php process, which I know from looking at ‘dmesg’. When I look at the print outs from each iteration of the loop the memory usage reported by memory_get_usage(true) never gets above 50mb.

Linux thinks the script is using almost 1.7gb, php thinks it’s only using 50mb!

What goings on?

Even if the script has memory leaks, I don’t understand why memory_get_usage(true) does not account for the memory…

UPDATE

After spending some time commenting out various parts of the processing I am running inside my loop I found that if I remove the following code:

class cMyClass {
    public static function static_cmp_fn(&$a, &$b) {
        if ($a['att'] == $b['att']) { return 0; }
        $ret = ($a['att'] < $b['att']) ? -1 : +1;
        return $ret;
    }
    function DoProcessing(){
        $sort_fn = array("cMyClass", "static_cmp_fn");
        usort($this->m_dictToSort, $sort_fn); 
        unset($sort_fn);
    }

}

php never eats all of the system memory. It seems to me that the usort is leaking memory, I don’t know why. What I don’t understand is why PHP reports the wrong information about how much memory it is using…

Any ideas?

  • 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-06-12T00:53:24+00:00Added an answer on June 12, 2026 at 12:53 am

    After spending some time commenting out various parts of the processing I am running inside my loop I found that if I remove the following code:

    $sort_fn = array("cMyClass", "static_cmp_fn");
    usort($this->m_dictToSort, $sort_fn); 
    

    php never eats all of the system memory. It seems to me that the usort is leaking memory, I don’t know why.

    Apparently it is. See the manual:

    http://php.net/manual/en/function.usort.php

    “A couple examples here advocate the use of ‘create_function’ for sorting, which is tempting to use because of the limitations of usort. But beware this method — the function created will NOT be freed at the end of the sorting routine, which creates a memory leak. For this reason, this method should probably never be used.”

    The array() method seems to do something similar. You could declare a wrapper function which calls your method externally, perhaps?

    UPDATE

    Tried to build a small test case to see what happens. As yet I cannot reproduce the leak; maybe with more data about what static_cmp_fn() does and how m_dictToSort is structured. A simple compare doesn’t trigger anything strange. Nor does allocating strings, arrays, or objects inside the loop. The garbage collector kills them off and memory stays low.

    I’d further restrict the problem by calling another function which doesn’t sort at all, or does a very basic sort, to see whether the problem is in usort doing something funny with its callable, as I thought (it appears it doesn’t, and I was wrong) or if something funny is happening inside the compare function.

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

Sidebar

Related Questions

I have a written a script in PHP that reads from data from a
So I have a comments script I've written in Codeigniter that uses PHP and
I have written a script image.php that is quite long, but I've just copied
I have written a script that fires a jQuery POST to retrieve data from
I have written a php script that generates a list of links that match
I've written a PHP script that runs via SSH and nohup, meant to process
I have written a PHP script that checks whether a domain is available for
Possible Duplicate: PHP - send file to user I have written a script that
I have a script written long ago by a freelancer that's worked fine until
I have written a php script that returns an arbitrary number of specific ids

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.