弱引用

计算机程序设计中,弱引用强引用相对,是指不能确保其引用的对象不会被垃圾回收器回收的引用。一个对象若只被弱引用所引用,则被认为是不可访问(或弱可访问)的,并因此可能在任何时刻被回收。一些配有垃圾回收机制的语言,如JavaC#PythonPerlLisp等都在不同程度上支持弱引用。

垃圾回收

垃圾回收用来清理不会再使用的对象,从而降低内存泄露和数据损坏的可能性。垃圾回收主要有两种类型:追踪引用计数。引用计数会记录给定对象的引用个数,并在引用个数为零时收集该对象。由于一次仅能有一个对象被回收,引用计数无法回收循环引用的对象。一组相互引用的对象若没有被其它对象直接引用,并且不可访问,则会永久存活下来。一个应用程序如果持续地产生这种不可访问的对象群组,就会发生内存泄漏。在对象群组内部使用弱引用(即不会在引用计数中被计数的引用)有时能避免出现引用环,因此弱引用可用于解决循环引用的问题。如Apple的Cocoa框架就推荐使用这种方法,具体为,在父对子引用时使用强引用,子对父引用时使用弱引用,从而避免了循环引用。[1]页面存档备份,存于互联网档案馆

程序对一些对象只进行弱引用,通过此法可以指明哪些对象是不重要的,因此弱引用也用于尽量减少内存中不必要的对象存在的数量。

变种

有些语言包含多种强度的弱引用。例如Java,在java.lang.ref[1][2]包中定义了软引用、弱引用和虚引用,引用强度依次递减。每种引用都有相对应的可访问性概念。垃圾回收器(GC)通过判断对象的可访问性类型来确定何时回收该对象。当一个对象是软可访问的,垃圾回收器就可以安全回收这个对象,但如果垃圾回收器认为JVM还能空出可用内存(比如JVM还有大量未使用的堆空间),则有可能不会立刻回收软可访问的对象。但对于弱可访问的对象,一旦被垃圾回收器注意到,就会被回收。和其他引用种类不同,虚引用无法跟踪。但另一方面,虚引用提供了一种机制,当一个对象被回收时程序可以得到通知(实现于ReferenceQueues[3])。 一些未配有垃圾回收机制的语言,比如C++,也提供强/弱引用的功能,以作为对垃圾回收库的支持。在C++中,普通指针可看做弱引用,智能指针可看做强引用,尽管指针不能算"真正"的弱引用,因为弱引用应该能知道何时对象变成不可访问的了。

示例

弱引用可用于在应用程序中维护一个当前被引用的对象的列表。该列表必须弱引用到那些对象,否则一旦对象被添加到列表中,由于它们被列表引用了,在程序运行期间将永远不会被回收。

Java

Java是第一个将强引用作为默认对象引用的主流语言。之前的(ANSI)C语言只支持弱引用。而后David Hostettler Wain和Scott Alexander Nesmith注意到事件树无法正常释放的问题,结果在大约1998年,推出了分别会被计数和不会被计数的强、弱引用。 如果创建了一个弱引用,然后在代码的其它地方用 get()获得真实对象,由于弱引用无法阻止垃圾回收,get()随时有可能开始返回 null(假如对象没有被强引用)。[4]

import java.lang.ref.WeakReference;
 
public class ReferenceTest {
	public static void main(String[] args) throws InterruptedException {
 
            WeakReference r = new WeakReference(new String("I'm here"));
            WeakReference sr = new WeakReference("I'm here");
            System.out.println("before gc: r=" + r.get() + ", static=" + sr.get());
            System.gc();
            Thread.sleep(100);
 
            //只有r.get()变为null
            System.out.println("after gc: r=" + r.get() + ", static=" + sr.get());
 
	}
}

弱引用还可以用来实现缓存。例如用弱哈希表,即通过弱引用来缓存各种引用对象的哈希表。当垃圾回收器运行时,假如应用程序的内存占用量高到一定程度,那些不再被其它对象所引用的缓存对象就会被自动释放。

Smalltalk

|a s1 s2|

s1 := 'hello' copy. "这是个强引用"
s2 := 'world' copy. "这是个强引用"
a := WeakArray with:s1 with:s2.
a printOn: Transcript. 
ObjectMemory collectGarbage.
a printOn: Transcript. "两个元素都还在"

