are java primitives garbage collected

前端 未结 3 1923
Happy的楠姐
Happy的楠姐 2020-12-03 17:45

If I declare an int (or any primitive type) within a method in Java, is that memory freed the moment the function returns, or does it have to hang around until the garbage c

3条回答
  •  不思量自难忘°
    2020-12-03 17:58

    is that memory freed the moment the function returns, or does it have to hang around until the garbage collector cleans it?

    The primitives declared inside the method are stored on the stack frame of that method. Since the stack frame is destroyed as soon as the method returns, the space allocated to local variables are freed.

提交回复
热议问题