如何清空long for循环中的所有变量,以避免程序耗尽内存



我有一个运行了20,000次的for循环,其中for循环也运行了20,000次,总共运行了4亿次。

在for循环中,它将20,000个数字添加到字符串中,然后将该字符串写入TXT文件。String Name = "";

所以我将得到总共20,000个文本文件,每个文件中有20,000个数字。

现在,如果for循环创建了大约200个文件,它开始耗尽内存并最终崩溃。

如何避免这种情况?

——这是代码——

public static void mapScanner()
{
    String content = "";
    for(int z = -10000; z < 10000; z++)
    {
        Util.CreateFile(z);
        for(int x = -10000; x < 10000; x++)
        {
            Block block = Bukkit.getServer().getWorld("world").getBlockAt(x, Bukkit.getServer().getWorld("world").getHighestBlockYAt(x, z) -1, z);
            if(block.getType() != Material.AIR)
            {
                content += Blocks.blockID(block.getType());
            }
        }
        try 
        {
            File file = new File("plugins/Map/" + z + ".txt");
            System.out.println(content);
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            content = "";
            System.out.println("The file : " + z + ".txt has been created and written.");
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

block.getType()只返回一个数字。


通用代码:

for(int x = 0; x < 20000; x++)
    {
        String content = "";
        CreateFile(x);
        for(int z = 0; z < 20000; z++)
        {
            content += "1";
        }
        try 
        {
            File file = new File("Map/" + x + ".txt");
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
            //System.out.println("The file : " + x + ".txt has been created and written.");
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

public static void CreateFile(int z) {
    File file = new File("Map/" + z + ".txt");
    boolean fileCreated = false;
    try {
        fileCreated = file.createNewFile();
    } catch (IOException ioe) {
        //System.out.println("Error while creating empty file: " + ioe);
    }
    if (fileCreated) {
        //System.out.println("Created empty file: " + file.getPath());
    } else {
        //System.out.println("Failed to create empty file: " + file.getPath());
    }
}

如果你把所有的内容都保存在内存中,你总是会有性能问题,给我一些代码来帮助你。

对我来说,一种方法是直接写入文件,所以不需要在内存中保存字符串。

为我的语法感到抱歉,我不擅长写作。

试试这样:

public static void mapScanner()
{
String content = "";
for(int z = -10000; z < 10000; z++)
{
    File file = new File("plugins/Map/" + z + ".txt");
    // if file doesn't exists, then create it, this inside another try.
    if (!file.exists()) {
        file.createNewFile();
    }
    FileOutputStream fw = new FileOutputStream(file);
    OutputStream bw = new BufferedOutputStream(fw);
    for(int x = -10000; x < 10000; x++)
    {
        Block block = Bukkit.getServer().getWorld("world").getBlockAt(x, Bukkit.getServer().getWorld("world").getHighestBlockYAt(x, z) -1, z);
        if(block.getType() != Material.AIR)
        {
            string temp = Blocks.blockID(block.getType());
            System.out.println(temp);
            bw.write(temp.getBytes());
        }
    }
    try 
    {
        // Close all
        bw.flush();
        bw.close();
        fw.close();
        file.close();
        System.out.println("The file : " + z + ".txt has been created and written.");
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
 }
}

String content = ""替换为StringBuilder content = new StringBuilder(),将content += Blocks.blockID(block.getType())替换为content.append(Blocks.blockID(block.getType()))

这应该使循环运行得快得多。我怀疑它会帮助你的OutOfMemory问题,尽管

最新更新