View Single Post

  #8 (permalink)  
Old 09-05-2004
Chung Leong
 
Posts: n/a
Default Re: PHP memory management

"MRe" <m.r.e.@.d.u.b.l.i.n...i.e> wrote in message
news:BbPZc.26436$Z14.8246@news.indigo.ie...
> Are there any tools or memory management functions that will allow me to

see
> exactly how many copies of various objects/classes/strings I have. Or a

way
> to dump a memory allocation when it happens or anything. I'm fearful that
> there may be millions of copies of my classes and such that I won't know
> about until it gets released - and two users go to the web site at the

same
> time and cause the server to run out of memory.


PHP uses techniques like reference counting and copy-on-write to manage
memory. When copies of an object are made, PHP doesn't physically copy the
contents. It just increments the reference counter of that object. So
pass-by-value and return-by-value is not as expensive as you think. When a
variable goes out of scope, the reference counter of the object it points to
is decremented. When the counter reaches zero, the memory is freed (in
theory). Physical copies of objects are made only when you make changes to
one of the variables.

In general, use references only when you need to. Creating references has
more overhead and sometimes confuses PHP's garbage collection
mechanism--e.g. when you have two objects referring to each other.

> C++ is so much easier, I know exactly what memory is allocated, when and
> where, and when it will be destroyed. Whoever invented garbage collection
> and all that; a can of comeuppance upon them (not the good kind).


PHP is a high level language. You really can't code in the same way as you
do in C++. It would be like trying to make a pizza from scratch from a
microwave pizza:
"I can't toss this thing! And the salami is sticking to my rolling pin!"



--
Obey the Clown - http://www.conradish.net/bobo/


Reply With Quote