长时间运行的PHP脚本使httpd.exe使用大量内存

I've written a script that connects to a mail server to download all attachments and store them on a database.

Since the mailbox is very large, and the mail server is quite slow, it takes a few hours to complete.

After processing about 10.000 messages (message count is not exact and varies), the script dies with error: "PHP Fatal error: Out of memory (allocated 7077888) (tried to allocate 2815819 bytes)"

I've checked Task Manager and the memory usage of httpd.exe process gradually rises up to 1.5GB (after 1 and a half hours while the script is running), altough memory_get_usage() placed within the loop constantly reports something about 351.33kB, wich doesn't make me feel that there is a leak in my script. And even if there is a leak, how am I supposed to check it?

I'm running Apache/2.2.17 (Win32) with PHP/5.4.16 (SAPI) on Windows Server 2008 R2

My loop consists of reading email content with imap functions and storing attachment contents in database, one by one

I have switched to memory_get_usage(true), wich reported almost constantly 1.25MB