`

About gc two objects which are inter referenced to each other

阅读更多

my workmate told me such a senario, there're two objects, which are instantiated in a local method, respectively assigned to local variables. A class has  a type B field, B class has a type A field, and their instances, each has other's instance, the following program illustrates:

 

class A {
	B b;
	
	public A() {
		System.out.println("constructing a");
	}
	
	public void finalize() {
		System.out.println("finalize a");
	}
}

class B {
	A a;
	
	public B() {
		System.out.println("constructing b");
	}
	
	public void finalize() {
		System.out.println("finalize b");
	}
}

public class Test {
	
	public static void main(String[] args) {
		new Test().go();
	}
	
	public void go() {
		A a = new A();
		B b = new B();
		
		a.b = b;
		b.a = a;
		
		try {
			a = null;
			b = null;
			System.gc();
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	public void finalize() {
		System.out.println("finalize Test");
	}
}

 

guess what? as we may reasonably guess: even if the method finishes, each instance has been referenced by the other one at least, so they wouldn't be garbage collected, right? but the actual result is:

constructing a
constructing b
finalize b
finalize a

 

they are both garbage collected even if they may still be referenced. WHY ?

 

primary conclusion may be they two as a whole has been treated a one object which's no longer be referenced, so they get garbage collected.

 

 

 

分享到:
评论

相关推荐

    Find out Stored Procs Which Referenced Specified TableName

    Find out Stored Procs Which Referenced Specified TableName

    机器学习之Iris数据集

    Data Set Information: This is perhaps the best known database to be found in the... the latter are NOT linearly separable from each other. Predicted attribute: class of iris plant. This is an exceedin

    Find Reference 2 1.2.10

    - See all assets not being referenced by any other asset so you can delete them all without worrying about breaking any references. - GUID Tools Advanced tool where you can see GUID of each asset ...

    3GPP TS 51.021 V6.1.0

    all tests are applicable to BSS meeting Phase 2 and/or Phase 2+ GSM requirements, because the requirements of the Phase 2 and Phase 2+ core GSM specifications which are referenced in the test are ...

    Find Reference 2

    - See all assets not being referenced by any other asset so you can delete them all without worrying about breaking any references. - GUID Tools Advanced tool where you can see GUID of each asset ...

    微软内部资料-SQL性能优化5

    Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...

    can't find referenced pointcut

    spring-framework-3.1.1.RELEASE jdk1.7 环境下 can't find referenced pointcut 问题解决 换了对应的jar文件即可 aspectjrt.jar aspectj-1.6.6.jar aspectjweaver.jar

    Professional NoSQL

    A few other NoSQL products, besides these, are also referenced. An important part of NoSQL is the way large data sets are manipulated. This book covers all the essentials of MapReduce-based scalable ...

    鸢尾花数据

    and is referenced frequently to this day. (See Duda & Hart, for example.) The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant. One class is ...

    STD-A0084_GeneralElectricalHardwareRequirementsandGuidelines

    referenced in each Component Technical Specification (CTS) or Subsystem Technical Specification (SSTS). It is intended to be a guideline when creating a CTS/SSTS, and a guideline for suppliers when ...

    acpi控制笔记本风扇转速

    firing again and to prevent other wake GPEs from interrupting the wake process. Added the AcpiGpeCount global that tracks the number of processed GPEs, to be used for debugging systems with a large ...

    DESIGN, PROCEDURES AND INSPECTION

    and specifications which are referenced in this manual; designers should note that there are differences among the documents’ requirements. Therefore, when this material is used, the editions which ...

    Chinese Entity Linking Comprehensive

    are assumed to be co-referenced (clustered), with the same "E" type ID or the same "NIL" ID if they refer to the same entity. Each "E" type ID and NIL ID is distinct from one another. 3. entity-...

    EhLib 9.1.024

    Allows to hold KeyValue and Text as not affecting to each other values. Take effect when KeyField, ListField, ListSource, DataField and DataSource properties is empty. Drop down list can: Show ...

    微软内部资料-SQL性能优化3

    An isolation level determines the degree to which data is isolated for use by one process and guarded against interference from other processes. Prior to SQL Server 7.0, REPEATABLE READ and ...

    EhLib 8.0 Build 8.0.023 Pro Edition FullSource for D7-XE8

    Allows to hold KeyValue and Text as not affecting to each other values. Take effect when KeyField, ListField, ListSource, DataField and DataSource properties is empty. Drop down list can: Show ...

    EhLib 6.3 Build 6.3.176 Russian version. Full source included.

    Allows to hold KeyValue and Text as not affecting to each other values. Take effect when KeyField, ListField, ListSource, DataField and DataSource properties is empty. Drop down list can: Show ...

    Perl extension module for Tuxedo

    These objects allow the user to create and manipulate the elements of these C structures, and these objects are then passed as parameters to the perl version of these tuxedo C functions. * buffer ...

    EhLib5.0.13 最新的ehlib源码

    Allows to hold KeyValue and Text as not affecting to each other values. Take effect when KeyField, ListField, ListSource, DataField and DataSource properties is empty. Drop down list can: Show ...

    Vagaa哇嘎画时代--体验群体智慧的力量!

    4.1 You are responsible for paying all applicable taxes and other costs you may incur in connection with your use of the Software including but not limited to all hardware and software costs and ...

Global site tag (gtag.js) - Google Analytics