Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP memory question do you need to unset?

Tags:

php

memory

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 ends. Am I wrong?

like image 339
Mark Avatar asked Jul 11 '26 21:07

Mark


2 Answers

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.

like image 118
RonLugge Avatar answered Jul 14 '26 13:07

RonLugge


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.

like image 42
mcrumley Avatar answered Jul 14 '26 12:07

mcrumley