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

Crashes at 300+ Frames #37

Open
JoshieGemFinder opened this issue Sep 1, 2021 · 2 comments
Open

Crashes at 300+ Frames #37

JoshieGemFinder opened this issue Sep 1, 2021 · 2 comments

Comments

@JoshieGemFinder
Copy link

JoshieGemFinder commented Sep 1, 2021

I've written a looping animation in a canvas, and i'm trying to convert it into a gif by adding a frame every time I draw to the canvas, my animation is 360 frames long, but as soon as it reaches 300 frames, my webpage crashes, i've attached the error
image

@joshi1983
Copy link

joshi1983 commented May 20, 2023

I ran into likely the same bug. I get the same error message after trying to encode 201 frames of a 512 by 512 resolution animation but I think the more critical detail is how large the compressed data is getting. It crashed shortly after the "outs" variable reached roughly 50 million elements. It is a ByteArray as defined in GIFEncoder.js which is repeatedly given more bytes using writeByte and writeBytes in the LZWEncoder.js. Eventually, the browser freezes for a few seconds before showing the error message above. The exact line of crash was outs.writeBytes(accum, 0, a_count) in the flush_char function of LZWEncoder.js while GIFEncoder is calling output on a pixel indirectly from the addFrame function. Immediately before that final call to writeBytes, outs = ByteArray { bin: Array(50139333)} according to Edge's debugger tools. I tried to encode the same animation in both Microsoft Edge and Google Chrome and it crashed on the same frame. I'm testing with a laptop that has 8GB of RAM.

I want to help you reproduce the problem so I'm sharing a prototype that tries to encode and download a gif as soon as you open it. When I open it in Edge, it encodes many frames but eventually crashes within a minute or so.
My prototype references a minified version but the problem is reproduced if you download and reference using script tags in the HTML file:
jsgif/LZWEncoder.js
jsgif/NeuQuant.js
jsgif/GIFEncoder.js
gifOutputBufferTooLarge.zip

Here is what I see after the crash in Microsoft Edge Version 113.0.1774.42 (Official build) (64-bit) in Windows 10:
image

I understand if the encoder runs out of RAM at some point but 50MB seems a little small for a maximum sized gif. Even if ByteArray can't expand beyond 50 million elements or so, a JavaScript Error or exception would be FAR more graceful than letting the browser tab completely crash. If GIFEncoder needs to be optimized for memory, a typed Array of 8-bit elements such as Uint8Array should use a small fraction of the memory.

@joshi1983
Copy link

joshi1983 commented Jul 14, 2023

Pull request #40 is open to tackle a large part of this problem by optimizing the data structure maintaining the encoded GIF data. I have it merged in my fork so if you want a more updated and stable jsgif, try my fork. antimatter15's repo looks like it has been dead for about 6 years.

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

No branches or pull requests

2 participants