Can any unused object leave the Garbage Collector?

Is it likely that an object that is not mentioned anywhere and still exists on the heap. I mean, there is a chance that an unused object will exit the garbage collector and remain on the heap until the end of the application.

I wanted to know, because if it is, then when coding I can be more careful.

+3
source share
11 answers

If the object is no longer referenced, it still exists on the heap, but it is also free to collect garbage (unless we are talking about class objects that live in PermGen space and never collect garbage), but this is not what you need to worry).

, , , ​​ .

, , , , , .

: , , ( , JVM Sun, ). : . GC , JVM "", , , - , . . , . , GC "" . , , - , , .

+7

, , JVM , .

+6

, . -, ( ) , "" , , .

( ), "". .

nitty-gitty, Sun gc.

+2

, . , , . , , , , , my , .

CG . , , , , . ​​ CG, .

+2

; - :

Foo foo = new Foo();

// do some work here

while(1) {};

foo.someOp(); // if this is the only reference to foo,
// it theoreticaly impossible to reach here, so it
// should be GC-ed, but all GC systems I know of will
// not Gc it

: garbage = object, .

+1

. , , .

+1

. " ": . System.gc(), ( , ).

, , " " ( : ), "" (GC - , - ). , . : , , , ; - , - - . null, .

+1

( , GC ), - - , , .

0

. , ...

: " , , ". , , , , . , , , .

, , "" , , , , .

, . , "" , , "escape-", . 99,99% -, .

0

- .

. , , , , , , .

0

, , , . , . , , .

, , :

  • JVM
  • The so-called invisible links - they rarely matter, but I had to take them into account once or twice in the last 5 years in the performance-sensitive application I'm working on
0
source

Source: https://habr.com/ru/post/1729952/


All Articles