J2me - OutofMemory in HTTP request | Telit Cinterion IoT Developer Community
December 9, 2016 - 10:34am, 2451 views
I have a method in my BGS5T project in which, after 2 days of normal working, it collapses. The error presented is the following
Contacting website...
java.lang.OutOfMemoryError
(stack trace incomplete)
The said method is the one used to communicate with a website. It receives a string and a mode selector (i=1 or smth else) and procedes with the request. Here's the code:
void enviarPost(int i, String comando)throws IOException, IllegalStateException, IllegalArgumentException, ATCommandFailedException{ System.out.println("Contacting website..."); if(i == 1) { url = "http://websitedummy.com.pl/index.php?IMEI=" + imeiX + "&IP=" + ipX; } //53543D303B44723D4E616F else { url2 = comando; url = "http://websitedummy.com.pl/index.php?data={\"IMEI\":\""+imeiX+"\",\"TS\":\"20/04/13-08:31:44\",\"SER\":\""+url2+"\"}"; } try { Thread.sleep(1000); connection = (HttpConnection) Connector.open(url); Thread.sleep(500); connection.setRequestMethod(HttpConnection.GET); Thread.sleep(500); connection.setRequestProperty("Content-Type", "text/plain"); Thread.sleep(500); int con = 0; try{ con = connection.getResponseCode(); } catch (Exception e4) { System.out.println(e4); } if (con == HttpConnection.HTTP_OK) { System.out.println("Vamos"); inputstream_ = connection.openInputStream(); int ch; while ((ch = inputstream_.read()) != -1 ) { dataReceived.append((char) ch); } System.out.println("Atualizado."); acabouatualizar=1; inputstream_.close(); connection.close(); } else { System.out.println("Error"); // Connection not ok } } catch (Exception e) { System.out.println("EXCEÇÂO 1 - " + e); } finally { if (inputstream_ != null) { try { inputstream_.close(); } catch (Exception e1) { System.out.println("EXCEÇÂO 2- " + e1); } } if (connection == null) { try { connection.close(); } catch (Exception e2) { System.out.println("EXCEÇÂO 3- " + e2); } } } }
To solve the issue i thought about clearing all the variables used in the connection. The problem is I kind of have to be almost sure what the issue is because the error will take 2 days to happen and this will cost me a great amount of time.
Any suggestions?
Thanks guys.
Hello,
OutOfMemoryError suggests that some resources are not released in the application. It could be somewhere outside of the method that you have pasted.
But in this method I can see that in the finally block you only try to close the connection if it is null and there should be condition if it is not null. Because the connection object is never null you don't get the NullPointerException here. Before the finally block you only close the connection if you receive HTTP_OK. So the connection is probably not always closed.
Regards,
Bartłomiej
I have updated the code with your suggestions.
I don't know if the problem is going to be solved but I would like to know:
Does it exist any way of rebooting the system when the code ends like this?
I have the following configurations in my midlet:
Shoudn't the modem reboot when this error occurs?
Thanks
Hello,
It should be possible. According to the document:
- Oracle-MIDlet-Restart: [true|false], if true the MIDlet is automatically restarted if it terminates
non gracefully, e.g. by an uncaught exception.
- Oracle-MIDlet-Restart-Count: [number], number of allowed MIDlets restarts before the
whole module is rebooted
Is it not happening in your case? On the other hand OutOfMemoryError is a special situation when there is no free memory for the application to run because some resources were not freed. And this is usually the programming error.
After the application crashes the JVM is still running so I suppose that the problem with free memory will still exist and may still cause problems with the stability of the platform especially with starting the MIDlet again without rebooting the whole system. So for this particular situation probably setting Oracle-MIDlet-Restart-Count: 0 would be the best choice.
On the other hand in the production version of the app this should not happen.
Regards,
Bartłomiej