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.
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");