The difference between [C] and String in jmap output

clipboard.png
as shown in the figure, in the heap typed by jmap, [C and String appear at the same time.
it"s a little strange that String itself is saved in final char [], and it is listed here at the same time, which means that [the number of instances and memory footprint of C includes String, or is String counted separately?

Mar.04,2021

The actual memory occupied by

String should be String + [C ] in the figure above.

to verify this, I wrote Mini Program

import java.io.*;
import java.util.*;

public class MemoryMap {
    static long bytes = 0;

    public static void main(String[] args) throws IOException { // Line 1
        Set<String> opt = new HashSet<String>();
        opt.addAll(Arrays.asList(args));
        List<String> list= new ArrayList< String>();
        if (opt.contains("alloc")) {
            for (int i = 0; i < 100; iPP) {
                String s = foo(1024 * 1024, i); //100Mb
                bytes += s.getBytes().length;
                list.add(s);
            }
        }
        System.out.println("Bytes=" + bytes + ", press Enter to exit...");

        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        if(opt.contains("gc")){
            System.gc();
        }
        String a = br.readLine();
        System.out.println(a);
        System.out.println(list.size());
    }

    private static String foo(long count, int k) { // Line 6
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < count; iPP) {
            sb.append(i % 10);
        }
        sb.append(k);
        return sb.toString();
    } 

}

compile

javac MemoryMap.java

run

separately
java MemoryMap alloc gc 

java MemoryMap gc 

and open another terminal window to view

respectively.

assignment of 100m + characters

?  $ jps
12426 MemoryMap
12428 Jps

?  $ jmap -histo 12426

 num     -sharpinstances         -sharpbytes  class name
----------------------------------------------
   1:          1160      209829280  [C
   2:            92        9702688  [I
   3:           485          55304  java.lang.Class
   4:            11          33232  [B
   5:          1147          27528  java.lang.String
   6:           525          26424  [Ljava.lang.Object;
   7:            79           5688  java.lang.reflect.Field

cases where there is no allocation

?  $ jmap -histo 12912

 num     -sharpinstances         -sharpbytes  class name
----------------------------------------------
   1:            92         685024  [I
   2:          1038         110832  [C
   3:           485          55304  java.lang.Class
   4:            14          33872  [B
   5:           524          25968  [Ljava.lang.Object;
   6:          1025          24600  java.lang.String
   7:            79           5688  java.lang.reflect.Field

the difference in the number of string bytes between the two cases is 27528-24600mm 2928
the difference in the number of bytes in the character array is 209829280-110832209718448

actually 100m + numeric characters account for 200m + bytes in memory

Menu