s1 := nil. "移除强引用" 
ObjectMemory collectGarbage.
a printOn: Transcript. "第一个元素消失"

s2 := nil. "移除强引用" 
ObjectMemory collectGarbage.
a printOn: Transcript. "第二个元素消失"

Lua

weak_table = setmetatable({}, {__mode="v"})
weak_table.item = {}
print(weak_table.item)
collectgarbage()
print(weak_table.item)

Objective-C 2.0

在Objective-C 2.0中,除了垃圾回收,自动引用计数也会受弱引用的影响。下面这个例子中的所有变量和属性都是弱引用。

@interface WeakRef : NSObject
{
    __weak NSString *str1;
    __assign NSString *str2;
}
 
@property (nonatomic, weak) NSString *str3;
@property (nonatomic, assign) NSString *str4;
 
@end

weak(__weak)和assign(__assign)的区别在于,当变量指向的对象被重新分配时,变量的值是否会跟着改变。weak声明的变量会变为nil,而assign声明的变量则会保持不变,成为一个悬摆指针。从Mac OX 10.7 “狮子”系统和iOS 5开始,随着Xcode 4.1版本的推出,weak引用被引入到Objective-C语言中(4.2版本开始支持iOS)。老版本的Mac OS X、iOS和GNUstep仅支持用assign声明弱引用。

Vala

class Node {
    public weak Node prev; //弱引用可避免列表中的节点之间出现循环引用
    public Node next;
}

Python

>>> import weakref
>>> import gc
>>> class Egg:
...     def spam(self):
...         print("I'm alive!")
...
>>> obj = Egg()
>>> weak_obj = weakref.ref(obj)
>>> weak_obj().spam()
I'm alive!
>>> obj = "Something else"
>>> gc.collect()
35
>>> weak_obj().spam()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'spam'

参考文献

  1. ^ java.lang.ref. [2013-12-28]. (原始内容存档于2022-06-06). 
  2. ^ Nicholas, Ethan. "Understanding Weak References" 互联网档案馆存檔,存档日期2010-08-19.. java.net页面存档备份,存于互联网档案馆). 发布于2006年5月4日. 访问于2010年10月1日
  3. ^ ReferenceQueues. [2013-12-28]. (原始内容存档于2011-09-30). 
  4. ^ weblogs.java.net Java Examples 互联网档案馆存檔,存档日期2010-08-19.

Read other articles:

Feldschlösschen Original Bier is in Zwitserland minder streng aan regels gebonden dan bier in Duitsland. Bieren kunnen gebrouwen worden volgens het Reinheitsgebot maar de wetgeving laat het gebruik toe van maïs en rijst met daarnaast toevoeging van maximaal 10% suiker en 20% zetmeel. De meeste Zwitserse bieren lijken erg op de Oostenrijkse en Beierse bieren. De bierproductie in Zwitserland heeft nog steeds een regionaal karakter en sommige gebieden worden van bier voorzien door plaatselijke...

 

Цейнерит Загальні відомостіСтатус IMA чинний (успадкований, G)[d][1]Абревіатура Zeu[2]Хімічна формула Cu(UO₂)₂(AsO₄)₂·12H₂ONickel-Strunz 10 8.EB.05[3]Dana 8 40.2a.14.1ІдентифікаціяСингонія тетрагональна сингонія[4]Просторова група space group P4/nncdТвердість 2,5Інші характеристикиНазв...

 

Mutxamel municipio de España Escudo Iglesia parroquial del Salvador MutxamelUbicación de Mutxamel en España. MutxamelUbicación de Mutxamel en la provincia de Alicante. Mapa interactivo — MuchamielPaís  España• Com. autónoma  Comunidad Valenciana• Provincia Alicante• Comarca Campo de Alicante• Partido judicial San Vicente del RaspeigUbicación 38°24′49″N 0°26′44″O / 38.413611111111, -0.4455555555...

Chester S. FurmanBorn(1842-02-14)February 14, 1842Edgar County, Illinois, United StatesDiedJuly 22, 1910(1910-07-22) (aged 68)Place of burialColumbia, Pennsylvania, United StatesAllegianceUnited States of AmericaService/branchUnited States ArmyUnion ArmyRankE-04CorporalUnit Company A, 6th Pennsylvania Reserves US Army Signal CorpsBattles/warsBattle of GettysburgAwards Medal of Honor Chester S. Furman (February 14, 1842 – July 22, 1910) was an American soldier who received the Medal...

 

