dirtrider478 12 Share Posted September 2, 2020 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 More sharing options...
Hashtag 8688 Share Posted September 2, 2020 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 More sharing options...
dirtrider478 12 Author Share Posted September 2, 2020 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 More sharing options...
Realistic 20 Share Posted September 2, 2020 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 More sharing options...
dirtrider478 12 Author Share Posted September 3, 2020 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 More sharing options...
LordJashin32 54 Share Posted September 14, 2020 umm. Might wanna use what this post said it can show u memory usage, and cpu usage, and etc. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.