Skip to content

Instantly share code, notes, and snippets.

@mikegioia
Created December 13, 2015 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikegioia/0ca42cd9cb2cd569deed to your computer and use it in GitHub Desktop.
Save mikegioia/0ca42cd9cb2cd569deed to your computer and use it in GitHub Desktop.
Memory allocation leak in imap_fetchheader
<?php
$email = "youremail@gmail.com";
$password = "password123"
$folder = "INBOX";
$imapStream = imap_open(
"{imap.gmail.com:993/imap/ssl}$folder",
$email,
$password );
if ( ! $imapStream ) {
echo "Error: ", imap_last_error(), PHP_EOL;
exit( 1 );
}
// For each mail ID call fetch headers and check the VSZ
// Use args sz,rss,vsz for more info.
$pid = getmypid();
$message = <<<STR
My PID is: $pid
Run this in another terminal:
$> watch -n 1 ps -o vsz $pid
STR;
echo $message;
for ( $i = 9; $i >= 1; $i-- ) {
echo "\r$i";
sleep( 1 );
}
echo "\rStarting...", PHP_EOL;
// Search the folder for mail IDs
$mailIds = imap_search( $imapStream, 'ALL', SE_UID, 'UTF-8' );
$count = count( $mailIds );
echo "Start Memory: ", memory_get_usage( TRUE ), ", ";
echo "Start Peak: ", memory_get_peak_usage( TRUE ), PHP_EOL;
foreach ( $mailIds as $mailId ) {
echo "\rMailID: $mailId / $count, ";
echo "Memory: ", memory_get_usage( TRUE ), ", ";
echo "Peak: ", memory_get_peak_usage( TRUE );
$headers = imap_fetchheader( $imapStream, $mailId, FT_UID );
$headers = NULL;
unset( $headers );
usleep( 100000 );
}
echo "\n\nDone!\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment