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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:37:56+00:00 2026-05-28T02:37:56+00:00

I’m running the PHP code below from command line. The issue is, its memory

  • 0

I’m running the PHP code below from command line. The issue is, its memory consumption far more than what it should be. I can’t, for the life of me, figure out where the memory is getting consumed.

for ($i=0;$i<100;$i++)  
        {
            $classObject = $classObjects[$i];                       

            echo $i . "   :   " . memory_get_usage(true) . "\n";
            $classDOM = $scraper->scrapeClassInfo($classObject,$termMap,$subjectMap);           
            unset($classDOM);           
        }

According to me, the memory consumed by my script should remain more or less constant after every iteration of the loop. Any memory consumed by $scraper->scrapeClassInfo() should be freed when its members go out of scope.

This is the output file I get. For the sake of brevity, I’m showing every 10th line of the output:

0   :   5767168
10   :   12058624
20   :   18350080
30   :   24903680
40   :   30932992
50   :   37748736
60   :   43778048
70   :   49807360
80   :   55836672
90   :   62914560
97   :   66846720

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 44 bytes) in /home/content/60/8349160/html/drexel/simple_html_dom.php on line 1255

Finally, as far as I can see, what $scraper->scrapeClassInfo() is doing should not really be the culprit, but just in case, here is the code:

function scrapeClassInfo($class,$termMap,$subjectMap)
        {
            $ckfile = tempnam ("/tmp", "CURLCOOKIE");
            $ckfile2 = tempnam ("/tmp", "CURLCOOKIE2");
            $ckfile3 = tempnam ("/tmp", "CURLCOOKIE3");         

            $termpage = $termMap[$class['termcode']];
            $subjectpage = $subjectMap[$class['subjectcode']];
            $classpage = $class['classlink'];

            //hit the main page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $this->mainURL);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the term page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $termpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the subject page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile3);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $subjectpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the class page and scrape
            $ch = curl_init();              
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile3);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $classpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            curl_close($ch);

            return str_get_html($result);
        }

The method called in the last line, str_get_html() is a member of Simple HTML DOM Parser

Should it matter, this is how I am calling my script:

/usr/local/php5/bin/php index.php 2>&1 1>output

  • 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-28T02:37:57+00:00Added an answer on May 28, 2026 at 2:37 am

    Alright, figured it out. Apparently, its a bug that all version of PHP prior to 5.3 suffer from. Setting CURLOPT_RETURNTRANSFER to true causes massive memory leaks.

    I ran the script again, this time invoking the php 5.3 binary:

    /web/cgi-bin/php5_3 index.php 2>&1 1>output
    

    And the output file reads:

    0   :   6291456
    10   :   9437184
    20   :   10747904
    30   :   11534336
    40   :   11534336
    50   :   11534336
    60   :   11534336
    70   :   11534336
    80   :   11534336
    90   :   11534336
    99   :   11534336
    152.74998211861 sec
    

    Now that’s what I’m talking about! Perfectly constant memory footprint.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am currently running into a problem where an element is coming back from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.