每次从文件写入/读取后,Java内存使用量都会增加。为什么?

时间:2015-12-02 17:24:15

标签: java memory save loading bufferedreader

我为基于磁贴的游戏编写了一个地图编辑器,一切运行正常,除了一件事:每当我写入文本文件或从文本文件中读取时,内存使用率会急剧上升,甚至不会下降或保持不变如果我关闭输入/输出流。

以下是加载示例代码:

public static void loadMap(String mapName, int width) {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./" + mapName + ".map")))) {
        Level.width = width;
        Level.height = width;
        String sCurrentLine;
        int lineCount = 0;
        int length = width * width;
        tiles = new int[length];
while ((sCurrentLine = br.readLine()) != null) { String[] dataArray = sCurrentLine.split(","); for (int i = 0; i < dataArray.length; i++) { tiles[i + lineCount * width] = Integer.parseInt(dataArray[i]); } lineCount++; } br.close();
} catch (IOException e) { e.printStackTrace(); } isLoading = false; }

存储图块的类的负责人:

private: void Fillcombo(void) {

         String^ constring = L"datasource=127.0.0.1;port=3306;username=root;password=12345";
         MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
         MySqlCommand^ cmdDataBase = gcnew MySqlCommand("select * from batcel.maq_corte;", conDataBase);

         MySqlDataReader^ myReader;
         try{
             conDataBase->Open();
             myReader = cmdDataBase->ExecuteReader();

            while (myReader->Read()){
                String^vResponsavel;
                vResponsavel = myReader->GetString("id_responsavel");
                comboBox2->Items->Add(vResponsavel);

最后但并非最不重要: level = new Level(64,64); 在线程中被调用。

任何人都可以告诉我,为什么每次加载后内存使用量会增加? 我知道mapsize非常大(4096x4096),但我尝试了各种尺寸,无关紧要,每次加载或保存时内存使用量都会增加。 我不希望我的程序在50次保存后消耗超过4GB的RAM。

0 个答案:

没有答案