How far will Java GC go? Let's say I have this code:
public class MyClass { private Object myObject = new Object(); public void clear() { myObject = null; } } public class MyMain { ... MyClass myClass = new MyClass(); ... myClass.clear(); myClass = null; }
Do I need to call myClass.clear () before setting myClass to null to make sure GC will remove myObject inside myClass? Or is it enough to set myClass to null and GC will delete all nested objects?
Maybe I'm a little paranoid and the answer is as simple as "GC will ultimately delete any objects inaccessible to any running code or any future available code." So, if the created object is unavailable or will never be available in any running code, will it eventually become a victim of the GC, even if it is nested and makes the C ++ programmer scary?
, myClass.clear(). , . , , .
myClass.clear()
- . , , , ArrayList<T> null, , - , . , , myClass.clear(), .
ArrayList<T>
null
. MyMain , ... , , myClass . , GC , , - , GC, - . .
MyMain
myClass
- , GC'd. .
Java ++.
"" gc .
, :
long[] arr = new long[7000000]; //arr = null; arr = new long[7000000];
-Xmx100M, OutOfMemoryError. , arr null, . , arr , . , , . GC .
, clear. GC myObject .
clear
myObject
, GC , :
public class Foo { public HashMap a; public HashMap b; public Foo() { a = new HashMap(); b = new HashMap(); a.put("b", b); b.put("a", a); } } // ... public void someFunction() { Foo f = new Foo(); }
( a b , .)
a
b
f a b. f, f , GC , a b .
f
(- ) JLS.
, . , ( " " " ", GC ). , GC.
, , finalize, , -, - , , - .
finalize
GC . , . null.
, myObject.clear, myObject = null, reset null.
myclass null. , .
GC , . , , MyClass , myObject , , GC
MyClass
myClass.clear(), myClass null, , GC myObject myClass?
clear myClass null.
, , , , myClass .
.
Source: https://habr.com/ru/post/1776292/More articles:How can I mix form authentication and Windows authentication in MVC 2 in IIS 7.5? - model-view-controllerHow to distinguish between different encodings / serialization / etc? - typesVCBuild task in MSBuild - change output path - msbuildHow to change entry point to exe file? - exebest practice for configuring C # com objects - .netGetting inherited inheritance property for distribution - inheritanceWhat design template? - phpPHP код дизайн, структура, шаблон..? - designWhy is CoreGui RobloxLocked in a DataModel and why can't trust users to use CoreScripts? - user-interfacehttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1776297/convert-multiple-swf-files-to-single-exe&usg=ALkJrhgJNLHPWhkkgnddz-JBNJ5d4qaR6wAll Articles