{"id":2296,"date":"2024-09-08T08:35:16","date_gmt":"2024-09-08T00:35:16","guid":{"rendered":"https:\/\/coderbee.net\/?p=2296"},"modified":"2024-09-08T08:40:16","modified_gmt":"2024-09-08T00:40:16","slug":"weakreference-%e4%bd%bf%e7%94%a8%e4%b8%8d%e5%bd%93%e5%af%bc%e8%87%b4oom%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20240908\/2296","title":{"rendered":"WeakReference \u4f7f\u7528\u4e0d\u5f53\u5bfc\u81f4OOM\uff1f"},"content":{"rendered":"<p>9\u67085\u30016\u53f7\u4e24\u5929\uff0c\u6709\u4e2a\u53e4\u8001\u7684\u5e94\u7528\u8fde\u7eed\u51fa\u73b0\u9891\u7e41\u7684 FullGC\uff0c\u6700\u7ec8OOM\u3002<br \/>\n\u8fd9\u4e2a\u53e4\u8001\u5e94\u7528\u7684\u6846\u67b6\u662f\u5341\u591a\u5e74\u524d\u57fa\u4e8e Spring \u9b54\u6539\u7684\uff0c\u5728\u90e8\u7f72\u4e0a\u5206\u4e3a web\u3001app \u4e24\u5c42\uff0cweb\u901a\u8fc7 EJB \u8fdb\u884c\u8c03\u7528app\u91cc\u7684\u670d\u52a1\u8bbf\u95ee\u6570\u636e\u5e93\u3002<\/p>\n<p>\u8fd9\u4e24\u5c42\u8bbf\u95ee Spring \u5bb9\u5668\u91cc\u7684 Bean \u4e00\u822c\u90fd\u662f\u901a\u8fc7\u7c7b\u4f3c\u4e0b\u9762\u7684\u5199\u6cd5\u6765\u83b7\u53d6\uff1a<br \/>\n<code><br \/>\nSomeService service = CustAppContextHolder.getContext(contextName).getBean(beanName);<br \/>\nservice.doSomething();<\/p>\n<p>\/\/ CustAppContextHolder.getContext \u7684\u5b9e\u73b0\u5927\u81f4\u5982\u4e0b\uff1a<br \/>\npublic class CustAppContextHolder {<br \/>\n    private static Map&lt;String, WeakReference<ApplicationContext>> caches = new ConcurrentHashMap&lt;String, WeakReference<ApplicationContext>>();<\/p>\n<pre><code>public static ApplicationContext getContext(String contextName) {\n    WeakReference&lt;ApplicationContext&gt; weakReference = caches.get(contextName);\n    ApplicationContext context;\n    if (weakReference != null &amp;&amp; (context = weakReference.get()) != null) {\n        return context;\n    } else {\n        synchronized (CustAppContextHolder.class) {\n            context = initContext(contextName);\n            caches.put(contextName, new WeakReference&lt;&gt;(context));\n        }\n    }\n    return context;\n}\n\nprivate static ApplicationContext initContext(String contextName) {\n    System.out.println(\"init context:\" + contextName);\n    return null;\n}\n<\/code><\/pre>\n<p>}<br \/>\n<\/code><br \/>\n<!--more--><\/p>\n<p>\u4ece\u5185\u5b58 dump \u5206\u6790\uff0cZookeeperRegistry.subscribed \u5bf9\u8c61\u5360\u7528\u4e86\u5185\u5b58\u5bfc\u81f4\u6ca1\u6cd5\u56de\u6536\uff0c\u540c\u4e00\u4e2a\u670d\u52a1\uff0c\u91cc\u9762\u597d\u51e0\u5343\u4e2a\u6d88\u8d39\u8005\u5b9e\u4f8b\u3002\u6bcf\u4e2a\u670d\u52a1\u5bf9\u5e94\u7684 URL \u91cc\u5e26\u6709\u4e00\u4e2a\u65f6\u95f4\u6233\uff0c\u5bfc\u81f4\u8fdb\u884c\u670d\u52a1\u8ba2\u9605\u65f6\u90fd\u8ba4\u4e3a\u662f\u4e0d\u540c\u7684\u670d\u52a1\u3002<br \/>\n\u9996\u5148\u627e\u8d1f\u8d23\u8be5 RPC \u6846\u67b6\u7684\u56e2\u961f\uff0c\u4ed6\u8bf4\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f <code>SomeService service = CustAppContextHolder.getContext(contextName).getBean(beanName);<\/code>  \u83b7\u53d6 RPC \u6d88\u8d39\u8005\u65f6\uff0c\u6bcf\u6b21\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u5b9e\u4f8b\u4f1a\u5728 ZK \u6ce8\u518c\u3002\u8fd9\u79cd\u5199\u6cd5\u5176\u5b9e\u4e00\u76f4\u662f\u8fd9\u6837\u7684\uff0c\u5341\u591a\u5e74\u4e86\uff0c\u4e2d\u95f4\u7ecf\u5386\u4e86\u4ece weblogic \u5207\u6362\u5230\u4fe1\u521b\u57fa\u4e8e apusic \u7684\u4e2d\u95f4\u4ef6\u3002<\/p>\n<p>\u7b2c\u4e00\u5929 5\u53f7\u6392\u67e5\u65f6\u4e3b\u8981\u56f4\u7ed5\u8fd9\u4e2a RPC \u8c03\u7528\u7684\u5730\u65b9\u8fdb\u884c\uff0c\u4f46\u4ece\u4ee3\u7801\u770b\uff0c\u5f88\u591a\u670d\u52a1\u90fd\u6ca1\u6709\u8fdb\u884c\u8c03\u7528\u4e86\uff0c\u4f46\u4ecd\u7136\u6709\u6d88\u8d39\u8005\u5b9e\u4f8b\u3002\u56e0\u6b64\u8bf4\u662f RPC \u8c03\u7528\u5bfc\u81f4\u662f\u8bf4\u4e0d\u901a\u7684\u3002<\/p>\n<p>\u665a\u4e0a\u56de\u6765\u53c8\u60f3\u4e86\u60f3\uff0c\u4e0d\u662f RPC \u88ab\u8c03\u7528\u4e86\uff0c\u800c\u5e94\u8be5\u662f\u5bb9\u5668\u88ab\u521d\u59cb\u5316\uff0c\u5bfc\u81f4\u521b\u5efa\u4e86 RPC \u6d88\u8d39\u8005\u5b9e\u4f8b\u3002<br \/>\n\u7b2c\u4e8c\u5929\u5230\u4e86\u516c\u53f8\uff0c\u67e5\u4e86\u4e0b\u65e5\u5fd7\uff0c5\u53f7\u90a3\u5929\uff0c\u6709\u63a5\u8fd11\u4e07\u6b21\u7684 &#8220;init context:&#8221; \u8f93\u51fa\uff0c\u4e5f\u5c31\u662f\u8bf4\u5e94\u7528\u5bb9\u5668\u88ab\u521b\u5efa\u4e86\u8fd1\u4e0a\u4e07\u6b21\uff0c\u602a\u4e0d\u5f97\u90a3\u4e48\u591a\u6d88\u8d39\u8005\u5b9e\u4f8b\u3002\u57284\u53f7\u53d1\u7248\u4e4b\u524d\u7684\u65e5\u5fd7\u91cc\uff0c\u6bcf\u5929\u4e5f\u6709\u5c0f\u51e0\u767e\u6b21\u7684\u521d\u59cb\u5316\u65e5\u5fd7\u8f93\u51fa\uff0c\u4e0d\u81f3\u4e8e\u628a\u5185\u5b58\u5360\u7528\u592a\u591a\u3002<br \/>\n\u8f93\u51fa\u65e5\u5fd7\u7684\u7ebf\u7a0b\u90fd\u662f ORBWorker\uff0c\u540e\u6765\u54a8\u8be2\u8d1f\u8d23 apusic \u4e2d\u95f4\u4ef6\u7684\u56e2\u961f\uff0c\u8bf4\u8fd9\u4e2a\u662f apusic \u8d1f\u8d23\u5904\u7406 EJB \u8c03\u7528\u7684\u7ebf\u7a0b\u3002\u4ed6\u4eec\u8fd8\u6307\u51fa\u53ef\u80fd\u662f CustAppContextHolder \u4f7f\u7528 WeakReference \u5bfc\u81f4\u4e0a\u4e0b\u6587\u88ab\u56de\u6536\u540e\uff0c\u518d\u6b21\u6765\u83b7\u53d6\u4e0a\u4e0b\u6587\u65f6\u521b\u5efa\u65b0\u7684\uff0c\u8fd9\u4e2a\u8001\u6846\u67b6\u7684\u4e0b\u4e00\u4e2a\u5927\u7248\u672c\u5c31\u628a\u8fd9\u4e2a WeakReference \u79fb\u9664\u4e86\u3002<\/p>\n<p>\u8fd9\u4e2a\u95ee\u9898\u76ee\u524d\u8fd8\u6ca1\u7a33\u5b9a\u5730\u590d\u73b0\u51fa\u6765\uff0c\u751f\u4ea7\u4e0a\u4e5f\u662f\u4e2a\u522b\u5e94\u7528\u5b9e\u4f8b\u4f1a OOM\uff0c\u5982\u679c\u771f\u662f\u8fd9\u4e2a WeakReference \u5bfc\u81f4\u7684\uff0c\u53ef\u80fd\u7684\u4f18\u5316\u65b9\u6848\u6709\uff1a<br \/>\n1\u3001\u5728\u5e94\u7528\u4ee3\u7801\u91cc\u5b9a\u4e49\u4e00\u4e2a\u8ddf CustAppContextHolder \u5305\u548c\u7c7b\u540d\u5b8c\u5168\u4e00\u6837\u7684\u7c7b\uff0c\u628a WeakReference \u79fb\u9664\u6389\uff0c\u901a\u8fc7\u7c7b\u52a0\u8f7d\u4f18\u5148\u7ea7\u66ff\u6362\u6846\u67b6\u7684\u7c7b\u3002<br \/>\n2\u3001\u9488\u5bf9 WeakReference \u7684\u7279\u5b9a\uff0c\u5b83\u5f15\u7528\u7684\u5bf9\u8c61\u5728\u6ca1\u6709\u5f3a\u5f15\u7528\u65f6\u53ef\u4ee5\u88ab GC \u56de\u6536\uff0c\u5b9a\u4e49\u9759\u6001\u53d8\u91cf\u6765\u7ef4\u6301\u5f3a\u5f15\u7528\uff0c\u4fdd\u8bc1\u521d\u59cb\u5316\u7684\u4e0a\u4e0b\u6587\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u4ece\u800c\u53ef\u4ee5\u590d\u7528\uff0c\u51cf\u5c11 RPC \u6d88\u8d39\u8005\u7684\u5b9e\u4f8b\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>9\u67085\u30016\u53f7\u4e24\u5929\uff0c\u6709\u4e2a\u53e4\u8001\u7684\u5e94\u7528\u8fde\u7eed\u51fa\u73b0\u9891\u7e41\u7684 FullGC\uff0c\u6700\u7ec8OOM\u3002 \u8fd9\u4e2a &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20240908\/2296\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[231,364,365],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2296"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2296"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2296\/revisions"}],"predecessor-version":[{"id":2298,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2296\/revisions\/2298"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}