Process vs Program
BeginnerA program is a passive, static set of instructions stored on disk; a process is a program in active execution with its own memory space, OS resources, and lifecycle.
Overview
A program is just a file on disk — a sequence of instructions and data (a .class file, a .jar, an .exe). It has no existence at runtime until the OS loads it. A process is the living instance of a program: the OS allocates a virtual address space (heap, stack, code segment, data segment), assigns a PID, and begins execution. Multiple processes can run the same program simultaneously — each Chrome tab is a separate process running the same Chrome binary. Process isolation means one process crashing cannot corrupt another's memory. In Java, the JVM itself is an OS process, and within it, your application code runs. You can launch multiple JVM processes from the same .jar file.
Program vs Process: Static vs Dynamic
A program is like a recipe in a cookbook — it is inert until someone follows it. A process is like actually cooking: it consumes resources (memory, CPU, I/O), has state (what ingredients have been added so far), and can interact with the environment. The same recipe (program) can be followed by multiple cooks (processes) simultaneously, each in their own kitchen (address space).
// A Java .jar is a program (static bytecode on disk)
// The JVM process is what brings it to life
// Check the current JVM process details
ProcessHandle self = ProcessHandle.current();
System.out.println("JVM Process PID: " + self.pid());
System.out.println("Command: " + self.info().command().orElse("unknown"));
System.out.println("Start time: " + self.info().startInstant().orElse(null));
System.out.println("Total CPU duration: " + self.info().totalCpuDuration().orElse(Duration.ZERO));
// Memory segments of the JVM process (via Runtime)
Runtime rt = Runtime.getRuntime();
long heapUsed = rt.totalMemory() - rt.freeMemory();
long heapMax = rt.maxMemory();
System.out.printf("Heap used: %d MB / %d MB max%n",
heapUsed / 1_048_576, heapMax / 1_048_576);
// List all JVM processes on this machine (Java 9+)
ProcessHandle.allProcesses()
.filter(ph -> ph.info().command()
.map(cmd -> cmd.contains("java")).orElse(false))
.forEach(ph -> System.out.println("JVM process: " + ph.pid()
+ " — " + ph.info().command().orElse("?")));Multiple Processes from the Same Program
You can launch the same .jar as multiple independent OS processes. Each gets its own heap, stack, and PID. They are completely isolated — a crash in one does not affect the other. This is the basis of microservices: multiple instances of the same artifact running as separate processes, each with its own port and state.
// Launching two instances of the same JAR as separate OS processes
String javaHome = System.getProperty("java.home");
String javaExec = javaHome + "/bin/java";
// Process 1: instance A of our application
ProcessBuilder pbA = new ProcessBuilder(javaExec, "-jar", "app.jar", "--port=8080");
pbA.environment().put("INSTANCE_ID", "A");
Process procA = pbA.start();
System.out.println("Instance A PID: " + procA.pid());
// Process 2: instance B — same jar, different config
ProcessBuilder pbB = new ProcessBuilder(javaExec, "-jar", "app.jar", "--port=8081");
pbB.environment().put("INSTANCE_ID", "B");
Process procB = pbB.start();
System.out.println("Instance B PID: " + procB.pid());
// Completely isolated: procA crashing won't affect procB
// They share nothing except the disk copy of app.jar
// IPC between them requires sockets, pipes, or shared filesKey Points to Remember
- 1A program is a static file on disk (bytecode, binary); a process is that program in active execution.
- 2A process has its own virtual address space: code, data, heap, and stack segments.
- 3Multiple processes can run the same program — each gets an independent PID and memory space.
- 4Process isolation prevents one crashing process from corrupting another's memory.
- 5The JVM is itself an OS process; your Java code runs within it.
- 6Java ProcessHandle gives runtime introspection of the JVM as an OS process.
Interview Questions
Sign in to ask AriaWhat is the difference between a program and a process?
Can two processes share the same code segment? How does the OS enable this?
How does process isolation protect against security vulnerabilities?
In a microservices deployment, are two replicas of the same service a program or two processes?
Ask Aria about Process vs Program
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.