y-redis icon indicating copy to clipboard operation
y-redis copied to clipboard

fix: memory leak in worker component

Open catssay opened this issue 1 year ago • 2 comments

Fix memory leak in the worker component: ydoc instances were not properly released after applying updates and writing to storage.

catssay avatar Dec 25 '24 06:12 catssay

Hi @catssay,

Does this fix your memory leak problem? I'm curious because according:

https://github.com/yjs/y-redis/blob/61a13e4bcac85373bba24375cdacfba05e77f697/src/api.js#L295 https://github.com/yjs/y-redis/blob/61a13e4bcac85373bba24375cdacfba05e77f697/src/api.js#L300

We still need to pass ydoc to updateCallback & persistDoc, and afterTransaction will be called right after the transaction:

https://docs.yjs.dev/api/y.doc#event-handler

Does that mean releasing yodc too early?

EastSun5566 avatar Jan 13 '25 09:01 EastSun5566

Hi @catssay,

Does this fix your memory leak problem? I'm curious because according:

https://github.com/yjs/y-redis/blob/61a13e4bcac85373bba24375cdacfba05e77f697/src/api.js#L295

https://github.com/yjs/y-redis/blob/61a13e4bcac85373bba24375cdacfba05e77f697/src/api.js#L300

We still need to pass ydoc to updateCallback & persistDoc, and afterTransaction will be called right after the transaction:

https://docs.yjs.dev/api/y.doc#event-handler

Does that mean releasing yodc too early?

Thank you for your question! Yes, this does address the memory leak issue. The memory leak happens because ydoc cannot be released after being consumed in each cycle. From my observation, the event handler does not seem to be used further after the release.

It might be worth considering moving the destroy operation to occur after the store operation. However, based on my understanding, ydoc should remain unchanged once all updates from Redis have been applied. If we were to move the destroy operation outside of getDoc, it would require any caller of getDoc to take responsibility for releasing ydoc after its use. So I put the destroy operation after applying updates.

catssay avatar Jan 22 '25 15:01 catssay