0

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
Jens
  • 1
  • 1
  • EDIT: I think those topics are related: http://stackoverflow.com/questions/17145228/track-down-allocations-of-int and http://stackoverflow.com/questions/7304665/objects-with-no-references-in-hprof – Jens Jan 22 '16 at 18:49

1 Answers1

0

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.

Borislav Stoilov
  • 3,247
  • 2
  • 21
  • 46
  • Thanks for your reply. I think I expressed the heap sizes wrong. Before all the threads start: Available heap: 14GB, Used heap:460MB After all threads have started (at Thread.sleep(3000000)): Ava. heap: 14GB, Usedheap:3GB. I already used visualVM (and eclipse MAT, as well as JProfiler). As mentioned they all show me the big int[] and char[] arrays. I think these arrays are from the JVM, because no object I created has a reference to them. When I parse the heapdumps via parseHeapDump.sh -keep_unreachable_objects (eclipse MAT), then I can see them. And yes this is the whole code – Jens Jan 22 '16 at 18:47