Optimized for Error, OutOfMemoryError:

Discussion in 'Plugin Development' started by passepartout007, Sep 3, 2020.

  1. Offline

    passepartout007

    Hello,
    I have a little memory problem when I perform my function.

    Code:
    package fr.passepartout007.gestionterrain;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.util.HashMap;
    import java.util.concurrent.TimeUnit;
    
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.data.BlockData;
    import org.bukkit.entity.Player;
    
    import fr.passepartout007.gestionbasedonnee.basededonne;
    import fr.passepartout007.lancementplugin.lancement;
    public class SaveNewArena extends Arena{
    
        public SaveNewArena(lancement lancement) {
            super(lancement);
            // TODO Auto-generated constructor stub
        }
       
        public void exportNewArena(Player player, String name) {
            World world = player.getWorld();
            player.sendMessage("§bSauvegarde de la map");
            HashMap<String,BlockData> saveBlock = new HashMap<String,BlockData>();
            //boucleZ
            for(int i= -1232 ; i<-1098 ; i++) {
                //bouclex
                for(int j= -1285 ; j <-1179 ; j++) {
                    for(int k=0 ; k<69 ;k++) {
                        Location location = new Location(world, i, k, j);
                       
                            BlockData data = location.getBlock().getBlockData();
                            saveBlock.put(location.toString(),data);
                       
                    }
    
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
               
                    player.sendMessage("§bIl reste : §f" + (((i*-1)-1098) + " couches"));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            basededonne bdd = new basededonne();
            String chemin = name +"txt";
            chemin = bdd.CheminFichier(chemin);
           
            exportArenaData(chemin,saveBlock);
        }
       
        public void exportArenaData(String chemin,HashMap<String,BlockData> saveBlock ) {
            ObjectOutputStream oos;
            try {
               
                oos = new ObjectOutputStream(
                          new BufferedOutputStream(
                                  new FileOutputStream(
                                    new File(chemin))));
               
                oos.writeObject(saveBlock);
                oos.close();
           
            } catch (FileNotFoundException e) {
                  e.printStackTrace();
            } catch (IOException e) {
              e.printStackTrace();
            }  
        }
    }
    

    Here is the error:

    Code:
    org.bukkit.event.EventException: null
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:437) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:441) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1250) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:917) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:910) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:827) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
    Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded
            at java.util.Arrays.copyOf(Unknown Source) ~[?:1.8.0_251]
            at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source) ~[?:1.8.0_251]
            at java.lang.AbstractStringBuilder.append(Unknown Source) ~[?:1.8.0_251]
            at java.lang.StringBuilder.append(Unknown Source) ~[?:1.8.0_251]
            at java.lang.StringBuilder.append(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.Location.toString(Location.java:586) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at fr.passepartout007.gestionterrain.SaveNewArena.exportNewArena(SaveNewArena.java:40) ~[?:?]
            at fr.passepartout007.phasedujeux.attenteduJeux.onInteract(attenteduJeux.java:78) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_251]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_251]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_251]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:316) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:529) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:514) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:437) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:441) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1250) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils$$Lambda$3475/1770178518.run(Unknown Source) [spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.aZ(MinecraftServer.java:917) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:910) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:894) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:827) ~[spigot-1.15.1.jar:git-Spigot-2ee05fe-d31f05f]
    [17:33:51] [Server thread/WARN]: Can't keep up! Is the server overloaded? Running 61069ms or 1221 ticks behind
    [17:33:51] [Server thread/WARN]: passepartout007 moved too quickly! -9.231519595640066,4.5,-1.1644467944779535
    I don't understand why this code takes so much memory ... can you help me optimize my code to avoid this error?
    faithfully,
    Passepartout007
     
  2. Offline

    Kars

    1232 - 1098 = 134
    1285 - 1179 = 106
    134 * 106 * 69 = 980,076

    You are creating close to a million Location and BlockData objects in that nested loop of yours.
    That'll do it every time :)
     

Share This Page