Garbage Collection..

Discussion in 'Plugin Development' started by werter318, Oct 11, 2013.

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

    werter318

    Hey guys,

    I own a minecraft minigame server, and I'm having some problems with the server memory. The minigame is earthbending (I made it myself, custom one) and everytime someone does an action it makes an object of that action, and gets removed after its done (All references get removed). but I just can't understand why the ram is increasing so fast when like 10 players are playing...

    any advice?
     
  2. Offline

    Rocoty

    Might be that you overlooked some references. Show us some code?
     
  3. Offline

    werter318

    EarthWave[] EarthWaveArray = new EarthWave[10];
    this for example is where the Earth Waves get stored, and also the only place where it's stored

    Code:java
    1.  
    2. for (int i = 0; i < EarthWaveArray.length; i++) {
    3. if (EarthWaveArray[I] != null) {[/I]
    4. [I] if (!EarthWaveArray[I].done) {[/I][/I]
    5. [I] EarthWaveArray[I].update();[/I][/I]
    6. [I] } else {[/I]
    7. [I] EarthWaveArray[I] = null;[/I][/I]
    8. [I] }[/I]
    9. [I] }[/I]
    10. [I] }[/I]


    the array is static
     
  4. Offline

    The_Doctor_123

    werter318
    We can't exactly help you by you giving little snippets of code you think that are the problem, which that is not.
     
  5. Offline

    werter318

    Oh, uhm.. okay

    I can't really show you all the code, but I'll try to explain what happens.

    In BendingAction.java there is a method called registerAction(EarthWave earthwave, Player player)
    this looks if the EarthWaveArray has an empty spot to put it in. then every 2 ticks update() gets called and all the EarthWave objects get updated, if they are done, boolean done will be true, and the object will get deleted from the array. the only problem is that it doesn't seem to get garbage collected.
     
  6. use a debugger to see what object is created the most times and how memory its using, then you get a point from where you can search
     
  7. Offline

    werter318

    How would I do this?
     
  8. Offline

    Rocoty

    Okay...you don't want to show us code...but that means we can't help you any more.
     
  9. Offline

    werter318

    I explained it as good as I can.
     
  10. Offline

    Rocoty

    werter318 I understand. I probably wouldn't be able to explain better. Frankly, though, what one can see from the code might reveal more than what one is able to explain.

    May I ask why you don't want to show more code?
     
  11. Offline

    Tehmaker


    Generally, people don't want other stealing their code. To be honest though, werter318, most people in this section that are answering questions, could easily code their own plugin to do exactly what yours does, without your code.

    If you really want help, post your whole class. I doubt many people will be stealing your code, as they would need all of your classes to have anything effective.
     
  12. Offline

    Comphenix

    Use a free profiler like VisualVM, or my favorite: JProfile.

    That will allow you to examine every allocated object, and perhaps track down the memory leak.
     
    Axe2760 likes this.
  13. Offline

    1Rogue

    Titling this "Garbage collection" is a tad bit misleading, since this doesn't have much to do with GC generations or general operations. As others have stated, if you want help in code optimization, then you need to provide code to be optimized. You can't optimize that which does not exist.
     
  14. Offline

    The_Doctor_123

    werter318
    With the information given, I, nor anyone, can help you. Sorry.
     
  15. Offline

    werter318

    Thanks! This is very useful.


    I'm sorry, but I didn't come here to give out my code and let you guys fix it(mainly because its for my server). I came here to learn a bit more on how GC works, and I think the title suits the topic totally fine.
     
  16. Offline

    The_Doctor_123

    werter318
    One simple answer. Variables are kept, and not thrown away by the GC if they have the potential to be used again. That's your answer. Put that solved tag in there now that you know it all.
     
  17. Offline

    werter318

    I know that part, now tell me why the ram keeps increasing even though all references are GONE.
     
  18. Offline

    The_Doctor_123

  19. Offline

    1Rogue


    Because you are either attempting to optimize with poor choices of JVM arguments on startup or you are doing some type of bad code management.
     
  20. Offline

    werter318

    The_Doctor_123 1Rogue
    They are, I just did some profiling with VisualVM (Thanks Comphenix ) and found out the problem has nothing to do with my plugins. One thing I noticed is that there are SO many byte arrays, the number just keeps increasing.
     
  21. Offline

    Comphenix

    That's probably just the chunk world data, which is usually complicit in these matters. A plugin is unlikely to generate enough data to overwhelm the heap by itself, but if it accidentally keeps a reference to game data (by storing Block or Entity instances somewhere), it can prevent it from being collected.

    You should look for instances of your own classes (such as EarthWaveArray), and see where and why they are being kept. Also, you don't have to reference Bukkit classes directly - consider using implicit pointers such as entity ID and block location, for entities and blocks respectively, or weak references. That alone may be able to solve, or at least significantly minimize the damage, the memory leak.
     
  22. Offline

    LucasEmanuel

    One important thing to remember regarding the GC is that if you declare anything as static, it will never be cleared from memory by the GC. Even if there are no references to a static object, it will persist in memory until the program is terminated.
     
  23. Correct me if I'm wrong, but as far as I know, there are no such things like static objects in java. Saving a reference to an object in a static field is the same thing as saving it anywhere else. The only problem is, that the reference is saved along with the Class what is again referenced by the ClassLoader that loaded it. As a ClassLoader never forgets about the classes it loaded, the only way to get this reference gc'ed, is to remove all references to the ClassLoader, causing it self to get gc'ed. In fact all this doesn't prevent you from simply setting the static field that saves the reference to null, to free the reference, and allow the referenced object to be gc'ed. As I got the Op, he exactly did this. I hope so.
     
  24. Offline

    LucasEmanuel

    hapm
    You are correct :) A little slip-up from me ;)
     
  25. Offline

    The_Doctor_123

    werter318
    All that there's needed to be said has been said. So you two options:

    1. Take a very close inspection of your code and fix the issue yourself.
    2. Take the "risk" of us "stealing" your code and show us it so we can actually tell you what's wrong. We don't read minds.
     
Thread Status:
Not open for further replies.

Share This Page