garbage collector, 싱글톤

Dec 21, 2023
garbage collector, 싱글톤

garbage collector

a에게 갔다가 주소 확인 후 new의 영역으로 이동   ————>   a의 주소가 b로 변경되면서 a의 new영역이 필요 X
a에게 갔다가 주소 확인 후 new의 영역으로 이동 ————> a의 주소가 b로 변경되면서 a의 new영역이 필요 X
 

싱글톤

  • 패턴 객체를 메모리에 단 한개 입력 가능
    • class President{ static President instance = new President(); private President(){ } } public class SingleTonEx01 { public static void main(String[] args) { President p1 = President.instance; System.out.println(p1.hashCode()); President p2 = President.instance; System.out.println(p2.hashCode()); } }
      private 메소드는 main에서 new를 띄울 수 없기 때문에 싱글톤 패턴을 사용 static 사용 가능 → 메모리 공간 소모
Share article

from-web-developer