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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:27:12+00:00 2026-05-15T09:27:12+00:00

I have a PHP script that runs on cron that can take up to

  • 0

I have a PHP script that runs on cron that can take up to an 15 minutes to execute. At regular intervals I have it spitting out memory_get_usage() so I can see what is happening. The first time it tells me my usage I am at 10 megs. When the script finishes I am at 114 megs!

Does PHP do it’s garbage collection while the script is running? Or what is happening to all that memory? Is there something I can do to force garbage collection. The task that my script is doing is a nightly import of a couple thousand nodes into Drupal. So it is doing the same thing a lot of times.

Any suggestions?

  • 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-15T09:27:13+00:00Added an answer on May 15, 2026 at 9:27 am

    The key is that you unset your global variables as soon as you don’t need them.

    You needn’t call unset explicitly for local variables and object properties because these are destroyed when the function goes out of scope or the object is destroyed.

    PHP keeps a reference count for all variables and destroys them (in most conditions) as soon as this reference count goes to zero. Objects have one internal reference count and the variables themselves (the object references) each have one reference count. When all the object references have been destroyed because their reference coutns have hit 0, the object itself will be destroyed. Example:

    $a = new stdclass; //$a zval refcount 1, object refcount 1
    $b = $a;           //$a/$b zval refcount 2, object refcount 1
    //this forces the zval separation because $b isn't part of the reference set:
    $c = &$a;          //$a/$c zval refcount 2 (isref), $b 1, object refcount 2
    unset($c);         //$a zval refcount 1, $b 1, object refcount 2
    unset($a);         //$b refcount 1, object refcount 1
    unset($b);         //everything is destroyed
    

    But consider the following scenario:

    class A {
        public $b;
    }
    class B {
        public $a;
    }
    
    $a = new A;
    $b = new B;
    $a->b = $b;
    $b->a = $a;
    unset($a); //cannot destroy object $a because $b still references it
    unset($b); //cannot destroy object $b because $a still references it
    

    These cyclic references are where PHP 5.3’s garbage collector kicks in. You can explicitly invoke the garbage collector with gc_collect_cycles.

    See also Reference Counting Basics and Collecting Cycles in the manual.

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

Sidebar

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.