Android GraphView project get freeze with real time updates
Asked Answered
S

2

2

I am trying to incorporate Android GraphView project into my app and all the time I have some strange problem with it.

My app requires drawing graph from real time data. I have thread with all the communication that is providing the data. In main thread I am reading this data and simply use mSeries1.appendData(new DataPoint(counter,data[0]),true,100); where counter is int that is incremented after each update.

Unfortunately at some point it freeze. I've tried putting it in synchronized block or changing the line of code to mSeries1.appendData(new DataPoint(counter,counter),true,100); and still this same result.

This is how the memory looks like during app running and when it freezes:

enter image description here

Does anyone have any idea what might be wrong in here?

EDIT:

This is my current method for updating my graph view:

public void onEventMainThread(ReadingsUpdateData data) {
        mSeries1.appendData(new DataPoint(counter,data.getData()[0]),true,100);
        counter++;
    }
Sidonnie answered 17/5, 2015 at 18:31 Comment(0)
T
1

Maybe it's too late, but I had the similar problem and finally I found that when GraphView is appended a new data of "NaN" freezes.

So check the situation in which the result will be NaN such as divide by zero or something like that.

Tuinenga answered 16/12, 2016 at 14:4 Comment(0)
O
0

Although you do not specify the rate at which you add points, and how long for the app runs without crashing, you should expect things to go wrong at some point (you're potentially generating an infinite number of point objects, while the memory is indeed limited).

Do you need to have all the points the app has received from the beginning drawn ? If not, you could implement a sort of circular buffer that only keeps the X last values generated by your "provider thread", and update the graph each time you receive a new value with the method

your_series.resetData( dataPoint[] my_circular_buffer_of_data_points );

This thread is quite similar to your problem, have a look at it !

Ocean answered 20/5, 2015 at 12:14 Comment(7)
Your answer looks reasonable but to be honest I am not sure how to use it - when you want this method to be used? As I understand it now, when I'll get new data from my thread I need to update buffer in other method and then (but when) send it to my graph view update method. I will still need to generate new DataPoint objects.Sidonnie
You update your circular buffer with the new data, then call the method I've stated in my answer to update the graph. You will still be generating new DataPoint objects, but you will also allow the garbage collector to destroy all DataPoint that you "flush" out of your buffer, thus avoiding any sort of memory overflow (unless you oversize your buffer).Ocean
I've added my method code so you will see. I still don't get it - if I'll change the line with appendData to buffer.add(new DataPoint(counter,data.getData()[0])); mSeries1.resetData((DataPoint[])buffer.toArray()); (where buffer is arraylist) I'll have some kind of buffer but I don't think this is the way you were thinking about.Sidonnie
You are correct, but I do not think you get the concept of a circular buffer, am I wrong ? Check how it works and maybe an implementation example.Ocean
I've tried a lot of various buffers in here with number of variations and nothing work - still at some point I get this same error. Can you give any example of described by you working method where I can have infinite number of points?Sidonnie
You cannot have an infinite number of points. Your app would be terminated by the Android OS for using too much memory. Have you tried implementing the buffer described in the link in my previous comment ?Ocean
I have tried to use a Queue: pop old data and push new data. It's easy, however, GraphView requires an array and creating an array from the Queue causes additional overhead.... :-(Folberth

© 2022 - 2024 — McMap. All rights reserved.