【问题标题】:How to find memory leaks in my Java application如何在我的 Java 应用程序中查找内存泄漏
【发布时间】:2019-08-21 00:37:56
【问题描述】:

这是我之前的问题HERE 的后续问题。我目睹了我的 Java 应用程序中的内存泄漏。最初,我认为泄漏来自我的应用程序的服务器组件。但是按照别人的建议,没有。

我使用了一个工具来转储堆内存并使用JProfiler 将其可视化。显然是因为我怀疑HashMaps。但我不确定,因为我不熟悉如何解释转储。

这是我的应用程序结构的简要 sn-p(它每 15 分钟缓存一些文本数据,以便快速检索服务器线程)。

导致泄漏问题的原因是什么?以及如何从下面的转储中识别它? 显然我做new Object()HashMap.put() 的方式有一些泄漏问题?!

第一个入门课程/主课程。这里我启动了 7 个主要的 HashMaps,每个都将一个键(现在只有一个 - 最终将有 16 个键)映射到大约 4000 个单行 JSON 字符串的时间序列 NavigableMap

public class MyCache {
        static HashMap <String, NavigableMap <Long, String>> map1= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map2= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map3= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map4= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map5= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map6= new HashMap <String, NavigableMap <Long, String>> ();
        static HashMap <String, NavigableMap <Long, String>> map7= new HashMap <String, NavigableMap <Long, String>> ();

        public static void main(String[] args) throws Exception {
    new Server();
    new Aggregation();
    }
}

然后在Aggregation() 中,我从HTTP 资源中获取一些文本,将它们转换为JSON 字符串,并将它们缓存在一些临时的NavigableMaps 中,然后将它们放入主HashMap 中(所以刷新不会对服务器影响很大)。

public class Aggregation {
    static NavigableMap <Long, String> map1Temp= new ConcurrentSkipListMap <Long, String> ();;
    static NavigableMap <Long, String> map2Temp = new ConcurrentSkipListMap <Long, String> ();
    static NavigableMap <Long, String> map3Temp= new ConcurrentSkipListMap <Long, String> ();
    static NavigableMap <Long, String> map4Temp= new ConcurrentSkipListMap <Long, String> ();
    static NavigableMap <Long, String> map5Temp = new ConcurrentSkipListMap <Long, String> ();
    static NavigableMap <Long, String> map6Temp = new ConcurrentSkipListMap <Long, String> ();
    static NavigableMap <Long, String> map7Temp = new ConcurrentSkipListMap <Long, String> ();

public Aggregation(){

// loop to cache last 15 mins
while (true) {
            logger.info("START REFRESHING ...");
    for (int i = 0; i < mylist.size(); i++) {
        long startepoch = getTime(mylist.get(i).time);
        MyItem m = mylist.get(i);
        String index=(i+1)+"";

        process1(index, m.name, startepoch);
        //adds to map1Temp
        process2(index, m.name, startepoch);
        //adds to map2Temp
        process3(index, m.name, startepoch);
        //adds to map3Temp
        process4(index, m.name, startepoch);
        //adds to map4Temp
        process5(index, m.name, startepoch);
        //adds to map5Temp
        process6(index, m.name, startepoch);
        //adds to map6Temp
        process7(index, m.name, startepoch);
        //adds to map7Temp
        }

    //then `put` them in the main `HashMap` all at-once:
            MyCache.map1.put(channel, new ConcurrentSkipListMap <Long, String> (map1Temp));
            MyCache.map2.put(channel, new ConcurrentSkipListMap <Long, String> (map2Temp));
            MyCache.map3.put(channel, new ConcurrentSkipListMap <Long, String>(map3Temp));
            MyCache.map4.put(channel, new ConcurrentSkipListMap <Long, String>(map4Temp));
            MyCache.map5.put(channel, new ConcurrentSkipListMap <Long, String> (map5Temp));
            MyCache.map6.put(channel, new ConcurrentSkipListMap <Long, String> (map6Temp));
            MyCache.map7.put(channel, new ConcurrentSkipListMap <Long, String> (map7Temp));

//printing the size of all Hashmap entries. They don't grow :-/
logger.info("\t"+"map1.size(): "+MyCache.map1.get(key).size());
logger.info("\t"+"map2.size(): "+MyCache.map2.get(key).size());
//and other 5...        

//then clear the temp maps so they don't grow over and over
            map1Temp.clear();
            map2Temp.clear();
            map3Temp.clear();
            map4Temp.clear();
            map5Temp.clear();
            map6Temp.clear();
            map7Temp.clear();
    }
//sleep for 15 min until next caching cycle
Thread.sleep(cacheEvery*1000*60);
}

【问题讨论】:

    标签: java memory-leaks heap-memory jprofiler jhat


    【解决方案1】:

    内存分析器告诉您,您有 3 个庞大的 HashMap 数据结构,占用大约 8GB 的​​ RAM ...包括它们引用的闭包键和值对象。看起来它们可能是地图的地图。

    这可能是您的内存泄漏的证据。您的应用程序正在向地图数据结构添加越来越多的条目,并且(可能)不会删除它们。这是内存泄漏的一种形式。

    (请注意,这是您在上一个问题中没有向我们展示的部分代码...)

    【讨论】:

    • 添加了我课程的所有 sn-ps。我想知道我的Hashmaps 不应该变大。你能看一下吗?
    • 显然,它们确实变大了。如果按照您的想法每 15 分钟清除一次地图,那么这意味着 ~8GB 是 15 分钟的数据。但无论哪种方式,您都应该假设分析器实际上告诉您这些映射的内存利用率的真相。
    • 您是如何找到 8GB 的​​?我没有看到任何线索!
    • 我记错了。不仅如此。 3,135 + 2,627 + 2,570 + 2,378 + 2,125 + ... 超过 10GB。现在我不是该 UI 的专家,可能会说地图之间存在一些共享。但它仍然在说“很多内存”。
    • 好的。我不知道其他1670 hashmap 项目是什么。我在每次迭代中创建它们。但我假设 GC 会清除那些,显然没有。
    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-01
    • 2019-08-20
    相关资源
    最近更新 更多