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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:45:15+00:00 2026-05-15T16:45:15+00:00

I have a do/while loop that goes over database rows. Because it runs many

  • 0

I have a do/while loop that goes over database rows. Because it runs many days at the time processing 100000s of rows, memory consumption is important to keep in check or it will crash. Right now every iteration adds about 4kb to script’s memory usage. I’m using memory_get_usage() to monitor the usage.

I unset every variable used in the loop first thing in each iteration so I really don’t know what else I could do. My guess is that do/while gathers some data with each iteration and this is what consumes the 4kb of memory. I know 4kb doesn’t sound like much but it soon starts to add up when you have 100000s of iterations.

Can somebody suggest another way of going through large amount of database rows or how to somehow eliminate this “memory leak”?

edit
Here’s the UPDATED loop code. Above it is just few require_once()s.

$URLs = new URLs_url(db());
$c = new Curl;
$c->headers = 1;
$c->timeout = 60;
$c->getinfo = true;
$c->follow = 0;
$c->save_cookies = false;

do {
    // Get url that hasn't been checked for a week
    $urls = null;

    // Check week old
    $urls = $URLs->all($where)->limit(10);

    foreach($urls as $url) {
        #echo date("d/m/Y h:i").' | Checking '.$url->url.' | db http_code: '.$url->http_code;

        // Get http code    
        $c->url = $url->url;
        $data = $c->get();

        #echo ' - new http_code: '.$data['http_code'];

        // Save info
        $url->http_code = $data['http_code'];
        $url->lastchecked = time();
        $URLs->save($url);
        $url = null;
        #unset($c);
        $data = null;
        #echo "\n".memory_get_usage().' | ';
        echo "\nInner loop memory usage: ".memory_get_usage();
    }
    echo "\nOuter loop memory usage: ".memory_get_usage();

} while($urls);

Some logs how memory consumption behaves in both loops:

Inner loop memory usage: 611080
Inner loop memory usage: 612452
Inner loop memory usage: 613788
Inner loop memory usage: 615124
Inner loop memory usage: 616460
Inner loop memory usage: 617796
Inner loop memory usage: 619132
Inner loop memory usage: 620500
Inner loop memory usage: 621836
Inner loop memory usage: 623172
Outer loop memory usage: 545240
Inner loop memory usage: 630680
Inner loop memory usage: 632016
Inner loop memory usage: 633352
Inner loop memory usage: 634688
Inner loop memory usage: 636088
Inner loop memory usage: 637424
Inner loop memory usage: 638760
Inner loop memory usage: 640096
Inner loop memory usage: 641432
Inner loop memory usage: 642768
Outer loop memory usage: 556392
Inner loop memory usage: 640416
Inner loop memory usage: 641752
Inner loop memory usage: 643088
Inner loop memory usage: 644424
Inner loop memory usage: 645760
Inner loop memory usage: 647096
Inner loop memory usage: 648432
Inner loop memory usage: 649768
Inner loop memory usage: 651104
Inner loop memory usage: 652568
Outer loop memory usage: 567608
Inner loop memory usage: 645924
Inner loop memory usage: 647260
Inner loop memory usage: 648596
Inner loop memory usage: 649932
Inner loop memory usage: 651268
Inner loop memory usage: 652604
Inner loop memory usage: 653940
Inner loop memory usage: 655276
Inner loop memory usage: 656624
Inner loop memory usage: 657960
Outer loop memory usage: 578732
  • 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-15T16:45:16+00:00Added an answer on May 15, 2026 at 4:45 pm

    This bit should probably happen only once, before the loop:

    $c = new Curl;
    $c->headers = 1;
    $c->timeout = 60;
    ...
    $c->getinfo = true;
    $c->follow = 0;
    $c->save_cookies = false;
    

    Edit: Oh, the entire thing is wrapped in a do/while loop. /facepalm

    Edit 2: There’s also this important bit:

    unset($class_object) does not release
    resources allocated by the object. If
    used in loops, which create and
    destroy objects, that might easily
    lead to a resource problem. Explicitly
    call the destructor to circumvent the
    problem.

    http://www.php.net/manual/en/function.unset.php#98692

    Edit 3:

    What is this? Can’t this be moved outside of the loop somehow?

    $URLs = new URLs_url(db());
    

    Edit 4:

    Try removing these lines, for now.

        $url->http_code = $data['http_code'];
        $url->lastchecked = time();
        $URLs->save($url);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a while loop that goes while a BuffedReader still has data, what
So I have a Linux program that runs in a while(true) loop, which waits
I have a while loop that goes through a table and I echo the
I have a while loop that loops through 3 results and echo's these out
So with pygame you have a while loop that loops continuously, then your event
I have a script that uses a simple while loop to display a progress
I have in my Java application a thread that run a while(True) loop with
I have a do-while loop that needs to log a message once (so it
The while loop I have while reading in from a file doesn't break. I'm
hi i have a while loop: var i = 0; while(i < 20) {

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.