Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java program run without its file?

Tags:

java

I'm fairly new at this stuff, but essentially: there are programs and there are processes. A program is a file that spawns a process, when executed.

You can't delete a program if there is a process still associated to it. The process needs to be killed first.

This seems to be the case for Java programs too. However I'm curious as to why - isn't the entire thing loaded into the JVM anyway?

like image 546
Voldemort Avatar asked Jun 09 '14 22:06

Voldemort


2 Answers

"Deleted file" involves som OS-semantics. Under Unix/Linux a file may be deleted and all open file handles stay valid. When the last open file handle vanishes, the space occupied by the deleted file is returned to the pool of free space.

Under Windows there may be other mechanisms.

like image 126
blafasel Avatar answered Oct 02 '22 06:10

blafasel


The JVM works as a Just-In-Time (JIT) compiler. There are many sources of information on JIT compilation, but basically as a java program is running it will encounter parts of the program that are needed, these pieces of the program are in .class files. These .class files is just an intermediate form of Java code (it's not quite Java code, but not quite machine code, yet). Obviously, compiling at runtime (JIT) takes resources (CPU cycles) and, thus, time. So, the JVM only loads pieces of the program that it needs to minimize wasted CPU cycles.

But yes, your understanding of process/programs is correct. To sum up: A process is a running instance of a program. This running program, then can spawn even more processes or threads to perform work.

like image 35
Rich E Avatar answered Oct 02 '22 06:10

Rich E