CodeGym /Java Course /๋ชจ๋“ˆ 3 /Java์˜ ์ •ํฌ ๋งํฌ

Java์˜ ์ •ํฌ ๋งํฌ

๋ชจ๋“ˆ 3
๋ ˆ๋ฒจ 18 , ๋ ˆ์Šจ 7
์‚ฌ์šฉ ๊ฐ€๋Šฅ

8.1 Java์˜ ์•ฝํ•œ ์ฐธ์กฐ

Java์—๋Š” ์—ฌ๋Ÿฌ ์ข…๋ฅ˜์˜ ์ฐธ์กฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

StrongReference ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค . ์ด๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ๋งค์ผ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ๋งํฌ์ž…๋‹ˆ๋‹ค.

Object object = new Object();//created an object
object = null;//can now be garbage collected

๊ทธ๋ฆฌ๊ณ  SoftReference, WeakReference, PhantomReference์˜ ์„ธ ๊ฐ€์ง€ "ํŠน์ˆ˜" ์œ ํ˜•์˜ ๋งํฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ชจ๋“  ์œ ํ˜•์˜ ๋งํฌ ๊ฐ„์—๋Š” ๋งํฌ๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐœ์ฒด์— ๋Œ€ํ•œ GC์˜ ๋™์ž‘์ด๋ผ๋Š” ํ•œ ๊ฐ€์ง€ ์ฐจ์ด์ ๋งŒ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ๊ฐ ๋งํฌ ์œ ํ˜•์˜ ์„ธ๋ถ€ ์‚ฌํ•ญ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์„ค๋ช…ํ•˜๊ฒ ์ง€๋งŒ ์ง€๊ธˆ์€ ๋‹ค์Œ ์ง€์‹์œผ๋กœ ์ถฉ๋ถ„ํ•ฉ๋‹ˆ๋‹ค.

  • SoftReference ๋Š” ์†Œํ”„ํŠธ ์ฐธ์กฐ์ž…๋‹ˆ๋‹ค. GC๊ฐ€ ๊ฐ์ฒด๊ฐ€ ์†Œํ”„ํŠธ ์ฐธ์กฐ ์ฒด์ธ์„ ํ†ตํ•ด์„œ๋งŒ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์•„๋งˆ๋„.
  • WeakReference - ์•ฝํ•œ ์ฐธ์กฐ. GC์—์„œ ์•ฝํ•œ ์ฐธ์กฐ ์ฒด์ธ์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐœ์ฒด์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด๋‹น ๊ฐœ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • PhantomReference ๋Š” ํŒฌํ…€ ์ฐธ์กฐ์ž…๋‹ˆ๋‹ค. GC์—์„œ ๊ฐ์ฒด๊ฐ€ ํŒฌํ…€ ์ฐธ์กฐ ์ฒด์ธ์„ ํ†ตํ•ด์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. GC๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•œ ํ›„.

๋˜ํ•œ ๋งํฌ ์œ ํ˜•์—๋Š” ์–ด๋Š ์ •๋„์˜ ๋ถ€๋“œ๋Ÿฌ์›€์ด ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • ์ผ๋ฐ˜ ํ•˜๋“œ ๋งํฌ๋Š” ์ฐธ์กฐ ์œ ํ˜•์˜ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ฒŒ ๋˜๊ธฐ ์ „์— ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ์— ์˜ํ•ด ์ •๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์†Œํ”„ํŠธ๋ ˆํผ๋Ÿฐ์Šค . ๊ฐ์ฒด๋กœ ์ธํ•ด ๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์†Œ๋ชจ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. OutOfMemoryError๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ์˜ ๊ตฌํ˜„์— ๋”ฐ๋ผ ๋” ๋น ๋ฅผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์•ฝํ•œ ์ฐธ์กฐ . ์•ฝํ•œ ๋ถ€๋“œ๋Ÿฌ์›€. ๊ฐœ์ฒด๊ฐ€ ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์„ ๋ง‰์ง€ ์•Š์œผ๋ฉฐ ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๋Š” ์ด๋Ÿฌํ•œ ์ฐธ์กฐ๋ฅผ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.
  • ํŒฌํ…€ ์ฐธ์กฐ . ๊ฐœ์ฒด์˜ "์ฃฝ์Œ" ์ฒ˜๋ฆฌ์— ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๋Š” ์ข…๋ฃŒ ํ›„ ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๋  ๋•Œ๊นŒ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฐจ์ด์ ์ด ๋ฌด์—‡์ธ์ง€ ์•„์ง ๋ช…ํ™•ํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ฑฑ์ •ํ•˜์ง€ ๋งˆ์‹ญ์‹œ์˜ค. ๊ณง ๋ชจ๋“  ๊ฒƒ์ด ์ œ์ž๋ฆฌ์— ๋†“์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์„ธ๋ถ€ ์‚ฌํ•ญ์€ ์„ธ๋ถ€ ์‚ฌํ•ญ์— ์žˆ์œผ๋ฉฐ ์„ธ๋ถ€ ์‚ฌํ•ญ์€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค.

8.2 Java์˜ WeakReference ๋ฐ SoftReference

๋จผ์ € Java์—์„œ WeakReference ์™€ SoftReference ์˜ ์ฐจ์ด์ ์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค .

