Effective Java


elements[size] = null; // Eliminate obsolete reference



Download 2,19 Mb.
Pdf ko'rish
bet35/341
Sana11.07.2022
Hajmi2,19 Mb.
#776765
1   ...   31   32   33   34   35   36   37   38   ...   341
Bog'liq
Effective Java

elements[size] = null; // Eliminate obsolete reference
return result;
}
An added benefit of nulling out obsolete references is that if they are subse-
quently dereferenced by mistake, the program will immediately fail with a
NullPointerException
, rather than quietly doing the wrong thing. It is always
beneficial to detect programming errors as quickly as possible.
When programmers are first stung by this problem, they may overcompensate
by nulling out every object reference as soon as the program is finished using it.
This is neither necessary nor desirable; it clutters up the program unnecessarily.
Nulling out object references should be the exception rather than the norm.
The best way to eliminate an obsolete reference is to let the variable that contained
the reference fall out of scope. This occurs naturally if you define each variable in
the narrowest possible scope (Item 57).


CHAPTER 2
CREATING AND DESTROYING OBJECTS
28
So when should you null out a reference? What aspect of the 
Stack
class
makes it susceptible to memory leaks? Simply put, it 
manages its own memory
.
The 
storage pool
consists of the elements of the 
elements
array (the object refer-
ence cells, not the objects themselves). The elements in the active portion of the
array (as defined earlier) are 
allocated
, and those in the remainder of the array are
free
. The garbage collector has no way of knowing this; to the garbage collector,
all of the object references in the 
elements
array are equally valid. Only the
programmer knows that the inactive portion of the array is unimportant. The pro-
grammer effectively communicates this fact to the garbage collector by manually
nulling out array elements as soon as they become part of the inactive portion.
Generally speaking, 

Download 2,19 Mb.

Do'stlaringiz bilan baham:
1   ...   31   32   33   34   35   36   37   38   ...   341




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish