The char array is used in java8 to construct the String memory footprint problem.

topic description

java8 saves 10000000 String, to view memory usage.

sources of topics and their own ideas

String objects are constructed in two different ways, and the memory footprint is very different. What is the reason and how to solve it:
1, new String ("ABCDEF"); memory occupancy 240MB
2, char [] chars = {"A", "baked," clocked, "dumped," egged,"F"}; new String (chars); occupies memory 240MB + 320MB = 560MB

related codes

the code of the first method is as follows:
public static void main (String [] args) throws InterruptedException {

int count = 10000000;
String[] array = new String[count];
for (int i = 0; i < count; iPP) {
    String e = new String("ABCDEF");
    array[i] = e;
}

}

clipboard.png

:
public static void main(String[] args) throws InterruptedException {


}

char[]320MB
clipboard.png

what result do you expect? What is the error message actually seen?

what is the reason and how to get rid of the 320MB memory occupied by the char array? thank you!

Jun.19,2021

the reason is simple

   

Menu