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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:20:19+00:00 2026-05-24T16:20:19+00:00

While debugging a piece of code that was exhausting the memory, I found a

  • 0

While debugging a piece of code that was exhausting the memory, I found a very interesting problem, and most importantly I don’t know how to fix it.

The application consists roughly of a single Survey object, which contains a number of Question objects. The Question objects contain a reference to the Survey they’re in, this is needed to be able to fetch answers from other Questions for instance.


The following loop was causing the memory overflow:

foreach ( $survey_ids_arr as $survey_id ) {
    $Survey = new Survey( $survey_id );
}

Nothing really exotic is happening in the Survey constructor;

  • fetching its properties from the database
  • fetching all questions’ properties from the database
  • creating a Question object for each question (passing a reference to $this)
  • adding all Question objects to an internal array

and from looking at the code, you would say that in each iteration the object is cleared from memory because the $Survey variable is overwritten. Right?? Wrong 🙂

The memory is piling up as the script goes through the loop – adding memory_get_usage() calls shows that the memory used by the Survey object isn’t freed as expected, at the moment another object is assigned to the $Survey variable. Even calling unset( $Survey ) at the end of the loop does not free the memory.


The culprit are the references to $this that are passed to the Question objects upon creation. These references prevent the object to be cleared from memory – as the manual on php.net states:

The destructor method will be called as soon as all references to a particular object are removed

So what prevents the object from being cleaned up, is the references it has in it to itself. Nice, huh? 🙂

So, the problem is my object is a memory killer. Unfortunately, I can’t think of a solution (other than writing an ugly method which clears the questions and calling that from the loop). The destructor in Survey is not an option; as stated above this is not called because the Question objects still have references.

Any ideas? Someone must have run into this problem already – the parent-containing-child-objects is not an uncommon architecture, is it?

  • 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-24T16:20:21+00:00Added an answer on May 24, 2026 at 4:20 pm

    So, here is the answer: switch to PHP 5.3, since this issue have been resolved in it. If you have to work with PHP < 5.3.0, it is your responsibility to free objects captured in circle references. One possible aproach is to introduce special method that will strip off links to child objects to allow them be collected by GC

    P.S. also might be useful for somebody, Doctrine 1.2 have such a links in models, so it’s subject for memory leaks, especially if you fetch lots of entities from your database. If you are facing this issues, try (1) reduce amount of entities fetched (e.g. hydrate as arrays), (2) process entities with raw sql requests.

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

Sidebar

Related Questions

I need help with debugging this piece of code. I know the problem is
While debugging Java code that uses reflection in Eclipse, some times I need to
While debugging the code in Eclipse, I runned into the following problem. There is
While debugging production code - I observed that my workflow constructor was triggered multiple
While debugging crash in a multithreaded application I finally located the problem in this
While debugging an issue with our system, I have discovered a thread contention that
While debugging jQuery apps that use AJAX, I often have the need to see
i get this dialog while debugging code in visual studio 2008. (your step-into request
While debugging a crash, I came across this issue in some code: int func()
While debugging Java code, Strings in the views Variables and Expressions show up only

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.