O
O
OM12016-09-05 10:49:21
Android
OM1, 2016-09-05 10:49:21

OkHttp and jsonObject: Why is there a memory leak?

Hello, I'm using OkHttp to get json from the server into a variable that is an adapter for a listView.
This algorithm hangs on TimerTask, i.e. it all happens at regular intervals. When debugging, there is either a leak or something like that.
Those. the amount of used memory is constantly growing and is not released.
Here is the code structure:

mTimer = new Timer();
    mMyTimerTask = new MyTimerTask();
    mTimer.schedule(mMyTimerTask, 1000, 10000);
    
  adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
    listView.setAdapter(adapter);
  
  get_data(){
    OkHttpHandler
    result = handler.execute(url).get();
    jsonObject = new JSONObject(result);
    jArr = jsonObject.getJSONArray("data");
    for(;;){
      items.add(jArr[i]);
    }
  }
  
  class MyTimerTask extends TimerTask {

        @Override
        public void run() {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    get_data();
                }
            });
        }
    }

What's wrong?
How to competently take json from the server and attach it to the listview? If possible... in more detail... Does it
make sense to switch to other libraries for json requests over http or for parsing?
Does it make sense to work with a class, let's call it data, in which to write data after parsing json, or is it better to work through a regular array, then in a listview?
Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2016-09-05
@zagayevskiy

Firstly, you did something wrong with your code, it will not start in Java.
Secondly, you add an element to the list (?) in an endless loop.

for(;;){
      items.add(jArr[i]);
    }

Thirdly, you have a class in which all this obscurantism happens through the timer.
It makes sense to use the model into which the server response is parsed. The parsing code should not know anything about leaves. Listview doesn't need to know anything about downloading from the server. Use MVP.

O
OM1, 2016-09-05
@OM1

1. and 2. Here is the structure of the code, i.e. it's just BLOCKS, it's not working code.
3. In the timer, only the call to get the request and parsing
4. The parser works with an array of strings, ListView is connected to this array through an adapter. The query and parsing are only connected to the list through an adapter.
Here are the questions:
1. where does the memory go: I work with only one array and leaves. Maybe they need to be "cleaned up"?
2. Do I have the correct model (request, parsing into an array, the leaves are filled with this array)?
I wrapped the JSON request through OkHttp into a separate JSONget class, I call and parse through AsyncTask into a separate Data class, by timer.
I screwed it to the view sheet through an adapter of the type: persons.add(new Person(name, text, img));
Tested - like everything is OK, the memory is freed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question