When an application creates an object from a new statement, the JVM allocates a piece of memory space for the object, which is always in memory as long as the object is referenced by a reference variable. When the object is not referenced by any object, his life cycle means the end, and the JVM reclaims the object at the appropriate time. The following life cycle is demonstrated by code:
Customer C= NewCustomer ("Tom",NewHashSet ());OrderO1= New Order("tom_order001",NULL);OrderO2= New Order("tom_order002",NULL); O1.Setcustomer (c); c.GetOrders ().Add (O1); O1= NULL;//Line sixthO2= NULL;//Line SeventhC= NULL;//Line eighth
For O2, at the end of the seventh line life cycle, for O1 at the end of the eighth line life cycle.
When executing:
=new Customer("Tom",new HashSet());Order=newOrder("Tom_order001",null);Order=newOrder("Tom_order002",null);
When executing:
o1.setCustomer(c);c.getOrders().add(o1);
When O1 is set to NULL, the life cycle of the Order1 object does not end, although O1 no longer references the Order1 object because the Orders collection of the customer object also holds a reference to the Order1 object.
When O2 is set to NULL, the Order2 object is no longer referenced by any reference variable, so the life cycle ends.
Proficient in the life cycle of Hibernate--java objects in the JVM