Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak when selecting a large number of rows #1302

Closed
yassenb opened this issue Jul 26, 2021 · 4 comments
Closed

Memory leak when selecting a large number of rows #1302

yassenb opened this issue Jul 26, 2021 · 4 comments

Comments

@yassenb
Copy link

yassenb commented Jul 26, 2021

Doing a simple EntityClass.find results in a memory leak (or perhaps more correctly bloat) when iterating through millions of records. The culprit is Transaction.entityCache which only gets filled and never freed during the iteration. As a result I'm forced to use a workaround like:

EntityClass.find { ... }.chunked(100_000).forEach {
    // read the chunk
    entityCache.data.clear()
}

instead of the simple

EntityClass.find { ... }.forEach {
    // read the entity
}

There should be a way to disable the cache or configure its size so that it doesn't grow forever.

@yassenb
Copy link
Author

yassenb commented Aug 3, 2021

Interesting that you would label the ability to work with real life databases with more than a few thousand records in them without crashing due to running out of memory an "enhancement".

@bdshadow
Copy link

I just guess from the reply of @Tapac here that it was supposed that cache would be used for "mostly dictionary-like" entities, which contain a rather limited amount of data. That's why it is an enhancement/new feature to make it possible to cache big amounts of data without requiring user to clear cache him/herself

@yassenb
Copy link
Author

yassenb commented Aug 24, 2021

It may be "supposed" that caching would be used in certain scenarios but at the moment it's used everywhere and there's no way to disable it. That's what this issue is about.
As per "requiring user to clear cache him/herself" - the workaround I've found is hardly public API. I found it only after reading the source code. What's more, the workaround splits the sequence/iterable in chunks which doesn't preserve the original behaviour which is a lot more convenient and optimal.

Tapac added a commit that referenced this issue Sep 17, 2021
EntityCache.maxEntitiesToStore and DatabaseConfig.maxEntitiesToStoreInCachePerEntity params introduced
@Tapac
Copy link
Contributor

Tapac commented Sep 17, 2021

New maxEntitiesToStore param on EntityCache will be available in the next release along with possibility to define it globally via DatabaseConfig.maxEntitiesToStoreInCachePerEntity

@Tapac Tapac closed this as completed Sep 17, 2021
SchweinchenFuntik pushed a commit to SchweinchenFuntik/Exposed that referenced this issue Oct 23, 2021
EntityCache.maxEntitiesToStore and DatabaseConfig.maxEntitiesToStoreInCachePerEntity params introduced
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants