mercredi 1 avril 2015

java String in following code leak memery? how to fix it

Here is the function:



String gen() {
String raw_content = "";
String line;
for (int i=0; i<10000; i++) {
line = randString();
raw_content += line + "\n";
}
return raw_content;
}


When I call gen() for 100 times in main(), my program will stuck. I suspect this is related to memory leak caused by Java String. So will the no-longer used memory be freed by JVM automatically? How to fix this?


Thanks!


Aucun commentaire:

Enregistrer un commentaire