Perlã®ã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³(GC)ã®ç¹æ§ã«ã¤ãã¦
ããªãã¸ã§ã¯ã(å¤æ°ãå¤)ãå¿ è¦ã§ã¯ãªããªã£ãã¨ãã«ãèªåçã«ã¡ã¢ãªä¸ãã解æ¾ããä»çµã¿ãã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ã¨ããã¾ããã¬ã¼ããã¸ã¨ããã®ã¯ããã¿ã¨ããæå³ãã³ã¬ã¯ã·ã§ã³ã¨ããã®ã¯éããã¨ããæå³ã§ãã
Perlã®ã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ã¯åç §ã«ã¦ã³ãæ¹å¼
ãPerlã®ã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ã¯åç §ã«ã¦ã³ãæ¹å¼ã¨å¼ã°ãããã®ã§ãããªãã¸ã§ã¯ãã¸ã®åç §ãä½æããããã³ã«ãåç §ã«ã¦ã³ããã²ã¨ã¤å¢ããã¾ããå対ã«ããªãã¸ã§ã¯ãã®åç §ãåãé¤ãããã¨ãã«ãåç §ã«ã¦ã³ããã²ã¨ã¤æ¸ããã¾ããåç §ã«ã¦ã³ãã0ã«ãªã£ãã¿ã¤ãã³ã°ã§ããã®ãªãã¸ã§ã¯ãã¯è§£æ¾ããã¾ãã
ãä¸è¬è«ã§ããã°ãåç §ã«ã¦ã³ãæ¹å¼ã¯ãå®è£ ãåç´ã§é«éã§ããããã¯Perlã®ããã©ã¼ãã³ã¹ä¸ã®ããç¹æ§ã ã¨ããã¾ããã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ã§ãããã©ã¼ãã³ã¹ã®åé¡ã§æ©ãã¨ããäºä¾ãPerlã§ã¯èãããã¨ããªãã§ã(ä»ã®ã¨ãã)ã
ã次ã«ããªãã¸ã§ã¯ããç ´æ£ãããã¿ã¤ãã³ã°ã¯ãåç §ããªããªã£ãã¿ã¤ãã³ã°ãªã®ã§ãããã°ã©ãã¼ãããªãã¸ã§ã¯ãã®ç ´æ£ã®ã¿ã¤ãã³ã°ãæ£ç¢ºã«ç¥ããã¨ãã§ãã¾ãã
ããã¨ãã°Javaã§ã¯ãã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ãä¸ä»£å¥CGã¨å¼ã°ããå¾ã«è§£æ¾ããããªãã¸ã§ã¯ããåå¨ããããã«ãããã«å®è¡ãããå¾å¦çãfinalizeã«æ¸ããã¨ã¯ã§ãã¾ãããPerlã®å ´åã¯ããªãã¡ã¬ã³ã¹ã«ã¦ã³ãæ¹å¼ã§ãã®ã§ããã¹ãã©ã¯ã¿DESTROYã«ãå¦çãè¨è¿°ããã°ãåç §ããªããªã£ãã¿ã¤ãã³ã°ã§å¦çãå®è¡ãããã¨ãã§ãã¾ãã
ãäºçªç®ã®ç¹å¾´ã¯çã¡ã¢ãªã§ããåç §ããªããªã£ãã¿ã¤ãã³ã°ã§å¦çããããã®ã§ãã¡ã¢ãªä¸ã«ãªãã¸ã§ã¯ããæ®ãã¾ãããç¾ä»£ã§ã¯ã¡ã¢ãªã¨ããã®ã¯ããããã¤ããã¨ãã§ããããã«ãªã£ãã®ã§ããã®æ¬ ç¹ã¯æã»ã©ã¯ãç®ç«ããªããªãã¾ããããã¡ã¢ãªé£ããªããã°ã©ã ã¯ãå°ããªãã·ã³ã§ã¯ãã¹ã¯ããã¤ã³ãã¹ã¯ããã¢ã¦ããèµ·ããåå ã«ããªãã®ã§ãçã¡ã¢ãªãªã»ããã管çããããã¨ãããã§ãããã
ãä¸ã¤ç®ã¯ãä¸ä»£å¥GCã®ããã«ãFullGCã®ã¿ã¤ãã³ã°ã§åæ¢ããªããã¨ã§ããä¸ä»£å¥GCã§ã¯ãç¹å®ã®ã¿ã¤ãã³ã°ã§ãå¤ãã®ãªãã¸ã§ã¯ãã解æ¾ãããFullGCã¨å¼ã°ãããã®ãããã¾ããFullGCã®æä¸ã¯ãããã°ã©ã ãã¹ããããã¾ããã§ãWebã¢ããªãªã©ã§ã¯ãããã»ã©æ°ã«ãããã¨ã¯ãªãã§ããã(ãã¶ã)ã
åç §ã«ã¦ã³ãæ¹å¼ã®æ¬ ç¹ã¯ç¸äºåç §ã解決ã§ããªããã¨
ãåç §ã«ã¦ã³ãæ¹å¼ã®æãããªæ¬ ç¹ã¯ãç¸äºåç §ã解決ã§ããªããã¨ã§ããç¸äºã«åç §ããã¦ãããªãã¸ã§ã¯ãã¯ãã¡ã¢ãªä¸ãã解æ¾ããã¾ããããªãã¸ã§ã¯ããç¸äºåç §ãã¾ãã¯å¾ªç°åç §ãã¦ããå ´åã¯ãããã°ã©ãã¼ãæ°ãã¤ããå¿ è¦ãããã¾ãã
# ç¸äºåç § my $hash1 = {}; my $hash2 = {}; $hash1->{hash2} = $hash2; $hash2->{hash1} = $hash1;
ãä¸è¨ã®ããã«è¨è¿°ããã ãã§ãç¸äºåç §ãçºçãã¦ããã®ãªãã¸ã§ã¯ãã¯èªåçã«ã¯è§£æ¾ããã¾ãããããã¯ãããã°ã©ãã¼ã注æããã¦ããªãã¨ã容æã«èµ·ããã¾ããã§ãã®ã§ãç¸äºåç §ãã¦ããå ´åã¯ãããã°ã©ãã¼ãèªåã§ãã©ã¡ããã®ãªãã¸ã§ã¯ãã®åç §ããåç §ã«ã¦ã³ããå¢ãããªããã¦ã£ã¼ã¯ãªãã¡ã¬ã³ã¹ã«å¤æ´ãã¦ãããå¿ è¦ãããã¾ãã
use Scalar::Util 'weaken'; # ç¸äºåç § my $hash1 = {}; my $hash2 = {}; $hash1->{hash2} = $hash2; $hash2->{hash1} = $hash1; # ä¸æ¹ãå¼±ãåç §ã«ãã weaken $hash1->{hash2};
ããã®ããã«ããã¨ã$hash1ã$hash2ã®åç §ã0ã«ãªã£ãæç¹ã§ãã¡ã¢ãªã¯è§£æ¾ããã¾ãã
ããã®ããã«Perlã§ã¯ãã¬ã¼ããã¸ã³ã¬ã¯ã·ã§ã³ã®å®è£ ã¯ãåç §ã«ã¦ã³ãæ¹å¼ã§ãããã©ã¼ãã³ã¹ä¸ã®åé¡ã«æ©ã¾ããããã¨ããã»ã¨ãã©ãªã代ããã«ãç¸äºåç §ã«ã¤ãã¦ãèªåã§æèããªããã°ãªããªãã¨ãããã¨ããè¦ãã¦ããã¾ãããã