J2V8 icon indicating copy to clipboard operation
J2V8 copied to clipboard

Memory leak

Open javabully opened this issue 6 years ago • 13 comments

We have an application that brings up several short lived J2V8 engines a second and over a couple of days, the java process consumes more and more memory until the server becomes unresponsive. Restarting the Java process fixes the issue and frees the memory. We have monitored the heap, which looks fine, so believe there is a memory leak in either the JNI layer or the V8 instances themselves. The leak can be easily demonstrated with the following code:

public static void main(String[] args) {		
	while(true) {		
		try (V8 v8 = V8.createV8Runtime()) {}			
	}
}

During a run of the test I dumped the memory stats periodically.

The first memory stats just after Java launch:

Thu Sep 12 09:14:39 UTC 2019

              total        used        free      shared  buff/cache   available
Mem:        2002820      797368      515472        5344      689980     1045256
The last memory stats before the server became unresponsive:

Thu Sep 12 11:34:21 UTC 2019

              total        used        free      shared  buff/cache   available
Mem:        2002820     1898052       64968        5148       39800        7040
The memory stats after killing the Java process:
              total        used        free      shared  buff/cache   available
Mem:        2002820      834248      923900        5212      244672     1019236

Here are some details on the server OS and software:

OS Details

NAME="Ubuntu" VERSION="18.04.2 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.2 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic

J2V8 Build Output

J2V8 Version [INFO] --------------< com.eclipsesource.j2v8:j2v8_linux_x86_64 >-------------- [INFO] Building j2v8_linux_x86_64 5.1.0-SNAPSHOT

V8 Details

Version 5.4.500

javabully avatar Sep 12 '19 12:09 javabully

Even worse, using more than one runtime sometimes causes segfaults with our application. We've opted to re-use runtimes for now. Maybe you'll have more luck with a newer version.

gleno avatar Dec 30 '19 13:12 gleno

We face the same issue too. Can you please help out?

rohit5ram avatar Dec 10 '20 15:12 rohit5ram

We cannot reuse runtime instance either. In multi-threaded environment its hard to control the thread accessing the runtime instance.

rohit5ram avatar Dec 10 '20 15:12 rohit5ram

I could not fix this issue so we side stepped it by using GraalVM. The javascript engine has given us better performance than J2V8 and has much more up to date ECMAScript compliance. We are using in it a highly threaded environment and have seen no leaks. I can highly recommend it.

javabully avatar Dec 10 '20 15:12 javabully

GraalVM doesn’t work with Android though, right?

On Thu, 10. Dec 2020 at 17:41 javabully [email protected] wrote:

I could not fix this issue so we side stepped it by using GraalVM. The javascript engine has given us better performance than J2V8 and has much more up to date ECMAScript compliance. We are using in it a highly threaded environment and have seen no leaks. I can highly recommend it.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipsesource/J2V8/issues/476#issuecomment-742600956, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWUGQRHFGH5TPQE3SXBBQDSUDT3ZANCNFSM4IWDB7UQ .

gleno avatar Dec 10 '20 15:12 gleno

You can try JavaScriptCore as an alternative on Android.

Not that there are not downsides there, including that it's a C/C++ API that you need to wrap yourself for Java (unless there's an existing wrapper I'm unaware of). That said, such a wrapper isn't that hard to produce.

I like the notion of J2V8, but:

  1. V8 libraries aren't readily available (and are really painful to build)
  2. If you actually need a C/C++ API at any point, JavaScriptCore's API is straightforward

I've now done projects with both on Android. Both worked. (I didn't notice the memory leak noted here as my J2V8 runtimes while multiple are fairly long-lived.)

JessHolle avatar Dec 10 '20 15:12 JessHolle

Currently, you have to close/release the V8 instance if you have created yourself. It's also mentioned in the Javadocs here and here.

We also have upgraded the V8 version (V8 8.3.110) in the latest J2V8 Android releases.

ahmadov avatar Dec 10 '20 16:12 ahmadov

Elmi, any plans for making that work on a Mac? We run latest J2V8 on Android, and test on Macs. But we haven’t been able to run latest J2V8 on mac, so we are kind of stuck on ES2015.

Cheers.

On Thu, 10. Dec 2020 at 18:36 Elmi Ahmadov [email protected] wrote:

Currently, you have to close/release the V8 instance if you have created yourself. It's also mentioned in the Javadocs here https://github.com/eclipsesource/J2V8/blob/master/src/main/java/com/eclipsesource/v8/V8Object.java#L25 and here https://github.com/eclipsesource/J2V8/blob/master/src/main/java/com/eclipsesource/v8/V8Value.java#L13 .

We also have upgraded the V8 version (V8 8.3.110) in the latest J2V8 Android releases.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipsesource/J2V8/issues/476#issuecomment-742636299, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWUGQWVGKTNYKQRWFCKIMDSUD2KHANCNFSM4IWDB7UQ .

gleno avatar Dec 10 '20 16:12 gleno

Currently, I can mention about any plan but I have successfully built J2V8 for Android, Linux and macOS (PR) some time ago. I guess Android and Linux builds should still be relevant. At least, the last Github Action build was successful for them.

You can build on your local machine either with pre-build V8 library (here) or you can also build V8 locally.

If you want to build V8 locally, use the following command:

python build.py -t macos -a x64 --docker v8 j2v8cmake j2v8jni j2v8cpp j2v8optimize j2v8java j2v8test

Maybe you can apply changes for macOS locally and try.

ahmadov avatar Dec 10 '20 16:12 ahmadov

Thanks, will give it a shot over the weekend. Cheers.

On Thu, 10. Dec 2020 at 18:49 Elmi Ahmadov [email protected] wrote:

Currently, I can mention about any plan but I have successfully built J2V8 for Android, Linux and macOS (PR https://github.com/eclipsesource/J2V8/pull/489) some time ago. I guess Android and Linux builds still should be relevant. At least, the last Github Action build was successful for them.

You can build on your local machine either with pre-build V8 library (here https://github.com/eclipsesource/J2V8/blob/master/.github/workflows/main.yml#L16) or you can also build V8 locally.

If you want to build V8 locally, use the following command:

python build.py -t linux -a x64 --docker j2v8cmake j2v8jni j2v8cpp j2v8optimize j2v8java j2v8test

Maybe you can apply changes for macOS locally and try.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/eclipsesource/J2V8/issues/476#issuecomment-742646513, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAWUGQTAVOLCJIPHZOCCYZ3SUD33JANCNFSM4IWDB7UQ .

gleno avatar Dec 10 '20 16:12 gleno

@ahmadov Yeah, it was possible to build for a Mac. Not really super intuitive, but possible. Thanks for your help.

gleno avatar Dec 11 '20 17:12 gleno

j2v8-6.2.0-mac.zip

Here are the binaries, if someone wants them.

gleno avatar Dec 11 '20 17:12 gleno

@gleno, good to hear!

ahmadov avatar Dec 11 '20 19:12 ahmadov