Answer the question
In order to leave comments, you need to log in
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();
}
});
}
}
Answer the question
In order to leave comments, you need to log in
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]);
}
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 questionAsk a Question
731 491 924 answers to any question