Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Proper way of defining Area or Ints?


    dirtrider478

    Recommended Posts

    Moral of the story some of my scripts that require allot of walking and interacting will use an absurd amount of ram, sometimes up to 1gig and still crashing because of memory error after 16+ hours of running. So after ripping my hair out for weeks on end ill take it to the community for help. And figured the only reason for this to be happening is im creating to much of one thing in my script and its not being erased. So my question to the community is defining area's gameobjects and npc's as a final, aid in using less memory. For instance does it create a new area or gameobject every time its called on from within the loop?

     

    Currently storing game objects, items, and npc's like

    private GameObject fountain;

    would it make a difference on the memory usage if I defined them fully and made them into a final like

    private final GameObject fountain = getGameObjects().closest("Waterpump");

     

    any help would be awesome, again ive mainly noticed this reccuring issue in scripts that do allot of interacting and walking in the game like fishing and interacting with objects or npc's.

     

    Link to comment
    Share on other sites

    By using the final keyword on your variables you aren't able to assign a new value to that variable. You don't want this. For example, if your character is too far a way from the waterpump, then the closest() method returns null. Now your variable has the value of null and you're not able to assign another value to it, making your variable is completely useless. Use final on the things that don't need to change, such as names, ids, tiles or areas.

    In most cases you don't need to store references to gameobjects, items or npcs in anything other than a local variable. The most important thing to focus on is, that you fetch the thing you need when you need it. Here is an example of a bad and a good way of writing your script:

    The bad way

     
    
        public int onLoop() {
            Area bankArea = new Area(0, 0, 0, 0);
            GameObject bankBooth = GameObjects.closest("Bank booth");
            Area fishingArea = new Area(0, 0, 0, 0);
            NPC fishingSpot = NPCs.closest("Fishing spot");
    
            if (Inventory.isFull()) {
                if (bankArea.contains(Players.localPlayer())) {
                    if (Bank.isOpen()) {
                        Bank.depositAllItems();
                    } else {
                        bankBooth.interact("Bank");
                    }
                } else {
                    Walking.walk(bankArea.getRandomTile());
                }
            } else if (fishingArea.contains(Players.localPlayer())) {
                if (shouldStartFishing()) { //for example a check if your character is animating
                    fishingSpot.interact("Fish");
                }
            } else {
                Walking.walk(fishingArea.getRandomTile());
            }
            return 60;
        }

    The good way

     
    
        final Area BANK_AREA = new Area(0, 0, 0, 0);
        final Area FISHING_AREA = new Area(0, 0, 0, 0);
    
        public int onLoop() {
            if (Inventory.isFull()) {
                if (Bank.isOpen()) {
                    Bank.depositAllItems();
                } else {
                    if (BANK_AREA.contains(Players.localPlayer())) {
                        GameObject bankBooth = GameObjects.closest("Bank booth");
                        if (bankBooth != null) {
                            bankBooth.interact("Bank");
                        }
                    } else {
                        Walking.walk(BANK_AREA.getRandomTile());
                    }
                }
            } else if (FISHING_AREA.contains(Players.localPlayer())) {
                if (shouldStartFishing()) { //for example a check if your character is animating
                    NPC fishingSpot = NPCs.closest("Fishing spot");
                    if (fishingSpot != null) {
                        fishingSpot.interact("Fish");
                    }
                }
            } else {
                Walking.walk(FISHING_AREA.getRandomTile());
            }
            return 60;
        }
    Link to comment
    Share on other sites

    15 hours ago, Hashtag said:

    By using the final keyword on your variables you aren't able to assign a new value to that variable. You don't want this. For example, if your character is too far a way from the waterpump, then the closest() method returns null. Now your variable has the value of null and you're not able to assign another value to it, making your variable is completely useless. Use final on the things that don't need to change, such as names, ids, tiles or areas.

    In most cases you don't need to store references to gameobjects, items or npcs in anything other than a local variable. The most important thing to focus on is, that you fetch the thing you need when you need it. Here is an example of a bad and a good way of writing your script:

    The bad way

      Hide contents
    
    
        public int onLoop() {
            Area bankArea = new Area(0, 0, 0, 0);
            GameObject bankBooth = GameObjects.closest("Bank booth");
            Area fishingArea = new Area(0, 0, 0, 0);
            NPC fishingSpot = NPCs.closest("Fishing spot");
    
            if (Inventory.isFull()) {
                if (bankArea.contains(Players.localPlayer())) {
                    if (Bank.isOpen()) {
                        Bank.depositAllItems();
                    } else {
                        bankBooth.interact("Bank");
                    }
                } else {
                    Walking.walk(bankArea.getRandomTile());
                }
            } else if (fishingArea.contains(Players.localPlayer())) {
                if (shouldStartFishing()) { //for example a check if your character is animating
                    fishingSpot.interact("Fish");
                }
            } else {
                Walking.walk(fishingArea.getRandomTile());
            }
            return 60;
        }

    The good way

      Hide contents
    
    
        final Area BANK_AREA = new Area(0, 0, 0, 0);
        final Area FISHING_AREA = new Area(0, 0, 0, 0);
    
        public int onLoop() {
            if (Inventory.isFull()) {
                if (Bank.isOpen()) {
                    Bank.depositAllItems();
                } else {
                    if (BANK_AREA.contains(Players.localPlayer())) {
                        GameObject bankBooth = GameObjects.closest("Bank booth");
                        if (bankBooth != null) {
                            bankBooth.interact("Bank");
                        }
                    } else {
                        Walking.walk(BANK_AREA.getRandomTile());
                    }
                }
            } else if (FISHING_AREA.contains(Players.localPlayer())) {
                if (shouldStartFishing()) { //for example a check if your character is animating
                    NPC fishingSpot = NPCs.closest("Fishing spot");
                    if (fishingSpot != null) {
                        fishingSpot.interact("Fish");
                    }
                }
            } else {
                Walking.walk(FISHING_AREA.getRandomTile());
            }
            return 60;
        }

    Well thats good to know that I dont have to store references for gameobjects or npcs, but I couldnt imagine that being part of my problem. Could it cause a ram usage problem because I dont have my area's stored as a final? 

    Link to comment
    Share on other sites

    Storing it as final should not change how the Java GC (Garbage collector) runs on the data (too much). It could be that one of your vars are not going out of scope and you keep adding to it. AKA a memory leak. Here is a pretty good article talking about memory leaks in Java. A big one could be if you are using Streams and not closing them. https://stackify.com/memory-leaks-java/

    Link to comment
    Share on other sites

    3 hours ago, Realistic said:

    Storing it as final should not change how the Java GC (Garbage collector) runs on the data (too much). It could be that one of your vars are not going out of scope and you keep adding to it. AKA a memory leak. Here is a pretty good article talking about memory leaks in Java. A big one could be if you are using Streams and not closing them. https://stackify.com/memory-leaks-java/

    Im gonna go through one of the scripts and clean it from any extra stuff thats not necessary for it to run and change allot of how things are stored. Ill let yall know if its successful. 

    Link to comment
    Share on other sites

    • 2 weeks later...

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.