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

Memory leak for Signal#select? #164

Open
kevinresol opened this issue Sep 6, 2021 · 2 comments
Open

Memory leak for Signal#select? #164

kevinresol opened this issue Sep 6, 2021 · 2 comments

Comments

@kevinresol
Copy link
Member

kevinresol commented Sep 6, 2021

v2.0.2
The following code will go OOM in a few seconds.

using tink.CoreApi;

class Main {
	static final root:Signal<Int> = Signal.trigger();

	static function main() {
		new haxe.Timer(100).run = () -> {
			for (i in 0...1000000)
				root.select(v -> v == 0 ? Some(v) : None);

			trace('heapUsed: ' + Std.int(js.Node.process.memoryUsage().heapUsed / 1024 / 1024) + 'MB');
		}
	}
}

In contrast, if I do it manually with new Signal(cb -> root.handle(v -> if(v == 0) cb(v))); it seems to work fine.

I wonder if I am using it incorrectly or I wrongly expected it to be lazy/suspendable?

@kevinresol
Copy link
Member Author

kevinresol commented Sep 7, 2021

I noticed that .select is build on top of .over which links the derived signal to the root one. So technically this is not a leak but the memory is held by the root signal. I wonder if that is only useful when the root signal is more short-lived than the derived ones?

In my use case, the root signal is almost static and will stay alive throughout the app's lifespan. That seems to hold alive all the derived signals, causing the leak.

kevinresol added a commit to why-haxe/why-dbus that referenced this issue Sep 9, 2021
kevinresol added a commit to why-haxe/why-bluez that referenced this issue Sep 9, 2021
@kevinresol
Copy link
Member Author

Should WeakRef be used for the ondispose hook so it doesn't keep the derived signal alive?

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

1 participant