Memory Management
Posted: Mon May 19, 2003 1:30 pm
I wonder what strategies people use to minimise memory usage.
1.VARS
(a) Simple one is to use $result['key'], $_POST['key'], $GLOBALS['key'] etc rather than declaring $var=$result['key'] .. and so on. If I'm processing user input, I'll often just to over-write the original, ie something like $_POST['var'] = addslashes($_POST['var']); rather than $var = addslashes($_POST['var']); Another advantage of that is that it remains clear throughout the script where the var came from.
2.RESULT RESOURCES
(a) Trivial one is not to find any column names you don't need.
(b) I wonder if you think it's best always to use either mysql_fetch_row() or mysql_fetch_assoc() (if you want key names). My thinking is that mysql_fetch_array() creates an array twice the size of the other two.
(c) According to the manual, a garbage collector thingummy frees up results when they're no longer needed. Does this operate only at the end of the script? If so, might be good to mysql_free_result() after you've finished fetching. This might only produce a noticable gain with a very long script and a very big result - or maybe it's not really necessary at all.
3.OBJECTS
(a) I've always wondered if creating a class object uses a significant amount of extra memory compared to simply using the same bunch of functions on their own - assuming it is feasible to use the functions without wrapping them in a class.
(b) Once I have instantiated a class, and got whatever output I need from it, suppose I want to free up the memory it uses without waiting until the end of the script. If $class = new Class; would I just unset($class) or would that destroy a var without actually destroying the object? I would guess the former since there doesn't seem to be a free_object() fn analogous to mysql_free_result().
Again, it might not really matter unless the object is a big resource hog and it's a very long script.
-------------------------------
Freeing up result resources or objects before waiting for the script to end might help to reduce peak memory load on a very busy site but, I guess, this isn't really going to be a problem on the vast majority of sites. I just want to be ready for the day when Yahoo comes knocking on my door..
1.VARS
(a) Simple one is to use $result['key'], $_POST['key'], $GLOBALS['key'] etc rather than declaring $var=$result['key'] .. and so on. If I'm processing user input, I'll often just to over-write the original, ie something like $_POST['var'] = addslashes($_POST['var']); rather than $var = addslashes($_POST['var']); Another advantage of that is that it remains clear throughout the script where the var came from.
2.RESULT RESOURCES
(a) Trivial one is not to find any column names you don't need.
(b) I wonder if you think it's best always to use either mysql_fetch_row() or mysql_fetch_assoc() (if you want key names). My thinking is that mysql_fetch_array() creates an array twice the size of the other two.
(c) According to the manual, a garbage collector thingummy frees up results when they're no longer needed. Does this operate only at the end of the script? If so, might be good to mysql_free_result() after you've finished fetching. This might only produce a noticable gain with a very long script and a very big result - or maybe it's not really necessary at all.
3.OBJECTS
(a) I've always wondered if creating a class object uses a significant amount of extra memory compared to simply using the same bunch of functions on their own - assuming it is feasible to use the functions without wrapping them in a class.
(b) Once I have instantiated a class, and got whatever output I need from it, suppose I want to free up the memory it uses without waiting until the end of the script. If $class = new Class; would I just unset($class) or would that destroy a var without actually destroying the object? I would guess the former since there doesn't seem to be a free_object() fn analogous to mysql_free_result().
Again, it might not really matter unless the object is a big resource hog and it's a very long script.
-------------------------------
Freeing up result resources or objects before waiting for the script to end might help to reduce peak memory load on a very busy site but, I guess, this isn't really going to be a problem on the vast majority of sites. I just want to be ready for the day when Yahoo comes knocking on my door..