apache / apache/pouchdb

Querying a view holds the resultset in memory until you perform another query on the same view

Open
#8,589 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
17.6k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

### Issue
The `pouchdb-abstract-mapreduce` module is keeping a reference to data that should be garbage collected which results in much higher memory usage depending on how many views you define in your application

The module defines the `persistentQueues` object which stores instances of the `TaskQueue` class.
When a view is queued to be updated or a view is queried it ensures there is a `TaskQueue` instance for that view and uses it to ensure that the view only has a single operation running at any time

The `TaskQueue` class stores a promise and whenever a new operation is added it replaces the promise with a new one
```JavaScript
add(promiseFactory) {
this.promise = this.promise.catch(function () {
// just recover
}).then(function () {
return promiseFactory();
});
return this.promise;
}
```
The new promise retains the return value of `promiseFactory()` which keeps it in memory until `add()` is called again

If you updated the `finish()` method from
```JavaScript
finish() {
return this.promise;
}
```
to
```JavaScript
finish() {
const output = this.promise;
this.promise = this.promise.then(() => {});
return output;
}
```
Then calling `finish()` would release the memory

Unfortunately you would still have to update the codebase to ensure that `TaskQueue.finish()` is always called when you have queued all the tasks and want to get the return value

### Info
- Environment: browser
- Platform: Chrome
- Adapter: idb
- Server: None (exclusively local data)

### Reproduce
- Create a design document which has a view
- Query the view
- Drop the return value
- Take a heap snapshot in the memory inspector of Chrome developer tools

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.