I was doing my 1st steps with android writing a money manager. It stores data in SQLite database and my approach was mostly procedural, while I was creating it.
It means: when I'm creating a chart or a summary it's look like this:
I create a database cursor
I iterate over that cursor to collect neccessary data
I pass the data directly to the element I need (a chart or listview for example)
Well, that works, but I'm learning more oop now and I'd like to rebuild my app.
I'm not fully aware of memory restrictions on mobile devices, that's why I'm creating this thread.
I came up with two ideas. Please tell me, which one you think is better (or correct my approach somehow if it's completly wrong).
Lets use an example of creating a chart or a listview like before. Now I'd do this that way:
I create database cursor.
I create number of objects equal to number of records I need to present data.
I use created objects to pass the data to chart or a listview.
This will require more code than my procedural approach but use of it should be much simplier then, and the code would has more 'proffessional' look (correct me if I'm wrong).
However, I got this dilema. Let's say I'm creating a set of objects based on data from table 'expenses'. I use them to present a chart or a listview on one of my activity. After I close the activity I don't need them anymore. What should I do to let garbage collector toss them away. Anything particullar? (yes, I'm new to the garbage collector stuff).
There is also second oop approach but I'm aware it will require a lot of memory and I'm not sure if it's a good idea at all. So, back to the example again:
I create cursors for each table I got
I create a set of objects matching the tables - basically I pass all the tables into set of objects for further use (I do it in a thread with some progress bar if necessary)
I use the created objects anytime I need to present my data.
Sounds silly, huh? I'm not sure when the garbage collector would dispose all those objects, and if it's a good idea to spam memory with that amount of data in once.
Thanks for any comments on this.
Both solutions work, but the first one is better because the objects are marked for GC as soon as they go out of scope, which happens much earlier than if they are kept for later use. Once the objects are GCed, the memory they occupied becomes available.
Related
I am looking for the smartest (and most effective) way to implement the following project:
I want to develop an app that accesses to about 100 different sport exercises. The exercises are available in an xml-file. The access to the exercises can be in different ways on different activities:
show all
show only exercises of a special category
mark as favorite and show favorites
show details of an exercise
sort
etc.
Loading the xml-file and creating the exercise-objects is already working and its not problem. But I think about the most effective way to implement things like that. Thinking about RAM and performance...
Parsing the xml-file once the app is started, creating the 100 objects and dealing with them during the app is running (of course ensure to reload the data if the objects where cleaned up by the garbage collector in the meantime). Is this possible and recommended? How can such a central point, where I can pick up the objects in all activities, look like? Can I find an example anywhere?
Parsing the xml-file every time an activity (that is using the exercises in any way) is created?
completely different way?
Maybe someone can give me a keyword.
What I understand from your question is you want to parse XML as app starts every time and generate 100s of Object that time, which you want to use in all other activity.
You can extend Application class for accessing same object for more than one activity. Take look at this question
Application is base class of your app and it is the first one to call. So, you can call your service here and also setter/getter of your objects here. By which you can access it in all activities.
Learn more about Application
Also, you can use SharedPreference or SQLite to store your data in database.
It is just my suggestion If possible can you please make change that XML into JSON format, so in that case you can be able to use the GSON Converter and libraries to get rid of the performance issues.
I have recently read inside documentation for Realm database that all of their queries are lazy and I am not sure, if I understand correctly the implications which this may cause.
Let me explain how I understand it and please feel free to correct me if I'm wrong. The way I see it is that, whenever I am making such command mRealm.where(Customer.class).equalTo(Customer.ID, "someId").findFirst(); I do not get Java object with all filled data, that is contained in db for this customer. Instead queries are made whenever I try to access some fields of this "fetched" object. Therefore I am wondering, if that is faster than OrmLite, if I want to access all of the fields of given class?
Another question related to it. Is it good idea to use Realm db for items which would be displayed in ListView or RecyclerView? If queries are constantly made during scrolling of the list, wouldn't it have serious impact on the performance?
I would be really glad, if someone could explain this to me in more detail.
RealmResults, which are created by our queries, are auto-updating views into the underlying data. You can think of them as type-safe cursors and they have some of the same trade-offs, but unlike Cursors we don't copy data into a CursorWindow so there is no pagination effect. You can access the entire object graph without being worried about reaching a CursorWindow limit.
Most ORM's copy all data into Java heap memory. This can be a potentially very costly operation both time- and memory-wise + you might have a lot of additional data you don't even need. The upside to this is that you do it once, then it is really fast to access the data.
Realm on the other hand, only load the data you actually need. This includes individual fields as well. So if you have a ListView showing 1 field from 10 items we will only load those 10 fields just like an CursorAdapter. It has to load that data from native memory though which is more expensive than reading it from the Java heap.
So to answer you question. Yes, Realm works perfectly fine with ListView.
If you want to know what is faster, try it ;). Probably Realm will be faster, because their propietary system that is more optimized than SQLite, even fine-tuned. But that's my supposition
No, it mustn't have impact in performance, because we are supposing that Realm is smart enough to fetch the real data when appropiate. When using another ORM, it's the same thing, data is fetched when a pagination occurs.
I am not sure about one particular android optimization tip, that suggests to avoid unnecesary object creations. I'm unsure about thet "creation" part. In my application i started to assign several objects (context, resources etc) to activity fields with the intention to avoid calling the same get functions (getBaseContext(), getResources()) multiple times in each lifecycle.
So my question would be, when i assign those objects to activity fields, do i create new objects (and use extra space) or am i making a new reference to already created object?
When calling getBaseContext(), getResouces() you are not creating any new objects. You are obtaining objects that the Android OS has created whenever your application's process is first created.
And in regards to watching how many objects you create, I wouldn't worry about that at all unless you are creating a huge amount of objects (and I mean magnitude orders above 100s). 100s may even be too low.
A good practice would be too always keep your heap size in mind, if it you seeing it growing larger as you build your application, do your best to manage it. You can find out information about your heap size by looking at the DDMS (Dalvik Debug Monitor Server) view in your IDE.
My app needs to store data on the phone, but I'm not sure what's the more efficient method. I don't need to search through the data or anything like that. I just need to be able to save the app's current state when it closes and restore when it's back up. There is between 1mb and 10mb worth of data that will need saving.
There are basically a bunch of custom classes with data in them, and right now I have them as Serializable, and just save each class to a file. Is there any reason for me to change that to store it in SQLite?
If you where to use sqlite you could save as you go, and know that whats in the DB is pretty much uptodate if the app/activity holding the data is suddenly killed by the os. Other that that I cant see and obvious reason to use sqlite for your use-case.
Also for the sql approach you have a clear cut way to change the structure of your domain objects at a later time and to migrate the data from a old to a new version of your database. This can be done using serialized objects as-well, but then the objects needs to be duplicated, both new and old at the same time. And that to me sounds very very scary and messy, especially considering that you might need to go from version x to y, so you might end up with some pretty tricky problems if you ever need to update the domain objects.
And I can honestly not see any benefits of using the flat-file/serialized approach.
You mention in your question that the data is only meant to save the state of the app, therefore my initial response would be to keep it on the devices especially since you mention that the file size would not be much more than 10MB, which is quite reasonable.
So my answer to you would be to keep it as is on the device. If your usage of the information changes in the future, you should then reconsider this approach, but for now it's totally logical.
If you're only saving serialized classes, you could use an ORM mapper as discussed in this thread . This saves you the inconvenience of writing your own mapper and is easily extendable to new classes. Also, if your requirements change, you COULD lookup data.
The only reasons for changing your system to SQLite would be more comfort and maybe a more foolproof system. E.g. now you have to check if the file exists, parse the contents etc. and if you'd use SQLite, you don't have to verify the integrity of the data and Android also helps you a little. And you could use the data for other causes, like displaying them in a ListView.
My question is not "how?" but "should I?".
I got the app that uses access to database very often. It's money manager basically. It uses data to make calculations, draw a charts and stuff.
I was wondering if the accessing database every ocasion I need some data is efficent way, especially when database grows larger.
Is it a good idea of making class that would read all the database tables into multidimensional arrays for further use of the application? I would read it in a thread with some progress bar if necessary, and then I would have much more efficient way of accesiing datas from arrays right?
Please be gentle with me, as I could be totally wrong here.
I don't think that is a good idea. If your data grows large, you can easily run out of memory. Unless you are sure you wouldn't have that much data, or you need to read them to memory anyway. How much data you are talking about? And how much of them you actually need on the screen?