PHP Pthread似乎有随机内存管理和内存泄漏
所以,我在这里遇到了死胡同。我已经尝试了我所知道的一切来隔离内存泄漏,从我收集的信息来看,它似乎与使用pthread对这个脚本进行多线程处理有关 我正在为维基百科编写一个机器人程序,我即将完成。从功能上讲,该程序是可靠的,在多线程和单线程中都能正常工作。内存泄漏仅在打开多线程时发生 两个版本在同一脚本/文件上使用完全相同的函数,以便于进行简单/一致的调试 发动机的螺纹在下面PHP Pthread似乎有随机内存管理和内存泄漏,php,multithreading,memory-leaks,pthreads,threadpool,Php,Multithreading,Memory Leaks,Pthreads,Threadpool,所以,我在这里遇到了死胡同。我已经尝试了我所知道的一切来隔离内存泄漏,从我收集的信息来看,它似乎与使用pthread对这个脚本进行多线程处理有关 我正在为维基百科编写一个机器人程序,我即将完成。从功能上讲,该程序是可靠的,在多线程和单线程中都能正常工作。内存泄漏仅在打开多线程时发生 两个版本在同一脚本/文件上使用完全相同的函数,以便于进行简单/一致的调试 发动机的螺纹在下面 //Multithread engine //This thread class allows for asyncron
//Multithread engine
//This thread class allows for asyncronous function calls. This is useful for the functions that consume time and can run in the background.
//Caution must be excercised to ensure that the functions are thread safe.
class AsyncFunctionCall extends Thread {
protected $method;
protected $params;
public $result;
public function __construct( $method, $params ) {
$this->method = $method;
$this->params = $params;
$this->result = null;
}
public function run() {
if (($this->result=call_user_func_array($this->method, $this->params))) {
return true;
} else return false;
}
public static function call($method, $params){
$thread = new AsyncFunctionCall($method, $params);
if($thread->start()){
return $thread;
} else {
echo "Unable to initiate background function $method!\n";
return false;
}
}
}
// Analyze multiple pages simultaneously and edit them.
class ThreadedBot extends Collectable {
protected $page, $pageid, $alreadyArchived, $ARCHIVE_ALIVE, $TAG_OVERRIDE, $ARCHIVE_BY_ACCESSDATE, $TOUCH_ARCHIVE, $DEAD_ONLY, $NOTIFY_ERROR_ON_TALK, $NOTIFY_ON_TALK, $TALK_MESSAGE_HEADER, $TALK_MESSAGE, $TALK_ERROR_MESSAGE_HEADER, $TALK_ERROR_MESSAGE, $DEADLINK_TAGS, $CITATION_TAGS, $IGNORE_TAGS, $ARCHIVE_TAGS, $VERIFY_DEAD, $LINK_SCAN;
public $result;
public function __construct($page, $pageid, $alreadyArchived, $ARCHIVE_ALIVE, $TAG_OVERRIDE, $ARCHIVE_BY_ACCESSDATE, $TOUCH_ARCHIVE, $DEAD_ONLY, $NOTIFY_ERROR_ON_TALK, $NOTIFY_ON_TALK, $TALK_MESSAGE_HEADER, $TALK_MESSAGE, $TALK_ERROR_MESSAGE_HEADER, $TALK_ERROR_MESSAGE, $DEADLINK_TAGS, $CITATION_TAGS, $IGNORE_TAGS, $ARCHIVE_TAGS, $VERIFY_DEAD, $LINK_SCAN) {
$this->page = $page;
$this->pageid = $pageid;
$this->alreadyArchived = $alreadyArchived;
$this->ARCHIVE_ALIVE = $ARCHIVE_ALIVE;
$this->TAG_OVERRIDE = $TAG_OVERRIDE;
$this->ARCHIVE_BY_ACCESSDATE = $ARCHIVE_BY_ACCESSDATE;
$this->TOUCH_ARCHIVE = $TOUCH_ARCHIVE;
$this->DEAD_ONLY = $DEAD_ONLY;
$this->NOTIFY_ERROR_ON_TALK = $NOTIFY_ERROR_ON_TALK;
$this->NOTIFY_ON_TALK = $NOTIFY_ON_TALK;
$this->TALK_MESSAGE_HEADER = $TALK_MESSAGE_HEADER;
$this->TALK_MESSAGE = $TALK_MESSAGE;
$this->TALK_ERROR_MESSAGE_HEADER = $TALK_ERROR_MESSAGE_HEADER;
$this->TALK_ERROR_MESSAGE = $TALK_ERROR_MESSAGE;
$this->DEADLINK_TAGS = $DEADLINK_TAGS;
$this->CITATION_TAGS = $CITATION_TAGS;
$this->IGNORE_TAGS = $IGNORE_TAGS;
$this->ARCHIVE_TAGS = $ARCHIVE_TAGS;
$this->VERIFY_DEAD = $VERIFY_DEAD;
$this->LINK_SCAN = $LINK_SCAN;
}
public function run() {
ini_set( 'memory_limit', '1G' );
echo ini_get( 'memory_limit' )."; ".(memory_get_usage( true )/1024/1024)." MB\n";
$this->result = analyzePage( $this->page, $this->pageid, $this->alreadyArchived, $this->ARCHIVE_ALIVE, $this->TAG_OVERRIDE, $this->ARCHIVE_BY_ACCESSDATE, $this->TOUCH_ARCHIVE, $this->DEAD_ONLY, $this->NOTIFY_ERROR_ON_TALK, $this->NOTIFY_ON_TALK, $this->TALK_MESSAGE_HEADER, $this->TALK_MESSAGE, $this->TALK_ERROR_MESSAGE_HEADER, $this->TALK_ERROR_MESSAGE, $this->DEADLINK_TAGS, $this->CITATION_TAGS, $this->IGNORE_TAGS, $this->ARCHIVE_TAGS, $this->VERIFY_DEAD, $this->LINK_SCAN);
$this->setGarbage();
$this->page = null;
$this->pageid = null;
$this->alreadyArchived = null;
$this->ARCHIVE_ALIVE = null;
$this->TAG_OVERRIDE = null;
$this->ARCHIVE_BY_ACCESSDATE = null;
$this->TOUCH_ARCHIVE = null;
$this->DEAD_ONLY = null;
$this->NOTIFY_ERROR_ON_TALK = null;
$this->NOTIFY_ON_TALK = null;
$this->TALK_MESSAGE_HEADER = null;
$this->TALK_MESSAGE = null;
$this->TALK_ERROR_MESSAGE_HEADER = null;
$this->TALK_ERROR_MESSAGE = null;
$this->DEADLINK_TAGS = null;
$this->CITATION_TAGS = null;
$this->IGNORE_TAGS = null;
$this->ARCHIVE_TAGS = null;
$this->VERIFY_DEAD = null;
$this->LINK_SCAN = null;
unset( $this->page, $this->pageid, $this->alreadyArchived, $this->ARCHIVE_ALIVE, $this->TAG_OVERRIDE, $this->ARCHIVE_BY_ACCESSDATE, $this->TOUCH_ARCHIVE, $this->DEAD_ONLY, $this->NOTIFY_ERROR_ON_TALK, $this->NOTIFY_ON_TALK, $this->TALK_MESSAGE_HEADER, $this->TALK_MESSAGE, $this->TALK_ERROR_MESSAGE_HEADER, $this->TALK_ERROR_MESSAGE, $this->DEADLINK_TAGS, $this->CITATION_TAGS, $this->IGNORE_TAGS, $this->ARCHIVE_TAGS, $this->VERIFY_DEAD, $this->LINK_SCAN );
}
}
程序体中的这个块调用线程引擎
if( WORKERS === false ) {
foreach( $pages as $tid => $tpage ) {
$pagesAnalyzed++;
$stats = analyzePage( $tpage['title'], $tpage['pageid'], $alreadyArchived, $ARCHIVE_ALIVE, $TAG_OVERRIDE, $ARCHIVE_BY_ACCESSDATE, $TOUCH_ARCHIVE, $DEAD_ONLY, $NOTIFY_ERROR_ON_TALK, $NOTIFY_ON_TALK, $TALK_MESSAGE_HEADER, $TALK_MESSAGE, $TALK_ERROR_MESSAGE_HEADER, $TALK_ERROR_MESSAGE, $DEADLINK_TAGS, $CITATION_TAGS, $IGNORE_TAGS, $ARCHIVE_TAGS, $VERIFY_DEAD, $LINK_SCAN );
if( $stats['pagemodified'] === true ) $pagesModified++;
$linksAnalyzed += $stats['linksanalyzed'];
$linksArchived += $stats['linksarchived'];
$linksFixed += $stats['linksrescued'];
$linksTagged += $stats['linkstagged'];
$alreadyArchived = array_merge( $stats['newlyArchived'], $alreadyArchived );
$failedToArchive = array_merge( $failedToArchive, $stats['archiveProblems'] );
$allerrors = array_merge( $allerrors, $stats['errors'] );
file_put_contents( $dlaaLocation, serialize( $alreadyArchived ) );
}
} else {
//for( $i = 0; $i < count( $pages ); $i += $workerLimit ) {
$workerQueue = new Pool( $workerLimit );
//$tpages = array_slice( $pages, $i, $workerLimit );
foreach( $pages as $tid => $tpage ) {
$pagesAnalyzed++;
echo "Submitted {$tpage['title']}, job ".($tid+1)." for analyzing...\n";
$workerQueue->submit( new ThreadedBot( $tpage['title'], $tpage['pageid'], $alreadyArchived, $ARCHIVE_ALIVE, $TAG_OVERRIDE, $ARCHIVE_BY_ACCESSDATE, $TOUCH_ARCHIVE, $DEAD_ONLY, $NOTIFY_ERROR_ON_TALK, $NOTIFY_ON_TALK, $TALK_MESSAGE_HEADER, $TALK_MESSAGE, $TALK_ERROR_MESSAGE_HEADER, $TALK_ERROR_MESSAGE, $DEADLINK_TAGS, $CITATION_TAGS, $IGNORE_TAGS, $ARCHIVE_TAGS, $VERIFY_DEAD, $LINK_SCAN ) );
}
$workerQueue->shutdown();
$workerQueue->collect(
function( $thread ) {
global $pagesModified, $linksAnalyzed, $linksArchived, $linksFixed, $linksTagged, $alreadyArchived, $failedToArchive, $allerrors;
$stats = $thread->result;
if( $stats['pagemodified'] === true ) $pagesModified++;
$linksAnalyzed += $stats['linksanalyzed'];
$linksArchived += $stats['linksarchived'];
$linksFixed += $stats['linksrescued'];
$linksTagged += $stats['linkstagged'];
$alreadyArchived = array_merge( $stats['newlyArchived'], $alreadyArchived );
$failedToArchive = array_merge( $failedToArchive, $stats['archiveProblems'] );
$allerrors = array_merge( $allerrors, $stats['errors'] );
return $thread->isGarbage();
});
echo "!!!!!!!!!!!!!!Links analyzed so far: $linksAnalyzed\n\n";
file_put_contents( $dlaaLocation, serialize( $alreadyArchived ) );
//$workerQueue = null;
//unset( $workerQueue );
//}
}
这是我第一次使用多线程,所以我是新手,所以我非常感谢您的帮助和建议,如果您有更多问题,请提问::-) 所以这不是来自pthreads。相反,多线程只是使问题更加明显。事实证明,我使用的是multicurl,由于使用了错误的函数来关闭句柄,所以尽管句柄已关闭,内存仍没有被释放。因此,事实证明这不是来自pthreads。相反,多线程只是使问题更加明显。原来我使用的是multicurl,由于使用了错误的函数来关闭句柄,所以尽管句柄已关闭,内存仍无法释放。您需要我做什么?我不知道如何在不丢失细节的情况下询问它。它不是最小的、完整的或可验证的。。。我需要确定pthreads中是否有问题,或者您的逻辑中是否有问题……您需要我做什么?我不知道如何在不丢失细节的情况下询问它。它不是最小的、完整的或可验证的。。。这需要我来确定pthreads或您的逻辑中是否存在问题。。。
Analyzed Stanley Hartt (8742961)
Rescued: 0; Tagged dead: 0; Archived: 0; Max System Memory Used: 1.25 MB
PHP Fatal error: Out of memory (allocated 46661632) (tried to allocate 6557907 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
Fatal error: Out of memory (allocated 46661632) (tried to allocate 6557907 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
Analyzed High-explosive anti-tank warhead (255968)
Rescued: 0; Tagged dead: 0; Archived: 5; Max System Memory Used: 22.75 MB
PHP Fatal error: Out of memory (allocated 14680064) (tried to allocate 6341940 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1261
Fatal error: Out of memory (allocated 14680064) (tried to allocate 6341940 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1261
PHP Fatal error: Out of memory (allocated 6291456) (tried to allocate 5243257 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
Fatal error: Out of memory (allocated 6291456) (tried to allocate 5243257 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
PHP Fatal error: Out of memory (allocated 7864320) (tried to allocate 5245685 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
Fatal error: Out of memory (allocated 7864320) (tried to allocate 5245685 bytes) in C:\Users\Maximilian Doerr\Documents\GitHub\Cyberbot_II\deadlink.php on line 1259
Analyzed Nadezhda Tylik (2896780)
Rescued: 0; Tagged dead: 0; Archived: 5; Max System Memory Used: 2.75 MB