Problems with generating chunks...

Discussion in 'Plugin Development' started by yottabyte, Mar 29, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    yottabyte

    So basically, I'm creating a plugin that generates a whole bunch of chunks (Kind of like the MinecraftLandGenerator but actually works and does it way faster). I've got all the functions together and I'm able to generate up to a 1024x1024 map in no time but then the problems appear.

    Now, my main function is just looping through chunks in a square. I've then put something like this for the generation part
    Code:
    world.unloadChunk(X-1, Z);
    world.regenerateChunk(X, Z);
    This looks all fine on the surface but if we look into details in the Bukkit source, unloading a chunk doesn't remove it from RAM, it just removes it from one HashMap and puts it into another one if I'm correct.
    I think there are even several HashMaps that just keep expanding the more you generate and I see no end to it. By debugging, I can see that just generating something 16000 chunks (2048x2048) expands 3 different maps, each holds something 5 million instances. What the ef?
    From what I can tell, there's one HashMap, one TreeMap and one net.minecraft.server.MetacodeChunkBlock. The HashMap is storing a whole bunch of information about chunks, dungeons, entities etc. even though I unloaded them.

    My guess is that there is something shifty with the unloadChunk function. This only happens when I keep chunks in RAM for some time. If I unload the chunk instantly it works much better (generated 7k*7k in 15 minutes) but there is no trees,flowers,snow etc. generated.

    Anyone else had this problem? Is it just a bug with Bukkit or am I doing it wrong? Halp ASAP please
     
  2. Offline

    Edward Hand

    If you could give me the names/locations of the Maps you mentioned I might be able to give some clues as to what is happening.
     
  3. Offline

    yottabyte

    Problem is, I'm just tracking them through VisualVM. I don't know the names and I don't know if I can find them somehow :/ It's very late, I will check again tomorrow.
     
  4. Offline

    Raphfrk

    You need to distinguish between the CraftChunk class and the Chunk class.

    The Chunk class is the minecraft server chunk class. The CraftChunk class is the CraftBukkit wrapper.

    The CraftChunk class stores a CraftBlock cache, so it is never removed. Note: the CraftBlock class doesn't actually store block info, it is just a handle to connect to the underlying blocks (via the CraftChunk).

    Also, the block cache uses soft references, which means that the garbage collector is allowed to remove them (as long as there are no other references to them).

    However, when you unload, the Chunk class should be unloaded.

    When you unload a chunk what is supposed to happen is

    a) Chunk class is saved by the server
    b) Chunk class is removed from the HashMap in the server
    c) Link to the Chunk in the CraftChunk class is pointed to null
    d) CraftChunk class is moved to backup HashMap

    The effect of b) and c) is that all references to the Chunk should have been removed. This means that the garbage collector can remove the Chunk class.

    As for generating and then instantly unloading. Do you actually get a situation where you can can go to the chunk (in game) and see no trees etc?

    Each chunk has a "decorated" flag. If you load the chunk and 3 of its neighbours are loaded, it is supposed to decorate it. Since you unload immediately, none of the chunks you load will have neighbours.

    However, it a player goes there it should decorate, since the chunk will have to be surrounded by other loaded chunks.
     
    yottabyte likes this.
  5. Offline

    yottabyte

    Wow, thanks for the detailed information!
    I want to generate "decorations" through the plugin too though and not only the terrain. I'm guessing generating decorations put some load on the server, reason people use the plugin is so that the server wont have to generate when running since that cause lag. That's why I kept chunks in memory for just some time and it seemed to work but that's when the memory issues happen.

    I will try to generate a map without decorations and see if they generate when exploring.
     
Thread Status:
Not open for further replies.

Share This Page