6. compile
class foo {
private int _bar;
private int *_pBar;
private static int _s_bar1=7;
private static int[100000000] _s_bar2;
public int foobar(value bar) {
int local_bar=0;
_pBar = malloc(10000000*sizeof(int));
…
}
public static staticFoobar() {
…
}
}
7. compile
모든 프로그램은
- 자료
- 흐름(의 표현)
컴파일러의 입장에서..
- 이 프로그램을 메모리에 올려야 하겠는데
-> 메모리에 빈 공간이 얼마나 필요할까?
- 어떤 자료는 컴파일 타임에 크기를 앎
- 어떤 자료는 컴파일 타임에 모름
- 흐름은??
8. compile
class foo {
private int _bar;
private int *_pBar;
private static int _s_bar1=7;
private static int[100000000] _s_bar2;
public int foobar(value bar) {
int local_bar=0;
_pBar = malloc(10000000*sizeof(int));
…
}
public static staticFoobar() {
…
}
}
sizeof(int) 는 4bytes 라 가정
compile time 에 결정!!
_s_bar1 는 4bytes
_s_bar2 는 4*100000000bytes
compile time 에 사용할지 안할지 모름!!
프로그램 어딘가에서 x = new foo(); 수행할 때 memory에 할당
10. compile
0000 0000 0000 0000 0000 0000 0000 0000
…
0000 0000 0000 0000 0000 0000 0000 0111
…
…
???? ???? ???? ???? ???? ???? ???? ???? ????
???? ???? ???? ???? ???? ???? ???? ???? ????
???? ???? ???? ???? ???? ???? ???? ???? ????
…
…
???? ???? ???? ???? ???? ???? ???? ???? ????
_s_bar1 4bytes
주소 메모리 공간(에 이렇게 쓰겠다는 약속)
_s_bar2 약 100Mbytes
어떤 값이 올지도 모르는
데 미리 100Mbytes 나 잡
아두는 것이 효율적일까?
0000 00f0
…
0000 00ff
…
…
11. compile
0000 0000 0000 0000 0000 0000 0000 0000
…
0000 0000 0000 0000 0000 0000 0000 0111 _s_bar1 4bytes
주소 메모리 공간(에 이렇게 쓰겠다는 약속)
_s_bar2
실제로는 약 100Mbytes
의 공간을 _s_bar2가 사용
하겠다 라는 기록만 저장
해 둡니다.
???? ???? ???? ???? ???? ???? ???? ???? ????
0000 00f0
…
0000 00ff
…
…
12. compile
0000 0000 0000 0000 0000 0000 0000 0000
…
0000 0000 0000 0000 0000 0000 0000 0111 _s_bar1 4bytes
주소 메모리 공간(에 이렇게 쓰겠다는 약속)
_s_bar2
???? ???? ???? ???? ???? ???? ???? ???? ????
0000 00f0
…
0000 00ff
…
…
foo@foobar(value) …
…
foo@staticFoobar() …
…
class 의 생성, 또는
method call 시 무엇을 어
떻게 해야 한다는 정보가
수록된 영역
13. compile
data section
주소 메모리 공간(에 이렇게 쓰겠다는 약속)
bss section
0000 00f0
…
0000 00ff
…
…
code section
compile 결과물
(= foo.o file )의 내용
15. link
foo.o foo2.o
+ … +
a+ =
executable image
(=
foo.exe 또는
foo.lib 또는
foo.dll)(xx.lib 또는 xx.dll)
16. link
foo.o
foo2.o
• 나중에(프로그램 실행 시점에) offset 값만 결
정되면 프로그램의 모든 영역에 접근 가능
foo.exe 또는 foo.lib 또는 foo.dll
offset (어떤 값이 될지는 모름)
offset + 1
offset + 2
offset + 3
…
17. static link
이 프로그램을 빌드하면 foo.exe 또는 foo.lib
의 사이즈는 약 400Mb 정도 될거임
foo.lib
object file 에서는 공간을
차지하지 않던 bss(초기화
되지 않은 전역변수) 영역
프로그램 생성 시점에는
해당 영역을 실제로 할당
해 두어야 함
19. dynamic link
• foo.dll은 필요할 때에만 load 됨
• 효율적이다. 하지만…
• 드디어 dll 지옥의 시작
foo2.o
+ =
foo2.exe
foo.dll
foo3.o
+ =
foo3.exe
foo.dll
foo.dll
20. runtime
class foo {
private int _bar;
private int *_pBar;
private static int _s_bar1=7;
private static int[100000000] _s_bar2;
public int foobar(value bar) {
int local_bar=0;
_pBar = malloc(10000000*sizeof(int));
…
}
public static staticFoobar() {
…
}
}
sizeof(int) 는 4bytes 라 가정
_pBar 는 동적 할당 변수,
약 400mb 크기의 memory를 할당할 수 있을
지 없을지는 시도해봐야 알 수 있다.
local_bar 는 지역변수, foobar()안에서 잠깐 쓰고 반환
25. dll 지옥
class foo {
private int _bar;
private int *_pBar;
private static int _s_bar1=7;
private static int[777777777] _s_bar2;
public int foobar(value bar) {
int local_bar=0;
_pBar = malloc(777777777*sizeof(int));
…
}
public static staticFoobar() {
…
}
}
_s_bar2 사이즈 변경
_pBar 사이즈 변경
• 변경된 foo.dll 을 v2.0 / 기존 foo.dll 을 v1.0 이라 하자
26. dll 지옥
• foo.dll은 system memory에 한 번만 load 되어 공유
• 현재 memory에 load 된 foo.dll 은 v1.0
• foo.dll v2.0 을 사용하는 foo3.exe는 어떤 동작을 할지 알 수
없다!!!
foo2.exe
foo.dll (v1.0)
foo3.exe
foo.dll (v2.0)
system memory
foo.dll (v1.0)
27. dll 지옥
• system directory 한군데에 모든 dll이 떡져있다
case1 : 윈도우 업데이트로 인한 system library 변경
case2 : 응용 프로그래머가 임의로 system library 변경
과거 MS Windows shared library 저장소
winsock.dll
c:windowssystem
msvcrt.dll
windows.dll
….
28. dll 지옥
• main 영역 / 임시 영역으로 이원화
• library 관리 책임이 사용자에게 있음
-> 사용자가 멋대로 library 업데이트 한다면?
-> 윈도우와 상황이 다르지 않다.
linux shared library 저장소
libc.so
/usr/lib
libpthread.so
glib.so
….
libc.so
/usr/local/lib
libpthread.so
glib.so
….
29. jar 지옥
• 내가 작성한 program 이 어느 jar 를 참조할지 모른다!!
• dll 지옥과 동일한 문제 발생 !!
java class path
/path/A
foo.jar (old)
/path/B
foo.jar (new)
30. dependency 지옥의 원인?
• PEBKAC(http://en.wikipedia.org/wiki/User_error)
• 프로그램은 거짓말을 하지 않아요
• 모든 문제의 원인은 닝겐입니다
31. 해결
• library metadata
– 빌드 날짜 / 크기 / 유니크 키 등을 빌드 시점에 적어둠
• 버전 관리되는 build/package manager
– 모든 종속성 library 의 버전을 관리하는 도구 사용
• virtual environment
– 가상환경에 library를 넣어두면 설령 떡지더라도 가상환경
만 재구성하면 된다