How does Java's garbage collector differentiate primitives and references on the operand stack?
If my understanding is correct, this code "someFunction(0, new SomeClass())" would result in a primitive integer 0 and a reference to a new instance of type SomeClass being pushed to the operand stack. Further, the only reference to the new instance would be on the operand stack, so the gc would have to traverse the operand stack to find live references. It is not clear to me how the gc could efficiently determine the types of the values on the operand stack (which are references and which are just numeric values). All strategies that occur to me seem inefficient, such as examining the bytecode + instruction pointer to determine the types of values on the operand stack or tracking the values of types on the operand stack in some structure.
This is NOT a duplicate of "Is the stack garbage collected in Java?". The answer to the question "is the stack garbage collected in java" is no, because stack frames are popped when functions return. Primitives stored in stack frames are not in the heap, they would be referred to as having auto storage duration in C. However, references stored in the operand stack should keep the objects to which they refer alive. Thus, the operand stack contains both primitive numeric values and references. The references must be traversed during tracing gc, but primitives should be ignored since their memory is freed when a call returns.