Description of these two methods in the Oracle API:
System.exit() : "Terminates the currently running Java Virtual Machine."
System.nanoTime() : "The value returned represents nanoseconds since some fixed but arbitrary origin time. The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin."
Based on the above, I was curious to see what result the nanoTime() method returns. I converted the result to show the elapsed time in minutes. The result was 64. I guess it was calculated from the moment my computer was turned on, since it had been on for about an hour.
I then ran another program. In it I used System.exit(), which is supposedly kills the current instance of the JVM.
I then ran the first program again, to measure the elapsed time with nanoTime() method .
My expectation was that this time I should get zero minutes, since I had "killed" the previous instance of the JVM, so another instance should probably be running now, and I figured that this other instance would surely use a different origin point as the basis for the measure of nanoseconds.
But it didn't, the result was 67 minutes, so the same origin point was used by JVM now too.
How is this possible?
Is the other JVM instance also based on the same origin point? (Maybe JVM borrows the origin point from Windows?)
Or am I misunderstanding what means "another instance of the JVM". If a Java program ends, and I run another program, doesn't that mean another JVM instance?
Question about System.exit() and System.nanoTime() and JVM instances
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Lisa
31 January 2022, 15:39useful
hihihi, when I run this
then I get 306.. that's approx. the days I own my new notebook. Not sure where nanoTime gets it arbitrary origin from but as they say it's arbitrary and you can not rely on what it will be when you bring up the JVM next time. Maybe some value set in the bios or system uptime or... . And depending on the JVM implementation it may vary and even be zero as you expected it to be. My suspicion has also been confirmed, it is a native method. Looking at the sources won't bring enlightment. +1
Gellert Varga
31 January 2022, 15:48
- OK, this is arbitrary:)
- Unfortunately, I don't know what the native method is.
- In the example you gave, didn't you miss another division by 60 as well?...
0
Lisa
31 January 2022, 15:55useful
jeez... you're right. Looks like my notebook is younger than I thought 🤪😜 never expected such a result
And a native method is one implemented in another language than Java (C, C++ ...)
+1
Gellert Varga
31 January 2022, 15:58
:)
And about the JVM instances, am I right in thinking:
if a Java program ends, and I start to run another program, does it mean a new JVM instance?
0
Lisa
31 January 2022, 16:09useful
That's how I understand it, too. And afaik the JVM ends also if the last daemon process stops or you kill its process externally (of course 🙃 ). A newly started Java program would mean a new JVM instance.
+1
Gellert Varga
3 February 2022, 09:41
I've never had a notebook, so I don't know what the usual way of using it is -- maybe it doesn't need to be switched off as regularly as a desktop computer.
Is it possible that your notebook has been running continuously for 5 days (306 hours) without being turned off, and the nanoTime method on your notebook counted the time from the moment you turn it on?
0
Lisa
3 February 2022, 18:11
Yo, I never turn it off. It usually is in standby. Just when there are updates that require a reboot I restart it. So this is totally possible...
0