Wednesday, April 24, 2019

php - Multiple foreach loop and just another memory leak story

To whom may concern,




As we are already known foreach is a painful and a headache while we using it to deal with thoudsands or millions of record. And it will become a bloody killer machine if we have a twin or may be more foreach in a row :)



For example,



foreach ($parent as $parentData) {
// few conditional added here
$parentObj = $this->extract();
foreach ($chilren as $childrenData) {
if ($childrenData['id'] === $parentData['id']) {

$childrenObj = $this->extract();
$parentObj->setData($childrenObj);
// and even more evil things come here....
}
}
$parentObj->save();
}


In my situation, I have twin foreach. And each one contains approximate 50,000 ~ 70,000 records. $parent and $children are parameters passed to the method.




The raw data source both of $parent and $children are CSV files. And I'm using yield to transform them to be travelsable with foreach.



There is no trouble with yield, belive me. It's guaranteed by a 60k+ player of this site :)



If you concern about the code: https://stackoverflow.com/a/37342184/2932590



Back to my concernation, I tried to unset both of $parentObj and $childrenObj at the end of first foreach, but unfortunately it's not working. I also tried to using the references &$parentData, but the result is same.



How could I make this work till the rest of my life?




Thanks in Advanced.





I got few advised to use SPL Iterators in this case. Can anyone please explain me how it work also?



Thanks.






I'm using SPL Iterator, below is the new code:



$parent = new IteratorIterator(new ArrayIterator($parentArr));
$children = new IteratorIterator(new ArrayIterator($chilrenArr));
foreach ($parent as $index => $parentData) {
$parentObj = null;
// few conditional added here
$parentObj = $this->extract();
$childrenObj = null;

foreach ($chilren as $key => $childrenData) {
if ($childrenData['id'] === $parentData['id']) {
$childrenObj = $this->extract();
$parentObj->setData($childrenObj);
// and even more evil things come here....
}
}
$parentObj->save();
$childrenObj->save();
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...