running out of memory using ArrayLists (HELP!)

Discussion in 'Plugin Development' started by tom1000o, Mar 17, 2013.

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

    tom1000o

    This is where teaching yourself java instead of taking a class on it will bite you in the butt. after about 10 or 20 seconds, my server crashes due to no memory being left. im running code similar to this, but i dont know why its taking so much memory? its got something to do with the arraylist. does anyone know whats up?
    Code:Java
    1. ArrayList<Location> blocks = new ArrayList<Location>();//defined globally
    2.  
    3. public ArrayList<Location> cube (Location l1, Location l2){
    4. blocks.clear();//this should take care of the memory problem, but it doesnt!
    5. //change l1's location by one block with for loops, eventually covering every block between l1 and l2.
    6. blocks.add(l1.clone());
    7. return blocks;
    8.  
    9. }
    10.  
    11. public void mainMethod(){//runs every 20 ticks
    12. //'one' and 'two' are corners of a small square (3x3)
    13. Location one = null, two = null;//ignore these being null
    14. for (Location l : cube(one,two)) {
    15. l.getBlock().setType(Material.GLASS);
    16. }
    17. }
     
  2. Offline

    chasechocolate

    How many blocks are you changing? Also, how much ram are you starting your server with?
     
  3. Offline

    tom1000o

    chasechocolate

    1gig of ram, and im changing 9 blocks each time the mothod is called. its a 3x3 2D cube under an entity.

    1gig of ram should be plenty, im just obviously not clearing something after every use, and im not sure what it is...
     
  4. Offline

    Shevchik

    l1.clone() probably this
     
  5. Offline

    tom1000o

    Shevchik
    it still happens with this code:

    Code:java
    1. Location temp = new Location(l1.getWorld(), l1.getX(), l1.getY(), l1.getZ());
    2. blocks.add(temp);
    3. temp = null;
     
  6. Offline

    XDemonic25

    Code:
    blocks.add(temp.getBlock());
    
    That work maybe?
     
  7. Offline

    tom1000o

    XDemonic25

    if 'blocks' was a Block array it would work, but its a Location array.. :p 2 different classes
     
  8. Offline

    XDemonic25

    my bad, didnt see that.. i saw "blocks.add" and auto-matically assumed it was a block xD
     
    tom1000o likes this.
  9. Offline

    tom1000o

    XDemonic25

    its all good lol. im 100% sure its a memory problem with an ArrayList, but i just cant figure out whats going on, because i clear it after every use.

    going to try and make the method that returns the arraylist static... that should help right?

    edit: didnt do anything

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    Comphenix

    When posting a code snippet illustrating your problem, you should try to include just exactly the amount of code needed in order to reproduce it - too much, and the bug is drowned in all the irrelevant noise around it; too little, and you risk losing all the necessary context or even the bug itself. In other words, SSCCE!

    You've definitely taken too much out in this case - the snippet you've posted will neither compile nor cause the problem you've described. It's pseudo code, and only shows how you think the program ought to work, not how it is actually put together.

    The problem might not be with the array list itself - perhaps you're creating too many block instances?
     
    DemmyDemon and microgeek like this.
Thread Status:
Not open for further replies.

Share This Page