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

Unloading library - V8 #45

Closed
Vshnv opened this issue Jun 7, 2021 · 11 comments · Fixed by #47
Closed

Unloading library - V8 #45

Vshnv opened this issue Jun 7, 2021 · 11 comments · Fixed by #47
Labels
enhancement New feature or request

Comments

@Vshnv
Copy link

Vshnv commented Jun 7, 2021

Description:
We are using the library in an "extension" application which could possibly be reloaded when the user wants.

But this seems to cause issues since the native library remains loaded on the old classloader.

Is there possibly any concise way provided to properly stop or unload the library?
I have tried to remove all references to the classloader that loads javet and classes loaded by that classloader, but it seems calling gc and runFinalization still doesnt unload the library/classloader.

@caoccao
Copy link
Owner

caoccao commented Jun 8, 2021

The Javet implementation is compatible with unloading the library, but I decided not to expose that capability.

Performance

Loading the library has to be single threaded which requires singleton pattern to be in place. That is how Javet loads Node.js instance. However, that comes with a noticeable performance overhead because Javet has to a) deploy 10-30MB binary to temp directory; b) load that 10-30MB binary to JVM; 3) initialize the instance.

To get rid of that overhead, Javet loads the V8 instance in the default classloader which also forbids the library to be unloaded.

Suggestion

You may load Javet in a new classloader. Once that classloader becomes orphan, JVM will unload Javet.

@Vshnv
Copy link
Author

Vshnv commented Jun 8, 2021

I actually am already loading javet onto a separate classloader. I tried removing all references and trying to force gc, it seems to not be able to release the classloader altho there are no references to anything loaded by it or the classloader itself held in the application.
The only library being loaded in the isolated classloader is javet.
Do you have any ideas on whether this might be due to javet? Like by shutdown hooks or similar that can leak its reference?

@caoccao
Copy link
Owner

caoccao commented Jun 9, 2021

Node.js is not designed to be reloaded. I'm trying to hack it.

V8 seems to be compatible with that. I'm testing it.

Please stay tuned. It may come true in the next release. In the meanwhile, if you could post you PoC code, that would be very helpful.

@Vshnv
Copy link
Author

Vshnv commented Jun 9, 2021

Alright. Thats great. Ill send you the PoC in the morning.

@caoccao
Copy link
Owner

caoccao commented Jun 9, 2021

Good news. That feature is included in this commit. Please wait for the next release.

By the way, could you check this discussion out?

@caoccao caoccao added the enhancement New feature or request label Jun 9, 2021
@Vshnv
Copy link
Author

Vshnv commented Jun 9, 2021

Oh, that's awesome! Is there any ETA for the next release?

Also, yea sure, Ill check out that discussion.

@caoccao
Copy link
Owner

caoccao commented Jun 10, 2021

I plan to release it next week.

@Vshnv
Copy link
Author

Vshnv commented Jun 11, 2021

Would a earlier patch release be possible? A week might really push our timeline

@caoccao
Copy link
Owner

caoccao commented Jun 11, 2021

Next Monday is public holiday, how about next Tuesday? One of the reasons is: I found a serious bug which might crush JVM. I'm still fixing it.

@Vshnv
Copy link
Author

Vshnv commented Jun 11, 2021

Oh I see. Alrighty. Thank you.

@caoccao caoccao linked a pull request Jun 15, 2021 that will close this issue
@Vshnv
Copy link
Author

Vshnv commented Jun 17, 2021

Thank you!

@Vshnv Vshnv closed this as completed Jun 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants