Page 1 of 1
Xml parser - how to free memory space??
Posted: Wed Jul 12, 2006 6:10 pm
by nivritha_g
Hello,
I have written an XML parser in php using
xml_parser_create() and
xml_set_element_handler(). The xml file parsed is around 73MB. My problem is, even after the XML file is parsed, the results obtained and the browser closed, it's still taking up about 30% of memory (by typing ps -aux at the command line). How do I get it to free up the memory after execution?
Please, any help would be great!
Thank you in advance.
Nivi.
Posted: Wed Jul 12, 2006 6:18 pm
by Weirdan
xml_parser_free and ob_end_clean don't seem to work
Posted: Thu Jul 13, 2006 12:29 pm
by nivritha_g
I have tried xml_parser_free() as well as ob_end_clean().
But none of them seem to work.
By unset(), u mean unset every variable I have used individually?
Any suggestions?
Thank you.
Nivi.
Posted: Thu Jul 13, 2006 12:42 pm
by Ollie Saunders
You could try executing the code inside a function and keep all the variables you use local to it. Although tbh this kind of problem shouldn't ever occur in PHP.
Re: Xml parser - how to free memory space??
Posted: Thu Jul 13, 2006 2:06 pm
by Roja
nivritha_g wrote:My problem is, even after the XML file is parsed, the results obtained and the browser closed, it's still taking up about 30% of memory (by typing ps -aux at the command line). How do I get it to free up the memory after execution?
It is likely that what you think you see, isn't what you really see.
In Linux (and other Unix variants), ps shows the amount of memory used by a process. However, the memory used can (and in the case of Apache, does) include caches.
In a nutshell, it is more expensive for a process to release memory, and request it again (often), so they have a tendency to be 'laggy' in releasing the memory. After they *clear* the variables from memory, they will keep a large amount of the allocated memory as allocated free space for it to use. Most people see it as "memory not being freed", when in fact, it is the application/daemon trying to provide a better, more responsive performance.
There are of course ways around that. If I recall correctly, reloading the config (NOT restarting the server) triggers Apache's release/cleanup cycle, so that should serve your purpose.
Of course, if it is PHP itself (running as a CGI, for example), then I'm stumped on how to get PHP to release it more quickly (short of the unset() mentioned earlier)
Resolved!!!!
Posted: Thu Jul 13, 2006 4:46 pm
by nivritha_g
Hello,
I finally understood what the problem was.
I was reading the whole xml file into an array using file() command (though i needed just about first 100 lines). Unsetting the array didn't make any difference.
But if I read just the lines I need using fgets(), the memory usage problem just disappeared.
Thank you all for your advice.
Nivi.