Skip to main content

Does CVM rely on the process exiting to release resources?

5 replies [Last post]
jacobsq
Offline
Joined: 2007-11-21
Points: 0

Problem as the title.

CDC --> System.java --->function Sytem.exit()
The function commented "If we have a process model,System.exit() must rely on the process exiting to release resources.".So does it mean System.exit() must exit process to release the resources?

CDC/src/share/javavm/runtime/jni_impl.c : CVMjniDestroyJavaVM() line 4783 comment "If we have a process model, then we cannot destroy the VM data structures, otherwise the daemon threads may crash."

Does it mean even if CVMDestroyJavaVM() is called ,still some resources are not released??

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
xyzzy
Offline
Joined: 2006-08-30
Points: 0

> Problem as the title.
>
> CDC --> System.java --->function Sytem.exit()
> The function commented "[b]If we have a process
> model,System.exit() must rely on the process exiting
> to release resources.[/b]".So does it mean
> System.exit() must exit process to release the
> resources?
>
> CDC/src/share/javavm/runtime/jni_impl.c :
> CVMjniDestroyJavaVM() line 4783 comment [b]"If we
> have a process model, then we cannot destroy the VM
> data structures, otherwise the daemon threads may
> crash."[/b]
>
> Does it mean even if CVMDestroyJavaVM() is called
> ,still some resources are not released??

That is correct. See the JNI spec description of DestroyJavaVM.
VM unloading is not supported. We do have an unsupported
-XfullShutdown option that tries to free up all resources, but it
requires that all your daemon threads exit first.

jacobsq
Offline
Joined: 2007-11-21
Points: 0

The problem has confused me for a long time.
I want to start CVM in thread in a process.The process also has some other thread.But when CVM exit,it will call C library function "exit(status)" force the process to exit regardless of other thread.

xyzzy
Offline
Joined: 2006-08-30
Points: 0

Yes, if your app calls System.exit(), then yes we exit the process, just like Java SE
does. To avoid this, you can avoid calling System.exit() and instead let your
main() method return.

If you really want System.exit() to return control to your thread, you can try
the unsupported -XsafeExit option (see ansi_java_md.c). The Java SE APIs
that Java ME is based on mostly assume processes. The -XsafeExit and
-XfullShutdown were early attempts to support non-process models. It would
be nice if a future spec added better support for this.

jacobsq
Offline
Joined: 2007-11-21
Points: 0

I have tried the -XsafeExit option and trace the code.In fact, the option is only useful when the java main() thread call System.exit(), because the code will check if it is main thread,if not, it will call Shutdown.java function Shutdown.exit(). if it is main thread, the code will use C function longjmp() to jmup back to ansiMain and return driectly even not call DestroyJavaVM().

There is an interesting example, if you write a hello.java like this:
class Mythread extends Thread {
public void run() {
while(1);
}
......
}

class hello {
public static void main() {
Mythread t = new Mythread();
t.start();
System.exit(0);
}
}

if use the -XsafeExit option,the java app will not exit forever. before call the safeExit() function in ansi_java_md.c, it will call DestroyJavaVM(), the function will call Shutdown.waitalluserthreadexit() to wait all the user thread exit, so DestroyJavaVM() will wait forever,then the java app will wait forever.

xyzzy
Offline
Joined: 2006-08-30
Points: 0

Correct. Threads must exit voluntarily.