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

Maybe memory leak? #667

Closed
2youyou2 opened this issue Jan 5, 2017 · 4 comments
Closed

Maybe memory leak? #667

2youyou2 opened this issue Jan 5, 2017 · 4 comments
Labels

Comments

@2youyou2
Copy link

2youyou2 commented Jan 5, 2017

Hi,
I was building a texture packer with sharp, and i use sharp.overlayWith to pack small pictures to a big picture.
I found the way to overlay multiple pictures to a picture in #405. But it seems this will increase memory usage to a huge number and memory will not be release.

Before pack textures: memory usage 88.8 m
image

After pack textures: Memory usage 1.67 g
image

I have also disable the cache with sharp.cache(false), but it has no effect.

The example is here: https://github.com/2youyou2/electron-pitfalls
Please follow these steps:

Thanks for looking this.

@lovell
Copy link
Owner

lovell commented Jan 5, 2017

Hello, the first thing to try is to force a garbage collection.

Please can you start Electron with --js-flags="--expose-gc" then add a call to gc after processing is complete, e.g.:

        input.then(data => {
          return Sharp(data, opts)
            .toFile(dest);
        }).then(global.gc);

@lovell lovell added the question label Jan 5, 2017
@2youyou2
Copy link
Author

2youyou2 commented Jan 5, 2017

Hi, thanks for your quick reply.
Now can use node ./run.js "sharp-memory/" --js-flags='--expose-gc' to enable global.gc

I have updated the code to :

        input.then(data => {
          return Sharp(data, opts)
            .toFile(dest);
        }).then(global.gc);

But it seems still have two problems:

  • global.gc not clean all memory increase by sharp, still left about 200m. But if type global.gc in electron inspector console panel after all progress, it will clean all memory.

  • Pack textures may use too much memory, the more textures the more memory alloc. In my case, memory usage may increase to 8g. So i add global.gc to every sharp progress to test, see commit. The memory will not grow too much, but i think it may cause window not stable?

@lovell
Copy link
Owner

lovell commented Jan 5, 2017

The final input is still in scope when then(global.gc) is called so it will not yet be eligible for GC (6000 x 6000 x 4 = 144MB). When calling gc manually from the Electron inspector, input is no longer referenced so is eligible for GC.

The Node/Electron process will grab as much memory as it can before resorting to a GC. If you artificially limit the memory assigned to the process, it will trigger GC more often and free no-longer referenced Buffer data.

See #429 for a more thorough discussion of the memory usage with various versions of Node.

@lovell lovell closed this as completed Jan 5, 2017
@2youyou2
Copy link
Author

2youyou2 commented Jan 7, 2017

Thanks for your answer! I fixed the problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants