'react-native app uses to much memory, as time increase

I am having an interesting issue here with my app(it takes to much memory).

I do not understand why, I could see on my phone that it uses about 1GB and I think that it will still increase in the future.

If my app stays open for two days it crash.

There is two things which I have which may causes the problem and I am still not sure as I am unable to see which one is causing it.

1- utils-decorators I am using it to cache some data from an api with custom cache specified, see below

import { memoizeAsync, Cache, memoize } from 'utils-decorators';

  @memoizeAsync({
    expirationTimeMs: 1000 * 60 * 60,
    cache: serviceTempData.HttpClientCache
  })
  async getHtml(url: string, isImage?: boolean): Promise<HTMLElement>{
      ....  
  }

Here is how the custom cache look like.

export default class CustomCache implements Cache<any> {
    private loadedFiles = new Map<string, any>();
    private maxItems: number;
    private clearSize: number;
    constructor(maxItems?: number, clearSize?: number) {
        this.maxItems = maxItems ?? 1000;
        this.clearSize = clearSize ?? 500;
    }

    keys(){
        var result = [] as string[];
        for(var x of this.loadedFiles.keys())
            result.push(x);
        return result;
    }

    clearAll() {
        this.loadedFiles.clear();
    }

    validate() {
        var counter = this.clearSize;
        if (this.loadedFiles.size > this.maxItems)
            for (var key of this.loadedFiles.keys()) {
                counter--;
                this.loadedFiles.delete(key);
                if (counter <= 0)
                    break;
            }
    }

    set = (key: string, parameter: any) => {
        this.validate();
        this.loadedFiles.set(key, parameter);
    }
    get = (key: string) => {
        return this.loadedFiles.get(key);
    }
    delete = (key: string) => {
        if (this.loadedFiles.has(key))
            this.loadedFiles.delete(key)
    }
    has = (key: string) => {
        return this.loadedFiles.has(key)
    }
}

I don't see why this is causing a problem but you may see something I don't.

2- Using expo.sqlight

I have build and ORM expo-sqlite-wrapper That is build around expo-sqlight, Now I am even more so not sure if this could cause the problem, as it may be loaded in the memory.

Note: check the wrapper a little to understand how it work Please do till what you think.

Update

Yes I forgot to mention that i also have a background service the running. Check it out.

    while (BackgroundService.isRunning()) {
            const dData = [...downloads.filter(x => serviceOperations.DownloadTasks[x.novel] === undefined)]
            if (dData.length > 0) {
                await dData.asyncForeach(async x => {
                    console.log("Downloading" + x.novel);
                    serviceOperations.DownloadTasks[x.novel] = x;
                    var parser = BackGroundOperator.includedParser.find(f => x.parserName == f.name);
                    if (parser) {
                        var novel = await parser.getNovel(x.novel)
                        parser.downloadNovel(novel, () => {
                            if (BackGroundOperator.onDownloadStart && BackGroundOperator.onDownloadStart[x.novel] != undefined) {
                                BackGroundOperator.onDownloadStart[x.novel]();
                                delete BackGroundOperator.onDownloadStart[x.novel];
                            }

                        });
                    }
                });
            }

            if (!workingOnMessages)
                validateMessages();

            if (!BackGroundOperator.serviceData && data) {
                console.log("Playing")
                BackGroundOperator.serviceData = data;
                AudioService.task(BackGroundOperator.includedParser);
            } else if (!data)
                BackGroundOperator.serviceData = undefined;


            if (!isWorking)
                checkRemovableFiles();

            if (!workingOnSessionProgress)
                validateSession();

            if (__DEV__ && pinger % 100 === 0) {
                console.log("Service is still alive")
                pinger = 0;
            }

            await HttpClient.wait(4000);
            if (__DEV__)
                pinger++
        }

This may also couse a problem, What do you think

Found Problem

Ok after toooo much work I found the problem in a components. The app is an ebook app and I have to save the scroll progress, and I do that onScroll operation. The issue was that I make to many db update operation, which dose not gives time to sqlight to free the resources and it stay in memory.

And that is why the memory increases after some times.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source