{"id":55,"date":"2021-01-04T11:30:29","date_gmt":"2021-01-04T11:30:29","guid":{"rendered":"https:\/\/blog.gwlin.com\/?p=55"},"modified":"2023-03-29T18:17:23","modified_gmt":"2023-03-29T18:17:23","slug":"PHP \u5783\u573e\u56de\u6536\uff08GC\uff09\u673a\u5236","status":"publish","type":"post","link":"https:\/\/www.gwlin.com\/blog\/posts\/55","title":{"rendered":"PHP \u5783\u573e\u56de\u6536\uff08GC\uff09\u673a\u5236"},"content":{"rendered":"\n<p>\u5728\u901a\u5e38\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0cPHP\u5f00\u53d1\u8005\u5e76\u4e0d\u9700\u8981\u5173\u5fc3\u5e95\u5c42\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3002\u56e0\u4e3a\u5728\u4e00\u4e2a\u5b8c\u6210\u8bf7\u6c42\u5468\u671f\u4e4b\u540e\uff0cPHP\u8fdb\u7a0b\u5c06\u88ab\u91ca\u653e\uff0c\u6240\u6709\u5185\u5b58\u91cc\u7684\u53d8\u91cf\u4e5f\u5c06\u88ab\u6e05\u7a7a\u3002\u4f46\u5f53\u4ee5CLI\u7684\u65b9\u5f0f\u957f\u65f6\u95f4\u8fd0\u884cPHP\u811a\u672c\uff0cPHP\u7684\u5783\u573e\u56de\u6536\u5c06\u53d8\u5f97\u91cd\u8981\u3002<\/p>\n\n\n\n<p>PHP\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u7ecf\u7406\u8fc73\u4e2a\u4e0d\u540c\u7684\u65f6\u671f\uff1a5.2\u53ca\u4e4b\u524d\u7248\u672c\u30015.3-5.6\u30017.0\u4e4b\u540e\u3002\u4e0b\u9762\u5206\u522b\u8be6\u7ec6\u8bb2\u8bb2\u3002<\/p>\n\n\n\n<p class=\"has-normal-font-size\"><strong>5.2\u53ca\u4e4b\u524d\u7248\u672c<\/strong><\/p>\n\n\n\n<p> \u9996\u5148\u6211\u4eec\u770b\u4e00\u4e0bPHP\u662f\u600e\u4e48\u50a8\u5b58\u53d8\u91cf\u7684\uff1a <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"c\" class=\"language-c\">typedef struct _zval_struct {\n    zvalue_value value;\n    zend_uint refcount__gc;\n    zend_uchar type;\n    zend_uchar is_ref__gc;\n} zval;<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u662fPHP\u50a8\u5b58\u53d8\u91cf\u4fe1\u606f\u7684\u6e90\u7801\uff0c\u662f\u4e00\u4e2aC\u7684\u7ed3\u6784\u4f53\u3002\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6709\u4e24\u4e2a\u6709__gc\u540e\u7f00\u7684\u5b57\u6bb5\uff1arefcount__gc\uff0cis_ref__gc\u3002\u5176\u4e2drefcount\u662f\u5f15\u7528\u8ba1\u6570\uff0cis_ref\u662f\u201c\u662f\u5426\u662f\u5f15\u7528\u53d8\u91cf\u201d\u7684\u6807\u8bc6\u3002\u5f15\u7528\u8ba1\u6570\u4f1a\u5728\u6709\u53d8\u91cf\u6307\u5411\u5b83\u7684\u65f6\u5019\u52a01\uff0c\u53d8\u91cf\u88abunset\u7684\u65f6\u5019\u51cf1\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u65f6\u671fPHP\u5e76\u6ca1\u6709\u4e13\u95e8\u7684\u5783\u573e\u56de\u6536\u5668 \uff0c\u8981\u5224\u65ad\u4e00\u4e2a\u53d8\u91cf\u5bb9\u5668\u7684\u662f\u5426\u53ef\u4ee5\u88ab\u91ca\u653e\uff0c\u5c31\u662f\u5224\u65adrefcount\u662f\u5426\u4e3a0\uff0c\u4e3a0\u7684\u65f6\u5019\u5c31\u7acb\u5373\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u4f46\u8fd9\u6837\u7684\u65b9\u5f0f\u65e0\u6cd5\u5224\u65ad\u53d8\u91cf\u5faa\u73af\u5f15\u7528\uff0c\u4e8e\u662f\u65e0\u6cd5\u91ca\u653e\u53d8\u91cf\u5bb9\u5668\uff0c\u5bfc\u81f4PHP\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u6b64\u57285.3\u5f15\u5165\u4e86\u65b0\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3002<\/p>\n\n\n\n<p><strong>5.3-5.6\u7248\u672c<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u4e9b\u7248\u672c\u4e2d\uff0cPHP\u628a\u90a3\u4e9b\u53ef\u80fd\u662f\u5783\u573e\u7684\u53d8\u91cf\u5bb9\u5668\u653e\u5165\u6839\u7f13\u51b2\u533a\uff0c\u5f53\u6839\u7f13\u51b2\u533a\u6ee1\u4e86\u4e4b\u540e\u5c31\u4f1a\u542f\u52a8\u65b0\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u8981\u6e05\u695a\u4e00\u70b9\uff0c\u4ec0\u4e48\u662f\u53ef\u80fd\u7684\u5783\u573e\u3002\u5982\u679c\u53d8\u91cf\u5bb9\u5668\u7684refount\u589e\u52a0\uff0c\u90a3\u4e48\u663e\u7136\u6709\u65b0\u7684\u53d8\u91cf\u6307\u5411\u5b83\uff0c\u5b83\u5c31\u4e0d\u53ef\u80fd\u662f\u5783\u573e\u3002\u800c\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u5bb9\u5668\u7684refcount\u51cf\u52300\uff0c\u90a3\u4e48\u5b83\u81ea\u7136\u4f1a\u88ab\u91ca\u653e\u3002\u53ea\u6709\u50cf\u4e0a\u9762\u6240\u8bf4\u7684\uff0c\u5f53\u4e00\u4e2a\u53d8\u91cf\u5bb9\u5668\u7684refcount\u51cf\u5c11\uff0c\u4f46\u4e0d\u4e3a0\u7684\u65f6\u5019\uff0c\u5b83\u624d\u53ef\u80fd\u662f\u4e00\u4e2a\u5783\u573e\uff0c\u624d\u4f1a\u8fdb\u5165\u5783\u573e\u56de\u6536\u5468\u671f\u3002<\/p>\n\n\n\n<p>\u5728\u5783\u573e\u56de\u6536\u4e2d\uff0c\u5224\u65ad\u4e00\u4e2a\u5bb9\u5668\u662f\u5426\u662f\u771f\u7684\u5783\u573e\uff0c\u5176\u601d\u8def\u5e76\u4e0d\u590d\u6742\uff1a\u5982\u679c\u4e00\u4e2a\u53d8\u91cf\u5bb9\u5668\u7684refcount\u7684\u503c\u5168\u90e8\u6765\u81ea\u5176\u5f15\u7528\u81ea\u8eab\uff0c\u90a3\u4e48\u5b83\u662f\u4e00\u4e2a\u5783\u573e\u3002\u5177\u4f53\u64cd\u4f5c\u662f\u5bf9\u53d8\u91cf\u53ca\u5176\u6210\u5458\u7684refcount\u8fdb\u884c\u6a21\u62df\u5220\u9664\uff0c\u5373\u51cf1\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u50cf\u6211\u4eec\u4e0a\u9762\u8bf4\u7684\uff0c\u5982\u679c\u5b83\u7684refcount\u503c\u90fd\u6765\u81ea\u5176\u6210\u5458\u5f15\u7528\u81ea\u8eab\uff0c\u90a3\u4e48\u6a21\u62df\u5220\u9664\u540e\uff0c\u5b83\u7684refcount\u503c\u5c31\u4f1a\u53d8\u62100\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u65ad\u5b9a\u5b83\u662f\u4e00\u4e2a\u5783\u573e\uff0c\u8981\u8fdb\u884c\u56de\u6536\u3002<\/p>\n\n\n\n<p><strong>7.0\u4e4b\u540e<\/strong><\/p>\n\n\n\n<p>PHP7\u5bf9\u6bd4PHP5\u5728\u6027\u80fd\u4e0a\u662f\u4e00\u4e2a\u5f88\u5927\u7684\u63d0\u5347\uff0c\u5185\u5b58\u6d88\u8017\u4e5f\u8981\u5c11\u4e0d\u5c11\u3002\u8fd9\u662f\u5f97\u76ca\u4e8ePHP\u53d8\u91cf\u5bb9\u5668zval\u7684\u5168\u65b0\u8bbe\u8ba1\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"c\" class=\"language-c\">struct _zval_struct {\n    zend_value value;\n    union {\n        struct {\n            ZEND_ENDIAN_LOHI_4(\n                zend_uchar type,\n                zend_uchar type_flags,\n                zend_uchar const_flags,\n                zend_uchar reserved)\n        } v;\n        uint32_t type_info;\n    } u1;\n    union {\n        uint32_t var_flags;\n        uint32_t next;                 \/\/ hash collision chain\n        uint32_t cache_slot;           \/\/ literal cache slot\n        uint32_t lineno;               \/\/ line number (for ast nodes)\n        uint32_t num_args;             \/\/ arguments number for EX(This)\n        uint32_t fe_pos;               \/\/ foreach position\n        uint32_t fe_iter_idx;          \/\/ foreach iterator index\n    } u2;\n};<\/code><\/pre>\n\n\n\n<p>\u5728PHP7\u4e2d\u4e0d\u540c\u7c7b\u578b\u7684\u53d8\u91cf\u50a8\u5b58\u53ca\u9500\u6bc1\u7684\u65b9\u5f0f\u5e76\u4e0d\u76f8\u540c\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u7b80\u5355\u53d8\u91cf\uff0c\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u3001\u5e03\u5c14\u503c\u7b49\uff0cPHP\u4e0d\u518d\u662f\u5199\u65f6\u590d\u5236\uff0c\u800c\u662f\u5373\u65f6\u5206\u914d\uff0c\u56e0\u6b64\u4e0d\u9700\u8981refcount\u3002\u5f53unset\u6389\u4e00\u4e2a\u53d8\u91cf\u7684\u65f6\u5019\uff0c\u5bf9\u5e94\u7684zval\u5bb9\u5668\u4f1a\u88ab\u6807\u8bb0\u4e3aIS_UNDEF\uff0c\u7b49\u5f85\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u590d\u6742\u53d8\u91cf\u5982\u6570\u7ec4\u3001\u5bf9\u8c61\u7b49\uff0cPHP\u8fd8\u662f\u5171\u4eab\u4e00\u4e2azval\uff0c \u5199\u65f6\u590d\u5236\u3002\u5f53\u53d1\u751f\u53d8\u91cf\u5f15\u7528\u65f6\uff0cPHP\u4f1a\u5206\u914d\u4e00\u4e2azend_reference\u7ed3\u6784\u3002\u53d8\u91cf\u6307\u5411\u8fd9\u4e2azend_reference\u7ed3\u6784\uff0c\u91cc\u9762\u4fdd\u5b58\u7740refcount\u8ba1\u6570\uff0c\u8fd9\u4e2a\u7ed3\u6784\u518d\u6307\u5411\u5b9e\u9645zval\u5bb9\u5668\u3002\u56e0\u6b64\u5f53\u53d1\u751f\u5faa\u73af\u5f15\u7528\uff0c\u8fdb\u5165\u56de\u6536\u5468\u671f\u4e4b\u540e\uff0cPHP\u4f1a\u5bf9\u8fd9\u4e2azend_reference\u7ed3\u6784\u8fdb\u884c\u6a21\u62df\u5220\u9664\u64cd\u4f5c\uff0c\u5982\u540cPHP5\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"c\" class=\"language-c\">typedef struct _zend_refcounted_h {\n    uint32_t         refcount; \/\/ \u8bb0\u5f55 zend_value \u7684\u5f15\u7528\u6570\n    union {\n        struct {\n            zend_uchar    type,  \/\/ zend_value\u7684\u7c7b\u578b, \u4e0ezval.u1.type\u4e00\u81f4\n            zend_uchar    flags, \n            uint16_t      gc_info \/\/ GC\u4fe1\u606f\uff0c\u8bb0\u5f55\u5728 gc \u6c60\u4e2d\u7684\u4f4d\u7f6e\u548c\u989c\u8272\uff0c\u5783\u573e\u56de\u6536\u7684\u8fc7\u7a0b\u4f1a\u7528\u5230\n        } v;\n        uint32_t type_info;\n    } u;\n} zend_refcounted_h;<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\u6587\u7ae0\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/nikic.github.io\/2015\/05\/05\/Internal-value-representation-in-PHP-7-part-1.html\">https:\/\/nikic.github.io\/2015\/05\/05\/Internal-value-representation-in-PHP-7-part-1.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/learnku.com\/articles\/33451\">https:\/\/learnku.com\/articles\/33451<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u901a\u5e38\u7684\u5e94\u7528\u573a\u666f\u4e2d\uff0cPHP\u5f00\u53d1\u8005\u5e76\u4e0d\u9700\u8981\u5173\u5fc3\u5e95\u5c42\u7684\u5783\u573e\u56de\u6536\u673a\u5236\u3002\u56e0\u4e3a\u5728\u4e00\u4e2a\u5b8c\u6210\u8bf7\u6c42\u5468\u671f\u4e4b\u540e\uff0cPHP\u8fdb\u7a0b\u5c06\u88ab\u91ca\u653e\uff0c\u6240\u6709\u5185\u5b58\u91cc\u7684\u53d8\u91cf\u4e5f\u5c06\u88ab\u6e05\u7a7a\u3002\u4f46\u5f53\u4ee5CLI\u7684\u65b9\u5f0f\u957f\u65f6\u95f4\u8fd0\u884cPHP\u811a\u672c\uff0cPHP\u7684\u5783\u573e\u56de\u6536\u5c06\u53d8\u5f97\u91cd\u8981\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-notes"],"_links":{"self":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":0,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}