matcauthon
matcauthon

Reputation: 2261

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
...

Upvotes: 5

Views: 772

Answers (1)

noamt
noamt

Reputation: 7825

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.

Upvotes: 1

Related Questions