4

I have a method that looks like this

public Dao<ModelStore, Integer> getDaoStore() throws SQLException {
    return BaseDaoImpl.createDao(getConnectionSource(), ModelStore.class);
}

when i call getDaoStore it is quite a lengthy process. In my log's i can see that the GC runs after every call to this, so I'm guessing there's a lot going on with this call.

Is there a way to speed this up?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pzanno
  • 2,185
  • 2
  • 20
  • 23
  • What does this call return? Thousends of objects from the database? In that case you should limit the amout of data loaded from the database. – Gamlor Mar 25 '11 at 12:07
  • @Gamlor this call returns a DAO object that will act on a given model to to various database task, such as insert, update, query, etc... – Pzanno Mar 25 '11 at 14:24
  • Hey @Pzanno. I've updated my response below with the real reason why the `createDao` is so slow under Android. – Gray Sep 15 '11 at 16:08
  • @Gray, thanks for the update. I'm glad you were able to find the bottom of this issue. I'll keep my eye on it and hope for a fix. – Pzanno Sep 15 '11 at 17:37

1 Answers1

8

A deep examination of Android-land has revealed that because of a gross Method.equals() method, annotations under Android are very slow and extremely GC intensive. We added table configuration files in version 4.26 that bypass this and make ORMLite start much, much faster. See this question and this thread on the mailing list.

We continue to improve annotation speeds. See also: ORMLite poor performance on Android?


DAO creation is a relatively expensive process. ORMLite creates a data representation of both the class and the fields in the class and builds a number of other utility classes that help with the various DAO functionality. You should make sure that you call the createDao method once per invocation. I assume this is under Android @Pzanno?

In 4.16 we added a DaoManager whose job it is to cache the Dao classes and this was improved in version 4.20. You should then always use it to create your Daos. Something like the following code is recommended:

private Dao<ModelStore, Integer> modelStoreDao = null;
...

public Dao<ModelStore, Integer> getDaoStore() throws SQLException {
    if (modelStoreDao == null) {
        modelStoreDao = DaoManager.createDao(getConnectionSource(),
            ModelStore.class);
    }
    return modelStoreDao;
}

Hope this helps. A memory audit of ORMLite is probably also in order. It's been a while since I looked at it's consumption.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
  • @Gray Thanks! Now in your example you're also caching the dao your self as a private member of a class. Is this needed since you said the DaoManager will cache it for you? – Pzanno Mar 25 '11 at 14:25
  • @Gray Also, if it is best to cache myself, would you recommend making it a static variable? or would that consume too much memory? – Pzanno Mar 25 '11 at 14:34
  • @Gray I just tested out the new DaoManager class from 4.16 and I can't say I noticed the caching that it's supposed to do. Also in my Android logs I'm getting this printed out a bunch `03-25 15:21:33.022: INFO/dalvikvm(8157): Could not find method java.sql.SQLException., referenced from method com.j256.ormlite.dao.DaoManager.createDao ` during the create tables with the TableUtils class. – Pzanno Mar 25 '11 at 15:23
  • Static is fine. The DaoManager will cache it but it has to create a key object each time it is looked up so saving it in a variable locally is a bit more efficient. You would only have noticed it if you were creating the Dao more than once. Not sure about why you're not finding SQLException class. – Gray Mar 25 '11 at 17:39
  • This turns out to be a bug in 4.16 when we used some java 1.6 calls by accident. 4.17 fixed that problem. – Gray Mar 29 '11 at 15:42
  • @Gray great, are you referring to the log report i pasted in not being able to find the SQLException? – Pzanno Mar 29 '11 at 21:02
  • Yes. See this thread on the mailing list: http://groups.google.com/group/ormlite-user/browse_thread/thread/8b6fbfae413650c6 – Gray Mar 29 '11 at 22:56
  • Just published 4.20 Pzanno. It does a better job of using cached DAOs if you have a lot of foreign and foreignCollection relationships. – Gray May 02 '11 at 14:01