{"id":16466,"date":"2024-10-23T12:10:46","date_gmt":"2024-10-23T04:10:46","guid":{"rendered":"https:\/\/92it.top\/?p=16466"},"modified":"2024-10-23T12:10:46","modified_gmt":"2024-10-23T04:10:46","slug":"%e4%bb%80%e4%b9%88%e6%98%af%e5%86%85%e5%ad%98%e6%ba%a2%e5%87%ba%e5%92%8c%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%ef%bc%9f%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16466","title":{"rendered":"\u4ec0\u4e48\u662f\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\uff1f\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247500065&amp;idx=1&amp;sn=0da1bf0825b5764a2eca8a422a1cd98a&amp;chksm=96c4ed1da1b3640bbad0bdee05357678d2517363733fc0b72834805b86d32e91b3001b2b42ea&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u4ea4\u884c2\u9762\uff1a\u4ec0\u4e48\u662f\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\uff1f\u5982\u4f55\u89e3\u51b3\uff1f<\/a><\/p>\n\n\n\n<p>\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\u662f\u6211\u4eec\u7ecf\u5e38\u542c\u5230\u7684\u4e24\u79cd\u5185\u5b58\u7ba1\u7406\u95ee\u9898\uff0c\u90a3\u4e48\uff0c\u5b83\u4eec\u662f\u5982\u4f55\u5bfc\u81f4\u7684\uff1f\u53c8\u8be5\u5982\u4f55\u89e3\u51b3\uff1f\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u6765\u804a\u4e00\u804a\u3002<\/p>\n\n\n\n<p><strong>\u5185\u5b58\u6ea2\u51fa<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5185\u5b58\u6ea2\u51fa\uff08OutOfMemoryError\uff09\u662f\u6307\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u5c1d\u8bd5\u5206\u914d\u5185\u5b58\uff0c\u4f46\u7531\u4e8e\u6ca1\u6709\u8db3\u591f\u7684\u5185\u5b58\u53ef\u7528\uff0cJava \u865a\u62df\u673a\uff08JVM\uff09\u629b\u51fa\u4e86 <code>OutOfMemoryError<\/code> \u9519\u8bef\u3002\u5e38\u89c1\u7684\u5185\u5b58\u6ea2\u51fa\u533a\u57df\u5305\u62ec\u5806\u5185\u5b58\u548c\u6c38\u4e45\u4ee3\uff08\u5728 Java 8 \u4e4b\u540e\u88ab\u5143\u7a7a\u95f4\u53d6\u4ee3\uff09\u3002<\/p>\n\n\n\n<p><strong>\u5bfc\u81f4\u7684\u539f\u56e0<\/strong><\/p>\n\n\n\n<p>\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff1a1. 2. <strong>\u5806\u5185\u5b58\u6ea2\u51fa<\/strong>\uff1a\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\uff0c\u5bfc\u81f4\u5806\u5185\u5b58\u8017\u5c3d\u30022. <strong>\u6808\u5185\u5b58\u6ea2\u51fa<\/strong>\uff1a\u9012\u5f52\u8c03\u7528\u8fc7\u6df1\uff0c\u5bfc\u81f4\u6808\u5185\u5b58\u8017\u5c3d\u30023. <strong>\u6c38\u4e45\u4ee3\/\u5143\u7a7a\u95f4\u6ea2\u51fa<\/strong>\uff1a\u7c7b\u52a0\u8f7d\u8fc7\u591a\uff0c\u5bfc\u81f4\u6c38\u4e45\u4ee3\/\u5143\u7a7a\u95f4\u8017\u5c3d\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u7528\u4e09\u4e2a\u793a\u4f8b\uff0c\u5206\u522b\u5c55\u793a\u4e86\u5806\u5185\u5b58\u6ea2\u51fa\u3001\u6808\u5185\u5b58\u6ea2\u51fa\u548c\u6c38\u4e45\u4ee3\/\u5143\u7a7a\u95f4\u6ea2\u51fa\u7684\u60c5\u51b5\uff1a<\/p>\n\n\n\n<p><strong>\u5806\u5185\u5b58\u6ea2\u51fa<\/strong><\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\u4ee3\u7801\uff0c\u901a\u8fc7\u4e0d\u65ad\u5411 <code>ArrayList<\/code> \u6dfb\u52a0\u5bf9\u8c61\u6765\u8017\u5c3d\u5806\u5185\u5b58\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.ArrayList;\nimport java.util.List;\n\npublic class HeapMemoryOverflow {\n    public static void main(String[] args) {\n        List&lt;Object> list = new ArrayList&lt;>();\n        while (true) {\n            list.add(new Object());\n        }\n    }\n}\n\n<\/pre>\n\n\n\n<p>\u5728\u8fd0\u884c\u4e0a\u8ff0 <code>HeapMemoryOverflow<\/code> \u793a\u4f8b\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u8c03\u6574 JVM \u53c2\u6570\u4ee5\u8f83\u5c0f\u7684\u5806\u5927\u5c0f\u8fd0\u884c\uff0c\u4f8b\u5982 <code>-Xmx10m<\/code>\uff0c\u4ee5\u66f4\u5feb\u5730\u89c2\u5bdf\u5230 <code>OutOfMemoryError<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u6808\u5185\u5b58\u6ea2\u51fa<\/strong><\/p>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\u4ee3\u7801\uff0c\u901a\u8fc7\u9012\u5f52\u8c03\u7528\u4e00\u4e2a\u6ca1\u6709\u7ec8\u6b62\u6761\u4ef6\u7684\u65b9\u6cd5\uff0c\u5bfc\u81f4\u6808\u5185\u5b58\u6ea2\u51fa\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class StackMemoryOverflow {\n    public static void main(String[] args) {\n        recursiveMethod();\n    }\n\n    public static void recursiveMethod() {\n        \/\/ \u6ca1\u6709\u7ec8\u6b62\u6761\u4ef6\u7684\u9012\u5f52\u8c03\u7528\n        recursiveMethod();\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c<code>StackOverflowError<\/code>\u4ee3\u7801\uff0c\u901a\u5e38\u4f1a\u5f88\u5feb\u53d1\u751f\u6808\u5185\u5b58\u6ea2\u51fa\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u7684\u6808\u5927\u5c0f\u4e0d\u5927\u3002<\/p>\n\n\n\n<p><strong>\u6c38\u4e45\u4ee3\/\u5143\u7a7a\u95f4\u6ea2\u51fa<\/strong><\/p>\n\n\n\n<p>\u5728 Java 8 \u4e4b\u524d\uff0c\u6c38\u4e45\u4ee3\u6ea2\u51fa\u53ef\u4ee5\u901a\u8fc7\u52a8\u6001\u751f\u6210\u5927\u91cf\u7c7b\u6765\u6a21\u62df\uff0cJava 8 \u4e4b\u540e\uff0c\u6c38\u4e45\u4ee3\u88ab\u5143\u7a7a\u95f4\u53d6\u4ee3\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 CGLIB \u52a8\u6001\u751f\u6210\u7c7b\u7684\u793a\u4f8b\uff0c\u53ef\u80fd\u5bfc\u81f4\u5143\u7a7a\u95f4\u6ea2\u51fa\uff0c\u9700\u8981\u6dfb\u52a0 CGLIB \u5e93\u4f9d\u8d56\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import net.sf.cglib.proxy.Enhancer;\nimport net.sf.cglib.proxy.MethodInterceptor;\nimport net.sf.cglib.proxy.MethodProxy;\n\nimport java.lang.reflect.Method;\n\npublic class MetaspaceOverflow {\n    public static void main(String[] args) {\n        while (true) {\n            Enhancer enhancer = new Enhancer();\n            enhancer.setSuperclass(DummyClass.class);\n            enhancer.setUseCache(false);\n            enhancer.setCallback(new MethodInterceptor() {\n                @Override\n                public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {\n                    return proxy.invokeSuper(obj, args);\n                }\n            });\n            enhancer.create();\n        }\n    }\n\n    static class DummyClass {\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c <code>MetaspaceOverflow<\/code> \u793a\u4f8b\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 JVM \u53c2\u6570 <code>-XX:MaxMetaspaceSize=10m<\/code> \u6765\u9650\u5236\u5143\u7a7a\u95f4\u5927\u5c0f\uff0c\u4ee5\u66f4\u5feb\u5730\u89c2\u5bdf\u5230\u6ea2\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u53ea\u662f\u7ed9\u4e86\u4e00\u4e2a\u5927\u7684\u601d\u8def\uff0c\u5173\u4e8e\u5185\u5b58\u6ea2\u51fa\u7684\u6392\u67e5\u5de5\u4f5c\u4e5f\u662f\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u53bb\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<ol>\n<li><strong>\u589e\u52a0\u5185\u5b58<\/strong>\uff1a\u8c03\u6574 JVM \u53c2\u6570\u589e\u52a0\u5806\u5185\u5b58\u5927\u5c0f\uff0c\u5982 <code>-Xmx<\/code>\u3002<\/li>\n\n\n\n<li><strong>\u4f18\u5316\u4ee3\u7801<\/strong>\uff1a\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5bf9\u8c61\u521b\u5efa\uff0c\u4f18\u5316\u6570\u636e\u7ed3\u6784\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u67e5\u9012\u5f52<\/strong>\uff1a\u907f\u514d\u8fc7\u6df1\u7684\u9012\u5f52\u8c03\u7528\u3002<\/li>\n\n\n\n<li><strong>\u76d1\u63a7\u548c\u5206\u6790<\/strong>\uff1a\u4f7f\u7528\u5de5\u5177\u5982 JVisualVM\u3001JProfiler \u5206\u6790\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5185\u5b58\u6cc4\u6f0f\uff08Memory Leak\uff09\u662f\u6307\u7a0b\u5e8f\u4e2d\u5b58\u5728\u4e00\u4e9b\u5bf9\u8c61\uff0c\u5b83\u4eec\u4e0d\u518d\u88ab\u4f7f\u7528\uff0c\u4f46\u7531\u4e8e\u4ecd\u7136\u88ab\u5f15\u7528\uff0c\u5783\u573e\u56de\u6536\u5668\u65e0\u6cd5\u56de\u6536\u8fd9\u4e9b\u5bf9\u8c61\u3002\u56e0\u6b64\uff0c\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u5185\u5b58\u6cc4\u6f0f\u4f1a\u5bfc\u81f4\u53ef\u7528\u5185\u5b58\u9010\u6e10\u51cf\u5c11\uff0c\u6700\u7ec8\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa\u3002<\/p>\n\n\n\n<p><strong>\u5bfc\u81f4\u7684\u539f\u56e0<\/strong><\/p>\n\n\n\n<p>\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u539f\u56e0\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u9759\u6001\u96c6\u5408\u7c7b<\/strong>\uff1a\u4f7f\u7528 <code>static<\/code> \u4fee\u9970\u7684\u96c6\u5408\u7c7b\u6301\u6709\u5bf9\u8c61\u5f15\u7528\uff0c\u56e0\u4e3a\u9759\u6001\u96c6\u5408\u7684\u751f\u547d\u5468\u671f\u548c JVM \u4e00\u81f4\uff0c\u6240\u4ee5\u9759\u6001\u96c6\u5408\u5f15\u7528\u7684\u5bf9\u8c61\u4e0d\u80fd\u88ab\u91ca\u653e\u3002<\/li>\n\n\n\n<li><strong>\u76d1\u542c\u5668\u548c\u56de\u8c03<\/strong>\uff1a\u6ce8\u518c\u7684\u76d1\u542c\u5668\u6216\u56de\u8c03\u672a\u88ab\u79fb\u9664\u3002<\/li>\n\n\n\n<li><strong>\u957f\u751f\u547d\u5468\u671f\u5bf9\u8c61\u6301\u6709\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61<\/strong>\uff1a\u957f\u751f\u547d\u5468\u671f\u5bf9\u8c61\u4e0d\u5f53\u6301\u6709\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61\u7684\u5f15\u7528\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u6211\u4eec\u7528\u4e09\u4e2a\u793a\u4f8b\uff0c\u5206\u522b\u5c55\u793a\u4e86\u5185\u5b58\u6cc4\u6f0f\u53ef\u80fd\u53d1\u751f\u7684\u573a\u666f\uff1a<\/p>\n\n\n\n<p><strong>\u9759\u6001\u96c6\u5408\u7c7b\u5bfc\u81f4\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n\n\n\n<p>\u9759\u6001\u96c6\u5408\u7c7b\u6301\u6709\u5bf9\u8c61\u5f15\u7528\uff0c\u5bfc\u81f4\u8fd9\u4e9b\u5bf9\u8c61\u65e0\u6cd5\u88ab\u5783\u573e\u56de\u6536\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class StaticCollectionLeak {\n    \/\/ \u9759\u6001\u96c6\u5408\u6301\u6709\u5bf9\u8c61\u5f15\u7528\n    private static List&lt;Object> objectList = new ArrayList&lt;>();\n\n    public static void main(String[] args) {\n        for (int i = 0; i &lt; 10000; i++) {\n            \/\/ \u6bcf\u6b21\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u5e76\u6dfb\u52a0\u5230\u9759\u6001\u96c6\u5408\u4e2d\n            objectList.add(new Object());\n        }\n        \/\/ \u5373\u4f7f\u5728\u8fd9\u91cc\u8bd5\u56fe\u6e05\u7406\u6389\u4e00\u4e9b\u5176\u4ed6\u7684\u5f15\u7528\n        System.gc();  \/\/ \u8fd9\u4e9b\u5bf9\u8c61\u4ecd\u7136\u65e0\u6cd5\u88ab\u56de\u6536\uff0c\u56e0\u4e3a\u5b83\u4eec\u88ab\u9759\u6001\u96c6\u5408\u5f15\u7528\n    }\n}<\/pre>\n\n\n\n<p><strong>\u76d1\u542c\u5668\u548c\u56de\u8c03\u672a\u88ab\u79fb\u9664<\/strong><\/p>\n\n\n\n<p>\u6ce8\u518c\u7684\u76d1\u542c\u5668\u6216\u56de\u8c03\u672a\u88ab\u79fb\u9664\uff0c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.ArrayList;\nimport java.util.List;\n\npublic class ListenerLeak {\n    private List&lt;EventListener> listeners = new ArrayList&lt;>();\n\n    public void addListener(EventListener listener) {\n        listeners.add(listener);\n    }\n\n    public void triggerEvent() {\n        for (EventListener listener : listeners) {\n            listener.onEvent();\n        }\n    }\n\n    public static void main(String[] args) {\n        ListenerLeak leakExample = new ListenerLeak();\n        \n        \/\/ \u533f\u540d\u7c7b\u521b\u5efa\u7684\u76d1\u542c\u5668\u5bf9\u8c61\n        leakExample.addListener(new EventListener() {\n            @Override\n            public void onEvent() {\n                System.out.println(\"Event triggered\");\n            }\n        });\n\n        \/\/ \u5047\u8bbe\u5728\u67d0\u4e2a\u65f6\u5019\u4e0d\u518d\u9700\u8981\u76d1\u542c\u5668\uff0c\u4f46\u672a\u79fb\u9664\n        \/\/ listeners.remove(listener); \/\/ \u5e94\u8be5\u79fb\u9664\u4e0d\u9700\u8981\u7684\u76d1\u542c\u5668\n    }\n}\n\ninterface EventListener {\n    void onEvent();\n}<\/pre>\n\n\n\n<p><strong>\u957f\u751f\u547d\u5468\u671f\u5bf9\u8c61\u6301\u6709\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61<\/strong><\/p>\n\n\n\n<p>\u957f\u751f\u547d\u5468\u671f\u5bf9\u8c61\u4e0d\u5f53\u6301\u6709\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5bfc\u81f4\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61\u65e0\u6cd5\u88ab\u56de\u6536\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.HashMap;\nimport java.util.Map;\n\npublic class LongLifeCycleLeak {\n    private static Map&lt;String, byte[]> cache = new HashMap&lt;>();\n\n    public static void main(String[] args) {\n        while (true) {\n            \/\/ \u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61\n            byte[] data = new byte[1024 * 1024]; \/\/ 1MB\n\n            \/\/ \u957f\u751f\u547d\u5468\u671f\u5bf9\u8c61\u6301\u6709\u77ed\u751f\u547d\u5468\u671f\u5bf9\u8c61\u7684\u5f15\u7528\n            cache.put(String.valueOf(System.nanoTime()), data);\n\n            \/\/ \u9700\u8981\u5b9a\u671f\u79fb\u9664\u4e0d\u518d\u9700\u8981\u7684\u6570\u636e\uff0c\u5426\u5219\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\n            \/\/ cache.clear(); \/\/ \u5e94\u8be5\u5728\u9002\u5f53\u65f6\u673a\u6e05\u7406\u7f13\u5b58\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u53ea\u662f\u7ed9\u4e86\u4e00\u4e2a\u5927\u7684\u601d\u8def\uff0c\u5173\u4e8e\u5185\u5b58\u6cc4\u6f0f\u7684\u6392\u67e5\u5de5\u4f5c\u4e5f\u662f\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6587\u7ae0\u4e2d\u53bb\u8be6\u7ec6\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<ol>\n<li><strong>\u53ca\u65f6\u91ca\u653e\u5f15\u7528<\/strong>\uff1a\u786e\u4fdd\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u5f15\u7528\u88ab\u6e05\u9664\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528\u5f31\u5f15\u7528<\/strong>\uff1a\u5bf9\u7f13\u5b58\u6216\u975e\u5173\u952e\u5bf9\u8c61\u4f7f\u7528 <code>WeakReference<\/code>\u3002\u6bd4\u5982 ThreadLocal \u7684\u5f31\u5f15\u7528\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\uff0c\u56e0\u6b64\u4f7f\u7528\u5b8c ThreadLocal \u4e00\u5b9a\u8981\u8bb0\u5f97\u4f7f\u7528 remove \u65b9\u6cd5\u6765\u8fdb\u884c\u6e05\u9664\u3002<\/li>\n\n\n\n<li><strong>\u6b63\u786e\u7ba1\u7406\u751f\u547d\u5468\u671f<\/strong>\uff1a\u7279\u522b\u662f\u76d1\u542c\u5668\u548c\u56de\u8c03\uff0c\u786e\u4fdd\u5728\u4e0d\u9700\u8981\u65f6\u79fb\u9664\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n\n\n\n<p>\u4e0b\u9762\u793a\u4f8b\u4ee3\u7801\uff0c\u7528\u4e8e\u6d4b\u8bd5\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import java.util.HashMap;\nimport java.util.Map;\n\npublic class MemoryLeakExample {\n    private static Map&lt;Integer, String> map = new HashMap&lt;>();\n\n    public static void main(String[] args) {\n        for (int i = 0; i &lt; 100000; i++) {\n            map.put(i, \"value\" + i);\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5982\u679c <code>map<\/code> \u662f\u4e00\u4e2a\u957f\u671f\u5b58\u5728\u7684\u9759\u6001\u53d8\u91cf\uff0c\u5e76\u4e14\u6ca1\u6709\u53ca\u65f6\u6e05\u7406\uff0c\u5219\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n\n\n\n<p><strong>\u5bf9\u6bd4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5173\u4e8e\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\u7684\u6bd4\u8f83\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u89e6\u53d1\u65f6\u673a<\/strong>\uff1a\u5185\u5b58\u6ea2\u51fa\u901a\u5e38\u5728\u5185\u5b58\u8017\u5c3d\u65f6\u7acb\u5373\u89e6\u53d1\uff0c\u800c\u5185\u5b58\u6cc4\u6f0f\u53ef\u80fd\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u9010\u6e10\u663e\u73b0\u3002<\/li>\n\n\n\n<li><strong>\u5f71\u54cd\u8303\u56f4<\/strong>\uff1a\u5185\u5b58\u6ea2\u51fa\u4f1a\u7acb\u5373\u5f71\u54cd\u7a0b\u5e8f\u7684\u53ef\u7528\u6027\uff0c\u800c\u5185\u5b58\u6cc4\u6f0f\u901a\u5e38\u662f\u4e00\u4e2a\u9010\u6b65\u79ef\u7d2f\u7684\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u68c0\u6d4b\u96be\u5ea6<\/strong>\uff1a\u5185\u5b58\u6ea2\u51fa\u8f83\u5bb9\u6613\u68c0\u6d4b\uff0c\u800c\u5185\u5b58\u6cc4\u6f0f\u5f80\u5f80\u9700\u8981\u6df1\u5165\u5206\u6790\u548c\u8c03\u8bd5\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u51b3\u590d\u6742\u5ea6<\/strong>\uff1a\u5185\u5b58\u6ea2\u51fa\u7684\u89e3\u51b3\u76f8\u5bf9\u7b80\u5355\uff0c\u901a\u5e38\u901a\u8fc7\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u6216\u589e\u52a0\u5185\u5b58\u5373\u53ef\u3002\u800c\u5185\u5b58\u6cc4\u6f0f\u7684\u89e3\u51b3\u9700\u8981\u8bc6\u522b\u5e76\u6e05\u7406\u4e0d\u5fc5\u8981\u7684\u5f15\u7528\uff0c\u53ef\u80fd\u6d89\u53ca\u66f4\u590d\u6742\u7684\u4ee3\u7801\u91cd\u6784\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u5206\u6790\u4e86Java\u7684\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\u5e76\u4e14\u5e94\u793a\u4f8b\u5c55\u793a\u4e86\u5b83\u4eec\u5bfc\u81f4\u7684\u539f\u56e0\uff0c\u5e94\u8be5\u8bf4\u5b83\u4eec\u662f\u6bd4\u8f83\u5e38\u89c1\u7684\u5185\u5b58\u7ba1\u7406\u95ee\u9898\uff0c\u5982\u679c\u5728\u751f\u4ea7\u73af\u5883\u51fa\u73b0\u4e5f\u662f\u6bd4\u8f83\u5934\u75bc\u7684\u95ee\u9898\u3002\u6240\u4ee5\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4e00\u5b9a\u8981\u6ce8\u610f\u81ea\u5df1\u7684\u4ee3\u7801\u98ce\u683c\u548c\u4ee3\u7801\u8d28\u91cf\uff0c\u5c3d\u91cf\u907f\u514d\u8fd9\u4e9b\u95ee\u9898\u7684\u53d1\u751f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u4ea4\u884c2\u9762\uff1a\u4ec0\u4e48\u662f\u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\uff1f\u5982\u4f55\u89e3\u51b3\uff1f \u5185\u5b58\u6ea2\u51fa\u548c\u5185\u5b58\u6cc4\u6f0f\u662f\u6211\u4eec\u7ecf\u5e38\u542c\u5230\u7684\u4e24\u79cd\u5185\u5b58\u7ba1\u7406\u95ee\u9898\uff0c\u90a3\u4e48 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16466"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16466"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16466\/revisions"}],"predecessor-version":[{"id":16467,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16466\/revisions\/16467"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}