Reputation: 13
I am a novice PHP developer and in the process of learning this great language. I now end up having a script consisting of 1,343,579 lines and when I try to run this I am getting the following:
Fatal error: Out of memory (allocated 269221888) (tried to allocate 536870912 bytes) in Unknown on line 0
I have a same piece of code being repeated 512 times because I am allowing 2^9 different combinations accounting for different Search Options. I am trying my best to minimise memory leak by using unset()
, nulling variables and sometimes explicitly calling the gc_collect_cycles()
.
Would you please let me know whether 1 million line code will simply end up having this memory problem OR I am doing something silly in relation to memory management.
Upvotes: 0
Views: 712
Reputation: 74008
Split up your source into smaller parts and include only what you really need. When you repeat the same thing over and over again, refactor this into a separate function and call it with appropriate parameters.
Without knowing the concrete task, we cannot give more advice.
You can build your query string gradually depending on your conditions
function execute_query($pdo, $cond1, $cond2, $cond3, ..., $cond9)
{
$params = array();
$bind_types = '';
$sql = 'select * from mytable where';
$sql = "$sql condition1 = ?";
$params[] = $cond1;
if (isset($cond2)) {
$sql = "$sql and condition2 = ?";
$params[] = $cond2;
}
if (isset($cond3)) {
$sql = "$sql and condition3 = ?";
$params[] = $cond3;
}
...
$sth = $pdo->prepare($sql);
return $sth->execute($params);
}
Upvotes: 1
Reputation: 2790
Memory leak isn't normal, and shouldn't happen. You can try some solution to dismember this code on small parts and do its structured.
You can try some MVC solution like ZEND, or something. Make sure your code doesn't doing nothing more then you need. Review all.
Gl on that.
Upvotes: 0