# Memory usage of Scripts

- Thread: https://externals.io/message/27979
- Original archive: https://news-web.php.net/php.internals/27979
- Started by Mathias Bank on 2007-02-10T08:17:48+00:00
- Messages: 1
- 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

---

## #27979 — Mathias Bank — 2007-02-10T08:17:48+00:00

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

Good morning,

I have a little problem with a php script which uses really lots of 
memory. Until now, I have thought that PHP will free variable memory 
after the end of a function. But it seems, that this is not true.

simple variables:

function readData() {
$data = file_get_contents("C:/phpProjekte/map1000.db");
$data2 = $data;
}

function readData2() {
$data = file_get_contents("C:/phpProjekte/map1000.db");
$data2 = $data;

unset($data);
unset($data2);
}

These two functions need _no memory_, it works fine.

recordsets (sqlite):

function readData() {
$db = sqlite_open('C:/phpProjekte/map1000.db', 0666);
$rs = sqlite_query($db, 'Select * from Node');
sqlite_close($db);
}

function readData2() {
$db = sqlite_open('C:/phpProjekte/map1000.db', 0666);
$rs = sqlite_query($db, 'Select * from Node');
sqlite_close($db);
unset($rs);
}

With these functions, you will see a _difference in memory usage_. But 
there is no sqlite_free_result, which would imply, that you have free 
your memory. I think, this is a bug. The memory should be freed after 
the end of the script automatically.

arrays:

function readData() {
$arr = array();
for ($i=1; $i<500; $i++) {
$arr[$i] = 5;
}
}

function readData2() {
$arr = array();
for ($i=1; $i<500; $i++) {
$arr[$i] = new test();
}

unset($arr);
}

There is _no difference_. The memory is freed correctly.


Objects:
class test {
public $t = 3;
}



function readData() {
$arr = array();
for ($i=1; $i<500; $i++) {
$arr[$i] = new test();
}
}
function readData2() {
$arr = array();
for ($i=1; $i<500; $i++) {
$arr[$i] = new test();
}
unset($arr);
}

There is a _difference_: The memory is not freed correctly. I also think 
this is a bug.

Of course, PHP will free used memory after the end of the script, but 
because PHP could also be used in complex scripts, it would be 
important, that the garbage collector will do what it promises.

I have found the error by creating a squarified cushion treemap. The 
algo really works good in PHP. But if you have large trees, than PHP 
needs to much memory.

Mathias
