释放线程时,Java会从堆中泄漏内存

时间:2015-10-27 13:23:09

标签: java memory-leaks

我有一个小应用程序,它分配了大约25000个线程,然后释放它们。 当线程被释放时,即使在所有线程都退出之后,应用程序的内存消耗也会上升并保持高水平。

顶部看起来像这样:

 PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
9133 root      20   0 22.601g 8.612g  12080 S   0.0  9.1   1:18.61 java

虽然 jmap -heap 如下所示:

Attaching to process ID 9133, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.66-b17

using thread-local object allocation.
Parallel GC with 18 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 1073741824 (1024.0MB)
   NewSize                  = 104857600 (100.0MB)
   MaxNewSize               = 104857600 (100.0MB)
   OldSize                  = 968884224 (924.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 1073741824 (1024.0MB)
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 78643200 (75.0MB)
   used     = 1572864 (1.5MB)
   free     = 77070336 (73.5MB)
   2.0% used
From Space:
   capacity = 13107200 (12.5MB)
   used     = 0 (0.0MB)
   free     = 13107200 (12.5MB)
   0.0% used
To Space:
   capacity = 13107200 (12.5MB)
   used     = 0 (0.0MB)
   free     = 13107200 (12.5MB)
   0.0% used
PS Old Generation
   capacity = 968884224 (924.0MB)
   used     = 1264416 (1.205841064453125MB)
   free     = 967619808 (922.7941589355469MB)
   0.13050227970271916% used

808 interned Strings occupying 54648 bytes.

据我所知,jmap报告中没有任何内容可以解释top报告的8.612g。

Java版本是oracle 1.8.0_66

该应用程序在Red Hat Enterprise Linux Server 7.1(Maipo)上运行。

该应用程序的代码如下:

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class WaitTest {
    static AtomicInteger ai = new AtomicInteger(0);

    public static void main(String[] args) {
        List<WaitingThread> threads = new LinkedList<>();
        while (true) {
            System.out.println("number of threads: " + ai.get());
            String s = System.console().readLine();
            if (s == null)
                System.exit(0);
            s = s.trim();
            if (s.isEmpty())
                continue;
            char command = s.charAt(0);
            if (command != '+' && command != '-') {
                System.out.println("+ or - please");
                continue;
            }
            String num = s.substring(1);
            int iNum;
            try {
                iNum = Integer.parseInt(num.trim());
            } catch (Exception ex) {
                System.out.println("valid number please");
                continue;
            }
            if (command == '+') {
                for (int i = 0; i < iNum; i++) {
                    WaitingThread t = new WaitingThread();
                    t.start();
                    threads.add(t);
                }
            }
            if (command == '-') {
                Set<WaitingThread> threadsToJoin = new HashSet<>();
                for (Iterator<WaitingThread> it = threads.iterator(); it.hasNext(); ) {
                    if (iNum > 0) {
                        WaitingThread t = it.next();
                        threadsToJoin.add(t);

                        synchronized (t.lock) {
                            t.lock.notify();
                        }

                        it.remove();
                        iNum--;
                    } else
                        break;

                }
                for (WaitingThread t : threadsToJoin)
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
            System.gc();
        }
    }

    static class WaitingThread extends Thread {
        public final Object lock = new Object();

        @Override
        public void run() {
            ai.incrementAndGet();
            try {
                deepStack(200);
            } catch (InterruptedException ex) {
            } catch (Exception ex) {
                System.out.println("exception in thread " + Thread.currentThread().getName() + ", " + ex.getMessage());
            } finally {
                ai.decrementAndGet();
            }
        }

        private void deepStack(int depth) throws InterruptedException {
            if (depth == 0) {
                synchronized (lock) {
                    lock.wait();
                }

            } else
                deepStack(depth - 1);
        }
    }

}

Here is a link to pmap output:

Here is a link to jmap -dump (hprof)

3 个答案:

答案 0 :(得分:0)

在列表变量cv::Point2d pp(u0, v0); cv::Mat R, t, mask; cv::Mat E = cv::findEssentialMat(points1, points2, focal, pp, cv::RANSAC, 0.999, 1.0, mask); cv::recoverPose(E, points1, points2, R, t); 中的命令“+”期间是否首先创建所有线程,这些命令永远不会超出范围?这意味着链表持有对所有线程的引用,并且在'threads'退出之前不会进行垃圾回收。此外,它是possible that the LinkedList itself is leaking memory。即使这不是问题,您也应该考虑using an ArrayList instead of a LinkedList

此外,这是另一个问题finding memory leaks in Java

答案 1 :(得分:0)

应用程序内存仅与堆大小松散耦合。当GC运行并清理时,它会清理堆,而不是系统内存。实际上,JVM甚至可能不会进行系统调用以减少系统内存,即使它经常释放堆空间。

答案 2 :(得分:0)

每个线程都需要一个堆栈(已分配的内存),它不是java堆的一部分。

堆栈的默认大小取决于jvm版本,操作系统等。

您可以使用-Xss jvm参数进行调整。

25000个帖子可以很容易地解释你的8.6g

根据Amir评论进行更新:

不是100%肯定但是看一下tour pmap日志,你可能在glibc

中遇到了竞技场分配器的问题

查看所有64Mb分配

检查您的glibc版本是否&gt; 2.10

尝试使用环境变量MALLOC_ARENA_MAX = 2设置运行程序。您可以使用实际值。