我不能,到目前为止,理解为什么当我加载9个不同的文本文件到我的android应用程序时,我得到了内存不足的异常。文本文件的总大小约为10 MB。我不得不在manifest文件中更改为large-heapsize以便能够加载这些文件。这不是一个好的解决方案,所以我想知道是否有人可以帮助我了解是什么原因导致堆大小超过100 MB。
另一件事,可能是什么线索??当heapsize超过100 MB并且分配的内存略低于100 MB时——所有加载都完成了,发生了一些事情:
垃圾收集器在加载完成后立即做一些事情-分配的空间从100 MB急剧下降到35 MB。
所以我想知道-发生了什么?为什么加载如此消耗内存?
我可以说我有一个叫做FileManager的类,所有的加载都在这里进行。
因为FileManager不扩展Activity,我必须传递一个context给这个类。是上下文在消耗内存吗?我想可能是,我必须移动加载时,应用程序开始应用程序。
这里是FileManager-class
public class FileManager {
private String[][] solarObj,, celestObj, stars, sun, moon, venus, march, jupiter, saturn;
private ArrayList <String[][]> stringObjects = new ArrayList <String[][]> () ;
private String[] textFile;
private Context context;
private AssetManager assetManager;
public FileManager(Context context) {
this.context = context;
assetManager = context.getResources().getAssets();
instanciateStrings();
putStringsToList();
initializeTextFile();
for (int array_index = 0; array_index < 9; array_index++) {
read(this.textFile[array_index], this.stringObjects.get(array_index).length, array_index);
//printString(array_index);
}
}
private void instanciateStrings() {
this.solarObj = new String[98][4];
this.celestObj = new String[7][3];
this.stars = new String[92][7];
this.sun = new String[14246][15];
this.moon = new String[14246][15];
this.venus = new String[14246][15];
this.march = new String[14246][15];
this.jupiter = new String[14246][15];
this.saturn = new String[14246][15];
}
/**
* Lägger in de tvådimensionella strängvektorerna i en arraylist.
*/
private void putStringsToList() {
this.stringObjects.add(this.celestObj);
this.stringObjects.add(this.solarObj);
this.stringObjects.add(this.stars);
this.stringObjects.add(this.sun);
this.stringObjects.add(this.moon);
this.stringObjects.add(this.venus);
this.stringObjects.add(this.march);
this.stringObjects.add(this.jupiter);
this.stringObjects.add(this.saturn);
}
/**
* Filnamnen läses in i en strängvektor
*/
private void initializeTextFile() {
this.textFile = new String[9];
this.textFile[0] = "celestobj_txt.txt";
this.textFile[1] = "solarObj_txt.txt";
this.textFile[2] = "stars_txt.txt";
this.textFile[3] = "sun_txt.txt";
this.textFile[4] = "moon_txt.txt";
this.textFile[5] = "venus_txt.txt";
this.textFile[6] = "march_txt.txt";
this.textFile[7] = "jupiter_txt.txt";
this.textFile[8] = "saturn_txt.txt";
}
private void read(String text_file, int len, int index) {
String[] stringBuffer = new String[len]; // temporär textsträng som read-objektet returnerar textraden till.
BufferedReader br;
try {
InputStream input = assetManager.open(text_file);
br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
//bufferedReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(text_file)));
String line;
int i = 0;
while ( (line = br.readLine()) != null) {
stringBuffer[i] = line; // läs in rader från textfilen
i++;
}
br.close();
} catch (FileNotFoundException fnde) {
//fnde.printStackTrace();
System.out.println("filerna kunde inte hittas");
} catch (Exception e) {
e.printStackTrace();
}
splitString(stringBuffer, index);
}
private void splitString(String[] str, int index) {
int nCols = this.stringObjects.get(index)[0].length; // antal kolumner
for (int i = 0; i < str.length; i++) {
StringTokenizer st = new StringTokenizer(str[i]);
for (int j = 0; j < nCols; j++) {
this.stringObjects.get(index)[i][j] = st.nextToken();
}
}
}
因为要加载9个文件,所以这个read方法被调用9次。这可能是内存泄漏的根源吗?
感谢您的回答
编辑:我选择展示全班。再次感谢! !
我认为这是因为你正在使用String[]来存储读取行。你可以尝试用StringBuilder代替String[]吗?
// stringBuffer[i] = line;
stringBuilder.append(line);