PHP/DOMDocument: unset() 不釋放資源 (PHP/DOMDocument: unset() does not release resources)


問題描述

PHP/DOMDocument: unset() 不釋放資源 (PHP/DOMDocument: unset() does not release resources)

I've got a variable $dom that contains an instance of PHP's DOMDocument. This variable is being set and unset within a loop.

unset($dom) will not release memory, though: memory_get_usage(false) reports that only 700B of about 10MB got released. memory_get_usage(true) reports the exact same amount of memory usage before and after unset().

I tried the following:

  • I checked that the reference count of $dom is exactly one immediately before the call to unset()xdebug_debug_zval('dom') reports: dom: (refcount=1, is_ref=0)=class DOMDocument {  }.

  • I tried the solution proposed in DOMDocument PHP Memory Leak but this was to no avail.

  • I tried gc_enable() + gc_collect_cycles(). But this did not help either.

  • I tried calling $dom‑>__destruct() before unset() but this will result in an error as DOMDocument apparently does not have a destructor.

  • I recursively removed all nodes in the DOM before I unset the variable. It did not make the least difference.

  • I tried setting the variable to null before re‑using it: $dom = null

Now I'm out of ideas... Anybody got a suggestion what else I could try to debug or solve this problem?

Edit:

  • php ‑v reports 5.3.10‑1ubuntu3.2 with Suhosin‑Patch (cli)

  • The source code can be seen here: https://github.com/jerico‑dev/ojs/blob/dev/plugins/generic/lucene/classes/SolrWebService.inc.php#L256

  • The output of the first few iterations is:

    before object creation: 19292296
    after object creation: 29849832
    before unset: 30055232
    after unset: 30054448
    before object creation: 29849592
    after object creation: 39858840
    before unset: 40079920
    after unset: 40079136
    before object creation: 39858272
    after object creation: 49923216
    before unset: 50136448
    after unset: 50135664
    

‑‑‑‑‑

參考解法

方法 1:

This should release the memory store in $dom.

$dom = null;

(by jericoMatthew Blancarte)

參考文件

  1. PHP/DOMDocument: unset() does not release resources (CC BY‑SA 3.0/4.0)

#domdocument #PHP #memory-leaks






相關問題

PHP/DOMDocument: unset() 不釋放資源 (PHP/DOMDocument: unset() does not release resources)

C++ Xerces Parser 加載 HTML 並蒐索 HTML 元素 (C++ Xerces Parser Load HTML and Search for HTML Elements)

Cách lấy tên thuộc tính kiểu bằng PHP xpath (How to get the style property name using PHP xpath)

DOMDocument:如何解析類似 bbcode 的標籤? (DOMDocument : how to parse a bbcode like tag?)

如何使用 DOMDocument 獲取此 html 中的 url (How to use DOMDocument to get url in this html)

DomDocument 未能為 RSS 提要添加“鏈接”元素 (DomDocument failing to add a "link" element for RSS feed)

如何防止將文檔類型添加到 HTML 中? (How to prevent the doctype from being added to the HTML?)

PHP DOM 文檔回顯問題 (PHP DOMdocument echoing problem)

使用 PHP 將數據放到服務器上(新的 DOMdocument 不起作用) (Use PHP to put data onto server ( new DOMdocument not working))

有沒有辦法構建類似於 DOMDocument 構建 HTML 文檔的 SQL 查詢? (Is there a way to build SQL queries similar to how DOMDocument builds HTML document?)

來自 URL 的 file_get_contents 僅適用於本地服務器 (file_get_contents from URL works on local server only)

使用多個 <table> 標記抓取 HTML 頁面並從特定的 <a> 標記後代中提取文本 (Scrape HTML page with multiple <table> tags and extract text from specific <a> tag descendants)







留言討論