Question

I have profiled a simple program as below and found a large number of char[] instances to be created. Having read here that char[] instances are usually attributed to Strings, I cant see how this is the case for this program. I know there is a char[] 'name' variable in the Thread class, but surely this should only create 10,000 of these, and so I'm wondering where the additional 35,000 threads have come from?

public class untitled {
    public static void main(String args[]){
        ArrayList<Thread> a = new ArrayList<Thread>();
        for(int i = 0; i < 10000; i++){
            Thread t1 = new Thread();
            a.add(t1);
            t1.start();
        }
    }
}

Here's a screenshot of the profiling memory results. There appears to be a umber of String[] instances also.

enter image description here

Im using this program to isolate some other sections of the profiler. In the full program, the char[] instances increase to ~335,000

Was it helpful?

Solution

You have to use the heap walker in order to answer this question. Select the char[] class in the classes view of the heap walker and create a new object set. Then go to the references view and select the "Cumulated incoming references" from the view selector.

enter image description here

The answer is that roughly a third of the char[] objects belong to strings that are allocated in the JVM by various classes and two thirds come from thread name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top