Hungarian Roman Catholic bishop The native form of this personal name is Boldog altorjai báró Apor Vilmos. This article uses Western name order when mentioning individuals. Blessed BishopBaron Vilmos AporBishop of Győrc. 1930.ChurchRoman Catholic ChurchDioceseGyőrSeeGyőrAppointed21 January 1941Installed2 March 1941Term ended2 April 1945PredecessorIstván BreyerSuccessorKároly Kálmán PappOrdersOrdination24 August 1915by Sigismund WaitzConsecration24 February 1941by Jusztini

 

Charitable humanitarian organization This article's lead section may be too short to adequately summarize the key points. Please consider expanding the lead to provide an accessible overview of all important aspects of the article. (November 2023) OxfamNamed afterOxford Committee for Famine ReliefFounded5 October 1942; 81 years ago (1942-10-05)Founded atOxford, EnglandTypeInternational NGORegistration no.202918[1]FocusPoverty eradicationDisaster reliefAdvocacyPo...

Participantes de la Competición Internacional Universitaria de Programación representando a la Universidad Nacional de Taiwán en 2010. La Competición Internacional Universitaria de Programación (en inglés International Collegiate Programming Contest, abreviado ICPC) es una competición anual de programación y algorítmica entre universidades de todo el mundo, donde prima el trabajo en equipo, el análisis de problemas y el desarrollo rápido de software. Tiene su sede en la Universidad...

 

Cymbeline's CastleCymbeline's Castle and Little KimbleHighest pointCoordinates51°45′00″N 0°47′44″W / 51.7499°N 0.7955°W / 51.7499; -0.7955GeographyLocationGreat Kimble, BuckinghamshireOS gridSP 83265 06350 Cymbeline's Castle, also known as Cymbeline's Mound and Belinus's Castle, is the remains of a motte-and-bailey castle in woods north-east of Great Kimble in Buckinghamshire, England. It is scheduled under the Ancient Monuments and Archaeological Area...

 

