Zend certified PHP/Magento developer

Help needed with loop optimization. PHP for some reason is not clearing memory between iterations. Like memory usage remains constant for some iterations but after like 40/50 iterations gap, it’ll increase, so after a few thousands iterations it runs out of memory. I’m working on magento 2.3. Help!!