If you notice that you're program's memory usage is increasing (as observed using ps) while calling the imap functions, calling this function should free that memory.(PHP 5 >= 5.3.0, PHP 7, PHP 8)
imap_gc — Clears IMAP cache
Purges the cache of entries of a specific type.
imapAn IMAP\Connection instance.
flags
       Specifies the cache to purge. It may one or a combination
       of the following constants: 
       IMAP_GC_ELT (message cache elements), 
       IMAP_GC_ENV (envelope and bodies),
       IMAP_GC_TEXTS (texts).
      
   Always returns true.
  
   Throws a ValueError if flags
   is invalid.
  
| Versione | Descrizione | 
|---|---|
| 8.1.0 | The imapparameter expects an IMAP\Connection
  instance now; previously, a validimapresource was expected. | 
| 8.0.0 | A ValueError is now thrown on invalid flagsparameter values. Previously,
       a warning was emitted and the function returnedfalse. | 
Example #1 imap_gc() example
<?php
$mbox = imap_open("{imap.example.org:143}", "username", "password");
imap_gc($mbox, IMAP_GC_ELT);
?>