j2me - Out of memory exception, does it have anything to do with the maximum heap or jar size?

可紊 提交于 2019-12-01 10:35:00

问题


I'm currently developing an app for taking orders. Before I ask my question, let me give you a few details of the basic functionality of my app:

The first thing the app does once the user is logged in, is to read data from a webservice (products, prices and customers) so that the user can work offline. Once the user have all the necessary data, they can starting taking orders. Finally, at the end of the day, the user sends all the orders to a server for its processing.

Now that you know how my app works, here's the problem : When I run my app in the emulator it works, but now that running tests on a physical device When I read data from the webservice, the following error appears on the screen :

Out of memory error java/lang/OutOfMemoryError

At first I thought that the data that is read from the WS (in json format) was too much for the StringBuffer :

             hc = (HttpConnection) Connector.open(mUrl);

            if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
                is = hc.openInputStream();
                int ch;
                while ((ch = is.read()) != -1) {
                    stringBuffer.append((char) ch);
                }
            }

But it turned out that the error occurs when I tried to convert the result from the WS (string in json Format) into a JSONArray . I do this because I need to loop through all the objects (for example the Products) and then save them using RMS. Here's part of the code I use:

      rs = RecordStore.openRecordStore(mRecordStoreName, true);
            try {

                // This is the line that throws the exception
                JSONArray js = new JSONArray(data); 

                for (int i = 0; i < js.length(); i++) {

                    JSONObject jsObj = js.getJSONObject(i);
                    stringJSON = jsObj.toString();                   
                    id = saveRecord(stringJSON, rs);             


                }

The saveRecord Method

    public int saveRecord(String stringJSON, RecordStore rs) throws JSONException {
            int id = -1;
            try {

                byte[] raw = stringJSON.getBytes();
                id= rs.addRecord(raw, 0, raw.length);

            } catch (RecordStoreException ex) {
                System.out.println(ex);
            }
            return id;
        }

Searching a little , I found these functions : Runtime.getRuntime().freeMemory() and Runtime.getRuntime().totalMemory()

With those functions I found out that the total memory is : 2097152 and the free memory before the error occurs is : 69584 bytes.

Now the big question(or rather questions) is :

  1. Where this little amount of memory is taken from ?? The heap size?? The device's specifications says that it has 4MB
  2. Another thing that worries me is if RMS increases the JAR size because the specifications also say that the maximum jar size is 2 MB.

As always, I really appreciate all your comments and suggestions.

Thanks in advance.


回答1:


i don't know what is the exact reason but these J2ME devices indeed have a memory problem.

my app is working with contacts, and when i tried to receive the JSON of contacts from the server, if it was too long the conversion indeed caused an out of memory error.

the solution that i found is paging. divide the data that you receive from the server into parts and read it part by part.



来源:https://stackoverflow.com/questions/24342116/j2me-out-of-memory-exception-does-it-have-anything-to-do-with-the-maximum-hea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!