قرية المعرس  - قرية -  تقسيم إداري البلد  اليمن المحافظة محافظة المحويت المديرية مديرية المحويت العزلة عزلة الغربي الاعلى السكان التعداد السكاني 2004 السكان 318   • الذكور 155   • الإناث 163   • عدد الأسر 37   • عدد المساكن 26 معلومات أخرى التوقيت توقيت اليمن (+...

Business school of the University of Pennsylvania Wharton School of the University of PennsylvaniaOther nameThe Wharton School of Business, The Wharton School, WhartonFormer namesWharton School of Finance and Economy (1881–1902) Wharton School of Finance and Commerce (1902–1972)MottoKnowledge for actionTypePrivate business schoolEstablished1881; 142 years ago (1881)FounderJoseph WhartonParent institutionUniversity of PennsylvaniaAcademic affiliationsINSEAD-Wharton Allian...

 

Sanskrit work meaning ode or eulogy Part of a series onHindu scriptures and texts Shruti Smriti List Vedas Rigveda Samaveda Yajurveda Atharvaveda Divisions Samhita Brahmana Aranyaka Upanishads UpanishadsRig vedic Aitareya Kaushitaki Sama vedic Chandogya Kena Yajur vedic Brihadaranyaka Isha Taittiriya Katha Shvetashvatara Maitri Atharva vedic Mundaka Mandukya Prashna Other scriptures Bhagavad Gita Agamas Related Hindu texts Vedangas Shiksha Chandas Vyakarana Nirukta Kalpa Jyotisha PuranasBrahm...

 

Human rights slogan This article is about the Black Lives Matter slogan. For other uses, see I Can't Breathe (disambiguation). I can't breatheGeorge Floyd protest in 2020Origin/etymologyKilling of Eric GarnerMeaningRallying cry against police brutalityContextPolice brutality and lack of police accountability I can't breathe is a slogan associated with the Black Lives Matter movement in the United States. The phrase originates from the last words of Eric Garner, an unarmed man who was kill...

2006 South Korean film by Kim Ki-duk TimeTime film posterKorean nameHangul시간Hanja時間Revised RomanizationSiganMcCune–ReischauerSigan Directed byKim Ki-dukWritten byKim Ki-dukStarringHa Jung-wooSung Hyun-ahDistributed bySpongeHappinetRelease date June 30, 2006 (2006-06-30) Running time97 minutesCountriesJapanSouth KoreaLanguageKoreanBudget$1 million[citation needed]Box office$185,299[1][2] Time is the thirteenth feature film by South Korean direct...

 

Afghan Police Chief (1965–2011) Shahjahan NooriNative nameشاه جهان نوریNickname(s)Maulana Shahjahan NooriBorn(1965-03-30)March 30, 1965Takhar Province, AfghanistanDiedMay 28, 2011(2011-05-28) (aged 46)Takhar Province, AfghanistanService/branchMilitary of Afghanistan Afghan National Police Ministry of Defense (Afghanistan)Years of service1981–2011RankMajor GeneralCommands held Commander during the Soviet–Afghan War Commander of the anti-Taliban United Islamic Front u...

 

Lithuanian beauty pageant title Miss Grand LithuaniaFormation2017TypeBeauty pageantHeadquartersVilniusLocationLithuaniaMembership Miss Grand InternationalOfficial language LithuanianNational directorErnest Hadrian BöhmParent organizationMiss Queen of Scandinavia(2016 – 2017)Exclusive Event International (2018) Jurate Stasiunaite, Miss Grand Lithuania 2018 Miss Grand Lithuania is a national beauty pageant title awarded to Lithuania representatives chosen to compete at the Miss Grand Interna...

1948 film by Michael Gordon Another Part of the ForestPublicity still with, from left to right, Dona Drake, Dan Duryea, Ann Blyth, Fredric March, Florence Eldridge, Edmond O'Brien, and John DallDirected byMichael GordonScreenplay byVladimir PoznerBased onAnother Part of the Forest by Lillian HellmanProduced byJerry BreslerStarringFredric MarchFlorence EldridgeDan DuryeaEdmond O'BrienCinematographyHal MohrEdited byMilton CarruthMusic byDaniele AmfitheatrofProductioncompanyUniversal-Internation...

 

2000 single by Angie StoneLife StorySingle by Angie Stonefrom the album Black Diamond Released2000Length4:07LabelAristaSongwriter(s)Gerry DeVeauxCraig RossProducer(s)Gerry DeVeauxCutfather & JoeAngie Stone singles chronology No More Rain (In This Cloud) (1999) Life Story (2000) Everyday (2000) Life Story is a song by American recording artist Angie Stone. It was written by Gerry DeVeaux and Craig Ross for Stone's debut studio album, Black Diamond (1999), while production was overseen by D...

 

Computer program used to access and manage a user's email This article is about mail readers for Internet e-mail. For BBS mail readers, see Offline reader. Mozilla Thunderbird email client user interface on a Linux operating system An email client, email reader or, more formally, message user agent (MUA) or mail user agent is a computer program used to access and manage a user's email. A web application which provides message management, composition, and reception functions may act as a web e...

2007 South Korean filmSeven DaysTheatrical posterKorean nameHangul세븐 데이즈Revised RomanizationSebeun DeijeuMcCune–ReischauerSebŭn Teijŭ Directed byWon Shin-yunWritten byYoon Jae-guProduced byLeeSeo-yeolJeong Yong-wookJeong Geun-hyeonIm Choong-geunStarring Yunjin Kim Park Hee-soon Kim Mi-sook Choi Moo-sung CinematographyChoi Young-hwanEdited byShin Min-kyungMusic byKim Jun-seongProductioncompaniesPrime EntertainmentYoon & Joon FilmsDistributed byPrime EntertainmentRelease date ...

 

Questa voce sull'argomento stagioni delle società calcistiche italiane è solo un abbozzo. Contribuisci a migliorarla secondo le convenzioni di Wikipedia. Segui i suggerimenti del progetto di riferimento. Voce principale: Associazione Calcio Rimini 1912. Rimini CalcioStagione 1964-1965Sport calcio Squadra Rimini Allenatore Romolo Bizzotto Presidente Guido Belardinelli Serie C12º posto nel girone B. Maggiori presenzeCampionato: Nanni (33) Miglior marcatoreCampionato: Benetti, Mante...

 

Strategi Solo vs Squad di Free Fire: Cara Menang Mudah!