Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage of Grails GORM methods

I'm wondering how grails handles memory usage and loading (fetching) of domain objects by GORM methods like:

findAllWhere
findAllBy
list
...
  • Are they fully loaded (respectively their proxies) into memory?
  • If I traverse them with each/every/any are they backed by an iterator loading them lazily?
  • Should I prefer createCriteria() {...}.scroll() for better memory usage?
like image 646
matcauthon Avatar asked Nov 13 '22 08:11

matcauthon


1 Answers

Assuming that we ignore the different behavior of each type of DB driver GORM uses, we can find the answers in the code and the documentation.

The dynamic finders are provided to Domain classes by org.grails.datastore.gorm.GormStaticApi and the finder classes within the org.grails.datastore.gorm.finders package.

Reviewing these classes we are able to see that queries which return multiple results are always assembled as a DetachedCriteria and always invoke the criteria.list() method; this means that the whole result batch is assembled and held in memory. Traversing the results with Groovy's collection methods won't make any difference because you're essentially invoking those methods on the returned result list.

As to the question "How much of the result Domain is loaded?" - this depends on the composition of the domain, but you may assume that the fields of the Domain are loaded and that any associations are by default lazy.

In scenarios that require better memory usage you can certainly self-compose the criteria in conjunction with result projections and use scroll (note that this feature depends on the type of DB).

In extreme cases I even bypass GORM and work directly with the DB driver.

like image 79
noamt Avatar answered Nov 15 '22 05:11

noamt