My java program runs forever (intentionally); main has a while(true) that does lots of work in reading JSON data from servers and doing lots of internal processing into an embedded h2 database.
there are 2 static functions I have defined outside of main to do the majority of the work: getUrl (perfoms HTTP GET with input parameters) and postUrl (performs HTTP POST with parameters). Both simply request HTTP data from localhost server, and return to the caller a JsonNode object (from jackson json class) that the guts of the while(true) process data from, into h2.
After a few days the java process is mysteriously "Killed"; research shows OOM is most likely culprit. Ive tried using System.gc() at very end of the while(true) but that doesnt help. Here is postUrl (getUrl is nearly identical, postUrl has 2 args input to it, getUrl only needs 1)
static JsonNode postUrl(String requestType, String postData) throws Exception {
//use HTTP POST to API and return JSON data
String output="";
try {
URL obj = new URL(post + requestType);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
String urlParameters = urlParmPrefix + requestType + postData;
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
output = response.toString();
} catch (Exception postUrlFUNCTION) {
System.out.println("Function postUrl failed for: " + postData);
postUrlFUNCTION.printStackTrace(System.out);
}
if (arg.equals("debug")) System.out.println(output);
ObjectMapper mapper = new ObjectMapper();
return (JsonNode)mapper.readValue(output, JsonNode.class);
}
These 2 functions are called, literally hundreds of times per second. I can see in HTOP program the RES field growing every minute or so. Its not at all my intention to carry any memory/objects into each interation of that while(true), id like to toss it all, but the System.gc() doesnt seem to help.
is this part of whats causing my issue, or perhaps something else in the while?