Go 는 Object-Oriented언어인가?
• 맞기도 하고 아니기도 하다.
• http://golang.org/doc/faq#Is_Go_an_object-oriented_language
• 아닌 이유
• 상속관계가 없다. No subclass
• 맞는 이유
• object-oriented 스타일의 프로그래밍을 허용한다. interface제공.
• object-oriented 스타일의 프로그래밍?
• 상속은 제공하지 않으며 interface만 제공.
• implements"라는 선언 필요없음.
• 단순히 해당 인터페이스의 메소드를 구현하기만 하면 인터페이스 사용가능
22.
Go루틴(Goroutines)
• OS 쓰레드보다경량화된 Go 쓰레드
• 같은 메모리 공간을 공유해서 다른 Goroutines을 병렬로 실행한다.
• 스택에서 작은 메모리로 시작 필요에 따라 힙 메모리에 할당/해제
• 프로그래머는 쓰레드를 처리하지 않고 마찬가지로 OS도 Goroutine의 존재를 모른다.
• OS관점에서는 Goroutine은 C program의 event-driven형식처럼 작동한다.
23.
Goroutines vs OSthreads
Goroutines OS threads
Memory
consumption
4kb 1mb
Setup and
teardown costs
pretty cheap high-priced
Switching costs
(saved/restored)
Program Counter,
Stack Pointer and DX.
ALL registers, that is, 16 general
purpose registers, PC (Program
Counter), SP (Stack Pointer),
Google Confidential andProprietary
GC Algorithm Phases
Off
Stack scan
Mark
Mark termination
Sweep
Off
Correctness proofs in literature (see me)
WBon
STW
GC disabled
Pointer writes are just memory writes: *slot = ptr
Collect pointers from globals and goroutine stacks
Stacks scanned at preemption points
Mark objects and follow pointers until pointer queue is empty
Write barrier tracks pointer changes by mutator
Rescan globals/changed stacks, finish marking, shrink stacks, …
Literature contains non-STW algorithms: keeping it simple for now
Reclaim unmarked objects as needed
Adjust GC pacing for next cycle
Rinse and repeat
Memory model
• Go에서각 변수는 참조가 존재하는 한 계속 남아 있다.
• 가능하면 Go 컴파일러는 변수를 함수의 스택 프레임(stack frame)에 있는
함수에 지역변수로 할당한다.
• 컴파일러가 함수가 리턴되고 난 후에 변수가 참조되지 않는다는 것을 증명할 수 없다면 컴
파일러는 dangling pointer 오류를 피하기 위해서 가비지 콜렉션이 되는 힙에 변수를
할당해야만 한다.