I query an Oracle database using PHP but at a certain point, probably dued to the fact that the PHP code is very complex, I get a memory leak. I'm working to solve this but I ask you: if I convert all the complex PHP code to C code and I call an "exec" from PHP when needed, do I gain something in performance and memory optimization? Or is it a bad idea? And why?
-
4Guess that depends if your C code also has a memory leak. Let's see the PHP code in question. Perhaps it can be fixed. – webbiedave Nov 02 '10 at 17:02
-
4It's a bad idea. If you cannot write proper PHP, you will suffer in C. – Nicolas Viennot Nov 02 '10 at 17:14
-
Hey, who says I can't write proper PHP (or even C)??? – Lotus1 Nov 02 '10 at 17:45
4 Answers
If you want to rewrite the code in C, then you'd be better off writing it as a PHP module than as a standalone program called from PHP using exec().

- 209,507
- 32
- 346
- 385
-
Hi, and thanks for your reply. The fact is that for some reasons I can't easily explain here I can't write a PHP module and I can't change memory_limit inside PHP.ini. – Lotus1 Nov 02 '10 at 17:33
Just a semi-related thought:
http://github.com/facebook/hiphop-php/wiki
HipHop for PHP transforms PHP source code into highly optimized C++. It was developed by Facebook and was released as open source in early 2010.

- 69,876
- 20
- 132
- 137
-
However, the entire application must be "converted", you can't simply convert individual scripts/classes; and you're limited to the modules that the good Facebook folks have rebuilt to run with HipHop – Mark Baker Nov 02 '10 at 17:22
-
Sure- its only an aside, though it is open source so you do have the optionality (if you have the knowledge) to tailor as you see fit – SW4 Nov 02 '10 at 17:23
Generally speaking, you will lose performance since the system has to spawn another process and wait for it to terminate before it gets back to your PHP script. Memory leaks in PHP are something I rarely see, although they can happen. It can also be the underlying C wrapper that leaks memory (the Oracle DB wrapper, per example). Installing a debugger like Xdebug can help you find the cause of this leak.
I suggest you read: Finding cause of memory leaks in large PHP stacks.
PHP 5.3 also introduced a garbage collector.
Any complexity is relative. If you can't break down a complexity, writing it in another form or language doesn't help.
In short: can't answer without some context.

- 15,731
- 6
- 49
- 56