简体   繁体   中英

Huge memory consumption of threads

I have a very basic programm which is shown below

Main-Method:

Thread.sleep(10000);

MyThread[] threads=new MyThread[16];

for(int i=0;i<16;i++){
    threads[i]=new MyThread();  
}

for(int i=0;i<16;i++){
    threads[i].start(); 
}

for(int i=0;i<16;i++){
    threads[i].join();  
}






Threadclass:

public class MyThread extends Thread{
byte[][] queue = new byte[125][];

public void run() {

    for(int i=0;i<125;i++){

    byte[] tempbyte=new byte[20];
    for(int i1=0;i1<20;i1++){
        tempbyte[i1]=(byte) 255;
    }
    queue[i]=tempbyte;
    }

    try {
        Thread.sleep(3000000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

So basicly each thread creates 125 arrays of 20-bytes. With 16 threads this should be 40.000 bytes (16*125*20). Now comes the problem: When I start this programm with the following VM-arguments:

-Xms14000m -XX:NewSize=10000m

and run

jcstat -gc #PID

then it shows around 460 mb for eden (EU). When the main-thread has started and I do jstat again, then it shows around 3000 mb!! Whats the reason for the big heap? I already did a heapdump and the source of heap-waste are a lot of int[] and char[] arrays. They are unreferenced, so i can not track them.

the size of the heap is determined by the jvm, it tracks allocated memory and allocation frequency (and many more things) and uses some complex methods to calculate it.The passed arguments are treated as suggestion not like configurations, so the jvm might chose to ignore -Xms14000m -XX:NewSize=10000m . You can't expect the heap size to be exactly a certain number made of your variables only.

I don't see "memory leak" in the code you provided, is this the whole thing?

Also I suggest you use Java Visual VM to track the memory consumption, its located in the jdk bin folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM