|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
前些天,在CSDN上看到了一个消息,说是ASP.NETAJAX成功在Linux上运行,这一点对我触动很大,而且引发了我许多感叹,所以想写出来分享一下。3)GarbageCollection
Statethebehaviorthatisguaranteedbythegarbagecollectionsystemandwritecodethatexplicitlymakesobjectseligibleforcollection.
1.Garbagecollectionisamechanismforreclaimingmemoryfromobjectsthatarenolongerinuse,andmakingthememoryavailablefornewobjects.
2.Anobjectbeingnolongerinusemeansthatitcan’tbereferencedbyany‘active’partoftheprogram.
3.Garbagecollectionrunsinalowprioritythread.Itmaykickinwhenmemoryistoolow.Noguarantee.
4.It’snotpossibletoforcegarbagecollection.InvokingSystem.gcmaystartgarbagecollectionprocess.
5.Therearenoguaranteesthattheobjectsnolongerinusewillbegarbagecollectedandtheirfinalizersexecutedatall.gcmightnotevenberuniftheprogramexecutiondoesnotwarrantit.Thusanymemoryallocatedduringprogramexecutionmightremainallocatedafterprogramtermination,unlessreclaimedbytheOSorbyothermeans.
6.Therearealsonoguaranteesontheorderinwhichtheobjectswillbegarbagecollectedorontheorderinwhichthefinalizersarecalled.
7.Circularreferencesdonotpreventobjectsfrombeinggarbagecollected.
8.Wecansetthereferencevariablestonull,hintingthegctogarbagecollecttheobjectsreferredbythevariables.Evenifwedothat,theobjectmaynotbegc-edifit’sattachedtoalistener.(TypicalincaseofAWTcomponents)Remembertoremovethelistenerfirst.
9.Allobjectshaveafinalizemethod.ItisinheritedfromtheObjectclass.
10.finalizemethodisusedtoreleasesystemresourcesotherthanmemory.(suchasfilehandlesandnetworkconnections)Theorderinwhichfinalizemethodsarecalledmaynotreflecttheorderinwhichobjectsarecreated.Don’trelyonit.Thisisthesignatureofthefinalizemethod.
protectedvoidfinalize()throwsThrowable{}
Inthedescendentsthismethodcanbeprotectedorpublic.Descendentscanrestricttheexceptionlistthatcanbethrownbythismethod.
11.finalizeiscalledonlyonceforanobject.Ifanyexceptionisthrowninfinalize,theobjectisstilleligibleforgarbagecollection(atthediscretionofgc)
12.gckeepstrackofunreachableobjectsandgarbage-collectsthem,butanunreachableobjectcanbecomereachableagainbylettingknowotherobjectsofitsexistencefromitsfinalizemethod(whencalledbygc).This‘resurrection’canbedoneonlyonce,sincefinalizeiscalledonlyoneforanobject.
13.finalizecanbecalledexplicitly,butitdoesnotgarbagecollecttheobject.
14.finalizecanbeoverloaded,butonlythemethodwithoriginalfinalizesignaturewillbecalledbygc.
15.finalizeisnotimplicitlychained.Afinalizemethodinsub-classshouldcallfinalizeinsuperclassexplicitlyasitslastactionforproperfunctioning.Butcompilerdoesn’tenforcethischeck.
16.System.runFinalizationcanbeusedtorunthefinalizers(whichhavenotbeenexecutedbefore)fortheobjectseligibleforgarbagecollection.
17.Localvariablesinmethodsgooutofscopewhenthemethodexits.Atthispointthemethodsareeligibleforgarbagecollection.Eachtimethemethodcomesintoscopethelocalvariablesarere-created.
18.Javausesa"marksweepgarbagecollectionalgorithm,whichtraversesalltheobjectreferences,markinganyobjectsthatarereferredtoandthengarbagecollectinganyobjectsthatareunmarked.
19.Javaallowsyoutoaddafinalize()methodtoanyclass.Thefinalize()methodwillbecalledbeforethegarbagecollectorsweepsawaytheobject.Inpractice,donotrelyonthefinalizemethodforrecyclinganyresourcesthatareinshortsupply-yousimplycannotknowwhenthismethodwillbecalled.
Intheexampointofview:
Youmustbeabletoidentifywhenanobjectisavailableforgc-youhaveeithersetittonulloryou
have"redirected"thevariablethatwasoriginallyreferringtoit,sothatitnowreferstoadifferent
object.
ifyouhaveareferencetoanobjectsay,AandthenyoupassAasanargumenttosomeconstructor-
newobj(A);-thenevenifyounullyourreference-A=null;-youcantsaythatAisavailablefor
gc.Sojustfollowthereferencesandwhentheydroptozeroyouknowitseligible/availableforgc,
notthatitwillhappen.
eg,
1.obj=newJo();
2.obj.doSomething();
3.obj=newJo();//Sameasobj=null;
4.obj.doSomething();
Objecta=newObject();
Objecta=null;//Nowtheobjectcreatedin1stlineisavailableforgc
Objecta=newObject();
a=newObject();//same.
//Noworiginalobjectcreatedinline1isavailableforgcandanew
objectisnowouttherereferencedby"a".
Aclassa=newAclass();//Object1
Aclassb=newAclass();//Object2
Aclassc=newAclass();//Object3
a=b;//nowwehavenovalidobjectreferencetoobject"a"anditwillbe
//garbagecollectedsometimeafterthisstatement.Butwhen?......
a=c;
c=null;//nogarbagecollectionwillbeeligiblesince
//"a"stillreferstoObject3
a=null;//nowobject"c"iseligibleforgcsinceitalwayshadavalidreference.
//Should"b"gooutofscope;thenwewouldpossiblyhaveeligibilityforgc.
//theremightstillbeotherreferencestoobject"b"preventingthecollection.
自己的整个学习思路完全被老师的讲课思路所牵制,这样几节课听下来,恐怕自己的见解都应该是书里的知识点了,根本谈不上自身发现问题,分析问题,和解决问题能力的切实提高。 |
|