PHP memory question do you need to unset?
What happens if you do not unset an array before the script is done executing?
I am running through thousands of CSV files, parsing data for hundreds of thousands of customers into arrays. It works fine for the first 5/6 hours then starts bogging down bad.
I run about 5-10 CSVs per execution...I'm wondering if unsetting the arrays in the script would help this or not...I thought they would be unallocated after the script开发者_Python百科 ends. Am I wrong?
As far as I'm aware, arrays -- like all memory -- should die when the script does.
Is your PHP script being invoked by another PHP script? If you're doing it by 'include', that essentially takes your 'lower' level PHP script and plugs it into the higher level one -- which would cause them to persist.
All memory is cleared when the script ends.
Have you tried using memory_get_peak_usage()
and memory_get_usage()
? They can be useful for finding memory allocation problems.
All used memory should be cleaned up after the script successfully finishes. If not this is a bug in PHP. unseting arrays wont help here.
I think it depends which version of PHP you are running. PHP 5.3 has an improved garbage collection mechanism which should prevent this memory leak of sorts. This page (http://www.php.net/manual/en/features.gc.performance-considerations.php) documents the issue in versions prior to 5.3 but suggests that you can manually invoke garbage collection by using the gc_collect_cycles()
function (if I've read it properly).
Unset
just destroys a variable - it doesn't free memory. If that behaviour occurs after hours of runtime, and this is a one-time script, maybe split the CSV files into smaller blocks to speed things up and help finding out where the problem starts to occur.
精彩评论