1

I began learning java recently (with the extra free time i have now) and wrote code for a game called torn using netbeans 8.2. This code is a runnable that continuously retrieves data from the site and stores it for later use. It was freezing mid-run, and i was told that adding a connect timeout and a read timeout for the url connection might help. I did that and it is currently running again (so far so good), and am waiting to see if that problem will happen again or not.

My question is about finding memory leaks. I read that the number of surviving generations is an indicator of whether there is a memory leak. This is a screenshot of the telemetry after a few hours of running. The code for the project is also available (OtherFactionsStats.java is the main class). (Any notes about improving my code are very welcome as well :D ). I appreciate the help.

bookish303
  • 65
  • 5
  • This doesn't look like a memory leak - at least not one that is recognizable after 8 hours. Look at the top-right diagram, the blue part: the used memory oscillates between maybe 20 MB and 50 MB over the whole 8 hours. If you had a memory leak this would be a curve with a raising trend (meaning that after the 8 hour period you measured the peaks would be much clearly higher) – Thomas Kläger Mar 31 '20 at 06:36
  • thats a relief. one more question..why do the surviving generations keep increasing.. thats bugging me. And thanks – bookish303 Mar 31 '20 at 15:18
  • They keep increasing because in your main method you create a `new RunUpdater(FactionIDpath);` every 5 seconds (`service.scheduleAtFixedRate(runnable, 0, 5, TimeUnit.SECONDS);`) and sometimes these RunUpdaters sleep for almost an hour... – Thomas Kläger Mar 31 '20 at 15:57
  • to my knowledge, the new instance is not created until the first one finishes executing.. also i compared the times in telemetry, and during sleep time the number of generations remains constant.. it actually increases much more rapidly when timeouts happen..thanks again – bookish303 Mar 31 '20 at 16:45

1 Answers1

1

I think that I finally found your resource leak: in LogFactionPersonalStat on line 154 you open a file with in = new BufferedReader(... that never is never closed.

I suggest that you learn about try with resources.

The code in the same file, lines 128 to 130 for example would change from

    FileWriter file = new FileWriter("" + path + completed); //set completion status to incomplete
    file.write("0"); //zero means not yet complete
    file.close();

to

    try (FileWriter file = new FileWriter("" + path + completed)) { //set completion status to incomplete
        file.write("0"); //zero means not yet complete
    }

Not a big change, but now you cannot forget the close the FileWriter (or BufferedWriter or BufferedReader)


One other note: RunUpdate line 53 looks very suspicious (this pattern is repeated several times):

    Logger.getLogger("Error reading from file");

Here you create a looger for "Error reading from file", but you never use that logger to write anything into the log.

You probably meant to write something like

    Logger.getLogger(OtherFactionsStats.class.getName()).severe("Error: Possible timeout or problems with file");

or

    Logger.getLogger(OtherFactionsStats.class.getName()).log(Level.Severe, "Error: Possible timeout or problems with file");
Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • I will look into that and see if anything changes. thank you – bookish303 Apr 01 '20 at 00:34
  • Hello again. solved a lot of kinks, but the project still freezes after about 40 hours.. my new post if you feel like reading up: https://stackoverflow.com/questions/61053452/can-a-java-project-with-a-runnable-that-runs-at-a-fixed-rate-stop-after-a-while... thank you for all the help you gave – bookish303 Apr 06 '20 at 05:28