monitorexit 0xc3

Pops an object and unlocks its monitor.

Opcode
0xc3 (195)
Family
Monitors
Operands
none
Length
1 byte
Operand stack
…, objectref → …
Can throw
NullPointerException, IllegalMonitorStateException

What it does

Unlocks the object's monitor (decrements its count). javac emits two for every synchronized block: one on the normal way out, and one in an exception handler that catches anything, unlocks, and rethrows. The handler also covers itself, so the monitor is released even if the unlock is interrupted.

How often javac uses it

In JDK 25.0.5's java.base module, monitorexit appears 2,147 times in 244 of its 7,391 classes: 0.11% of all instructions, the 84th most common of 194 opcodes in use. Some of the methods that use it: java.io.BufferedReader.read, java.io.BufferedWriter.flushBuffer, java.io.CharArrayReader.read, java.io.CharArrayWriter.write.

Examples

From the sample classes, compiled by javac 25 with -g. Each Java line is shown above the bytecode it compiled to; monitorexit is highlighted.

void increment() in Sync, compiled by javac 25
… 7 instructions before
8count++;
9getfield #13// Sync.count : int
13iadd
14putfield #13// Sync.count : int
9}
19goto 27
22astore_2 catch any, any
… 1 instruction after

Open in the viewer

void bump() in Switches, compiled by javac 25
… 7 instructions before
55count++;
9getfield #63// Switches.count : int
13iadd
14putfield #63// Switches.count : int
56}
19goto 27
22astore_2 catch any, any
… 1 instruction after

Open in the viewer

Related instructions

monitorenter Pops an object and locks its monitor (start of synchronized).athrow Throws the exception on top of the stack.

Specification: JVMS §6.5, monitorexit (Java SE 25).