Reference counting works in the following way.
Normally, if you assign an identifier to the value of an instantiated object
then the parser will create a copy of that object. That is, the parser will
allocate a physical chunk of memory of the relevant size and copy the memory
contents of the original object into this location, your new variable will
refer to this new chunk of memory.

Say you had something like:

1       $objA = new classA;
2       $objA->temp = 10;
3
4       $objB = $objA

Line 4 will cause the allocation of memory and copying of the contents of
$objA into this new memory area. $objB will refer to this new copy.


Allocating memory and copying values into that memory space consumes
resources (time, processor, memory space etc..). Reference counting works by
putting of the allocation of memory until such a time as the new copy of the
object is actually altered. So, say you have something like:

1       $objA = new classA;
2       $objA->temp = 10;
3
4       $objB = $objA
5       $objB->temp2 =20;

At line 4 no new memory is allocated. Instead, both $objA and $objB refer to
exactly the same instance of the object. The parser keeps a reference count
to this object, in this case at line 4 it will contain a value of 2 (2
variables refering the same object). When an attempt is made to alter the
contents of an object the parser checks the reference counter, if it is
greater than one then it knows that it needs to create a copy. So, on line 5
the parser allocates some new memory and copies the object, $objB will then
refer to this new object. Now, when a property of $objB is altered it does
not affect $objA. Once the parser has created a new object for $objB it
drops the reference counter for $objA to 1.

This is particularly useful for when objects are passed by value to
functions. Many functions do not actually change the contents of the object
and just use the values inside it. Reference counting improves performance
by saving resources involved in the creation of a new copy of an object.

Hope this helps.

Neil


-----Original Message-----
From: Luiz Fernando (Tuca) [mailto:[EMAIL PROTECTED]]
Sent: 18 July 2001 08:16
To: [EMAIL PROTECTED]
Subject: Re: [PHP] How is the management of memory by PHP?


I would like to know like is the management, that recourses the php utlize.
I am making a work to university and I need of  more information.
I find in www.zend.com a article about Reference Counting, but i stayed
confuse, if o recourse is of PHP 4 or of
ZEND Engine..

Thanks

Luiz Fernando

<[EMAIL PROTECTED]> escreveu nas notícias de
mensagem:[EMAIL PROTECTED]
> Hi Luiz!
> On Tue, 17 Jul 2001, Luiz Fernando (Tuca) wrote:
>
> > Somebody know how it is the management of memory by PHP?
> >
> yes, it's quite well done :)
>
> what exactly are you interested in?
>
> -- teodor



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to