3

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

assylias
  • 321,522
  • 82
  • 660
  • 783
smur89
  • 329
  • 1
  • 3
  • 15
  • a Thread object is quite heavy and contains many fields, including a `char[]`. You should not create that many threads and use a thread pool instead. – assylias Jul 23 '13 at 11:38
  • The actual program I'm running doesnt use this many threads, I was testing something else and discovered this char[] issue. The implementation I'm using is here: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=chameneosredux&lang=java&lang2=java&data=u32 – smur89 Jul 23 '13 at 12:29
  • There's a large number of `char[]` (roughly 2 per String) but not a large volume. – Mike Dunlavey Jul 23 '13 at 13:57
  • I guess I should also be asking where these Strings are coming from? – smur89 Jul 23 '13 at 14:00

1 Answers1

1

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.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
  • Thank you. I didn't know how you could drill down further to see this information. Exactly what I was looking for. Although, I'm getting different results to this. – smur89 Jul 24 '13 at 10:29