# Memory management ...

- Thread: https://externals.io/message/54345
- Original archive: https://news-web.php.net/php.internals/54345
- Started by Lester Caine on 2011-08-04T10:17:22+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54345 — Lester Caine — 2011-08-04T10:17:22+00:00

URL: https://externals.io/message/54345#54345

I'm still trying to get my head around this 'Weak Reference' thing, but since 
I'm still working to be compatible with PHP5.2 then I've not taken much notice 
of this 'garbage collection' stuff. So this may be where I am missing something? 
I manage what material I need using a list of numbers, and create objects as and 
when they are required, pulling the detail from the database. There is a limit 
on the amount of memory available, so in the past I got 'out of memory', now I 
just say 'some detail not available'. If I'm out of space, then I reuse an 
existing object that is 'invalid', but to simplify things then much of the time 
I don't ACTUALLY need the full record, and simply build the page via the one 
object which is sequentially loaded ... slower, but memory efficient. Never do 
'SELECT * FROM', always specify just the fields you need at that time.

So it looks like the question I should be asking is "How do you 'drop' an 
object?" I think my take on this 'Weak Reference' stuff is that it does not 
actually 'create' the object? But I am creating the object because I need to use 
it, not because I may need it later? I'll get the database server to do any 
heavy processing ... and that may well be a different machine ... I just want 
the results. So I try to avoid pulling unnecessary data into the PHP side - I 
can only put so much on a client page.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

---

## #54346 — Ferenc Kovacs — 2011-08-04T10:43:58+00:00

URL: https://externals.io/message/54345#54346 · In reply to: #54345

> I'm still trying to get my head around this 'Weak Reference' thing, but
> since I'm still working to be compatible with PHP5.2 then I've not taken
> much notice of this 'garbage collection' stuff. So this may be where I am
> missing something? I manage what material I need using a list of numbers,
> and create objects as and when they are required, pulling the detail from
> the database. There is a limit on the amount of memory available, so in the
> past I got 'out of memory', now I just say 'some detail not available'. If
> I'm out of space, then I reuse an existing object that is 'invalid', but to
> simplify things then much of the time I don't ACTUALLY need the full record,
> and simply build the page via the one object which is sequentially loaded
> ... slower, but memory efficient. Never do 'SELECT * FROM', always specify
> just the fields you need at that time.
>
> So it looks like the question I should be asking is "How do you 'drop' an
> object?" I think my take on this 'Weak Reference' stuff is that it does not
> actually 'create' the object? But I am creating the object because I need to
> use it, not because I may need it later? I'll get the database server to do
> any heavy processing ... and that may well be a different machine ... I just
> want the results. So I try to avoid pulling unnecessary data into the PHP
> side - I can only put so much on a client page.
>

if you unset a variable, the zval's refcount for that variable will be
decreased by one.
if the refcount reach zero, the zval will be freed.
http://www.php.net/manual/en/features.gc.refcounting-basics.php

this was also before 5.3
the new feature in 5.3 however you can fix the memory leaks that
happened because of the circular references:
http://www.php.net/manual/en/features.gc.collecting-cycles.php

a simple example for circular reference is basically when you have two
zval, they both reference each other, so their refcount is one, so
they cannot be freed, but they should been.
this was addressed in 5.3, now the gc collector will traverse the
zvals and find such "islands" and free them.

so to address your question: if you unset your variables you can
reclaim memory, but there could be edge cases, when you will memory
leak before version 5.3.

-- 
Ferenc Kovács
@Tyr43l - http://tyrael.hu

---

## #54349 — Lester Caine — 2011-08-04T11:36:03+00:00

URL: https://externals.io/message/54345#54349 · In reply to: #54346

Ferenc Kovacs wrote:
> so to address your question: if you unset your variables you can
> reclaim memory, but there could be edge cases, when you will memory
> leak before version 5.3.

So it looks like I'm not hitting any edge cases in the earlier code as some 
sites have been running for years without a reboot ... Must think about doing 
that sometime, running a windows server for 21 months without a reboot might be 
pushing luck :)

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk//
Firebird - http://www.firebirdsql.org/index.php

---

## #54359 — johannes@schlueters.de — 2011-08-04T13:21:30+00:00

URL: https://externals.io/message/54345#54359 · In reply to: #54349

> Ferenc Kovacs wrote:
> > […]
> 
> So it looks like I'm not hitting any edge cases in the earlier code as some 
> sites have been running for years without a reboot ... Must think about doing 
> that sometime, running a windows server for 21 months without a reboot might be 
> pushing luck :)

PHP frees memory forcefully on request end as good as it can (which is
quite good) but sometimes a request takes a lot of memory and some
people try to reduce that.

johannes
> -- 
> Lester Caine - G8HFL
> -----------------------------
> Contact - http://lsces.co.uk/wiki/?page=contact
> L.S.Caine Electronic Services - http://lsces.co.uk
> EnquirySolve - http://enquirysolve.com/
> Model Engineers Digital Workshop - http://medw.co.uk//
> Firebird - http://www.firebirdsql.org/index.php