์ฆ‰, ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๋Š” ์•ฝํ•œ ์ฐธ์กฐ๋งŒ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐœ์ฒด์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด๊ฐ€ SoftReferences์—์„œ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒฝ์šฐ JVM์— ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์ ˆ์‹คํžˆ ํ•„์š”ํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ํŠน์ • ๊ฒฝ์šฐ์— Strong ์ฐธ์กฐ๋ณด๋‹ค SoftReference ์— ํ™•์‹คํ•œ ์ด์ ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค . ์˜ˆ๋ฅผ ๋“ค์–ด SoftReference๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์บ์‹œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋ฏ€๋กœ JVM์ด ๊ฐ€์žฅ ๋จผ์ € ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์€ SoftReferences๋งŒ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐœ์ฒด๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

WeakReference๋Š” ClassLoader์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ํด๋ž˜์Šค๊ฐ€ ๋กœ๋“œ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ClassLoader๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด WeakReference๊ฐ€ ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๊ฐ€ ClassLoader์— ๋Œ€ํ•œ ๋งˆ์ง€๋ง‰ ๊ฐ•๋ ฅํ•œ ์ฐธ์กฐ๊ฐ€ ์ œ๊ฑฐ๋˜์ž๋งˆ์ž ClassLoader์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ด์œ ์ž…๋‹ˆ๋‹ค.

Java์˜ WeakReference ์˜ˆ์ œ:

// some object
Student student = new Student();

// weak reference to it
WeakReference weakStudent = new WeakReference(student);

// now the Student object can be garbage collected
student = null;

Java์˜ SoftReference ์˜ˆ์ œ:

// some object
Student student = new Student();

// weak reference to it
SoftReference softStudent = new SoftReference(student)

// now the Student object can be garbage collected
// but this will only happen if the JVM has a strong need for memory
student = null;

8.3 ์ž๋ฐ”์˜ PhantomReference

PhantomReference ์ธ์Šคํ„ด์Šค๋Š” WeakReference ๋ฐ SoftReference ์˜ˆ์ œ์™€ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ƒ์„ฑ๋˜์ง€๋งŒ ๊ฑฐ์˜ ์‚ฌ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฐœ์ฒด์— ๊ฐ•ํ•œ(Strong), ์•ฝํ•œ(WeakReference) ๋˜๋Š” ๋ถ€๋“œ๋Ÿฌ์šด(SoftReference) ์ฐธ์กฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ PhantomReference๋Š” ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด Phantom Reference ๊ฐœ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

PhantomReference myObjectRef = new PhantomReference(MyObject);

PhantomReference๋Š” finalize()๊ฐ€ ์˜๋ฏธ๊ฐ€ ์—†๋Š” ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ์ฐธ์กฐ ์œ ํ˜•์€ ๊ฐœ์ฒด์— ์•ก์„ธ์Šคํ•˜๋„๋ก ์„ค๊ณ„๋˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์œ ํ˜•๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ๊ฐœ์ฒด๊ฐ€ ์ด๋ฏธ ์ข…๋ฃŒ๋˜์—ˆ๊ณ  ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšŒ์ˆ˜ํ•  ์ค€๋น„๊ฐ€ ๋˜์—ˆ๋‹ค๋Š” ์‹ ํ˜ธ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๋Š” ์ถ”๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด ์ด๋ฅผ ํŠน์ˆ˜ ReferenceQueue ์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค . ReferenceQueue๋Š” ๊ฐœ์ฒด ์ฐธ์กฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฐ์น˜๋˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.

ํŒฌํ…€ ์ฐธ์กฐ๋Š” ๊ฐœ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋Š” ์•ˆ์ „ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํฐ ์ด๋ฏธ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ๊ฐํ•ด ๋ณด์‹ญ์‹œ์˜ค. ์ด๋ฏธ ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๊ณ  ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘ ์ค€๋น„๊ฐ€ ๋œ ์ด๋ฏธ์ง€๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๋ ค๊ณ  ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์ƒˆ ์ด๋ฏธ์ง€๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•˜๊ธฐ ์ „์— ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ๊ฐ€ ์ด์ „ ์ด๋ฏธ์ง€๋ฅผ ์ฃฝ์ผ ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ค์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์—ฌ๊ธฐ์„œ PhantomReference๋Š” ์œ ์—ฐํ•˜๊ณ  ์•ˆ์ „ํ•œ ์„ ํƒ์ž…๋‹ˆ๋‹ค. ์ด์ „ ์ด๋ฏธ์ง€ ๊ฐœ์ฒด๊ฐ€ ์†Œ๋ฉธ๋œ ํ›„ ์ด์ „ ์ด๋ฏธ์ง€์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ReferenceQueue๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์ด ๋งํฌ๊ฐ€ ์žˆ์œผ๋ฉด ์ƒˆ ์ด๋ฏธ์ง€๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

3
ะžะฟั€ะพั
Working with memory in Java,ย  18 ัƒั€ะพะฒะตะฝัŒ,ย  7 ะปะตะบั†ะธั
ะฝะตะดะพัั‚ัƒะฟะตะฝ
Working with memory in Java
Working with memory in Java
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION