pro typescript.ch07.exception, memory, performance

27
Pro TypeScript Ch.07 Exception, Memory, Performance 윤윤윤 ([email protected])

Upload: seok-joon-yun

Post on 13-Apr-2017

226 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Pro typescript.ch07.Exception, Memory, Performance

Pro TypeScriptCh.07 Exception, Memory, Perfor-mance

윤석준 ([email protected])

Page 2: Pro typescript.ch07.Exception, Memory, Performance

2

Agenda Exceptions Memory Performance

Page 3: Pro typescript.ch07.Exception, Memory, Performance

프로그램이 계속해서 처리가 불가능 함을 나타내기 위해 사용Program Logic 처리를 위해 사용하는 것은 부적절

Exceptions예외

Page 4: Pro typescript.ch07.Exception, Memory, Performance

4

Exception

발생한 예외는 JavaScript Console 에 표시됨 Windows, Linux : Ctrl + Shift + I or F12 Mac : Cmd + Opt + I

별도 처리 가능

Page 5: Pro typescript.ch07.Exception, Memory, Performance

5

Throwing Exceptions

Page 6: Pro typescript.ch07.Exception, Memory, Performance

6

Throwing Exceptions

Error 를 상속받았지만 별 다른 구현 없음

-> Error 타입만으로 구분되는 것으로도 의미가 있음

toString() 를 구현하여 Console 에 출력되는 메세지를 직접 정의

Page 7: Pro typescript.ch07.Exception, Memory, Performance

7

Throwing Exceptions 우리가 작성한 코드에서 발생한 Error 와

다른 곳에서 발생한 Error 의 구분이 가능

Page 8: Pro typescript.ch07.Exception, Memory, Performance

8

Exception Handling

별도의 처리를 하지 않으면 프로그램 종료 원할 경우 try – catch – finally 블록으로 감싸줘서 처리

Page 9: Pro typescript.ch07.Exception, Memory, Performance

9

Exception Handling

모든 종류의 Error 에 대해서 처리 비정상적인 상태로 계속 수행될 가능성이 있음

Page 10: Pro typescript.ch07.Exception, Memory, Performance

10

Exception Handling

처리 가능한 Error 만 처리 나머지는 다시 통과시키는 것이 바람직함

Page 11: Pro typescript.ch07.Exception, Memory, Performance

11

Error class hierarchy

Page 12: Pro typescript.ch07.Exception, Memory, Performance

12

Error class hierarchy

Page 13: Pro typescript.ch07.Exception, Memory, Performance

13

Error class hierarchy

Page 14: Pro typescript.ch07.Exception, Memory, Performance

관련 오류 중 Out Of Memory 같은 것을 제외하고는 대부분 자동으로 처리자원 해제 , 가비지 콜렉션에 대해서 설명

Memory메모리

Page 15: Pro typescript.ch07.Exception, Memory, Performance

15

Releasing Resources

대부분 API 들은 비동기 패턴으로 되어 있는 경우가 많음 오류 발생을 대비해서 try - finally 블록을

이용해자원해제를 보장해 주어야 함

Page 16: Pro typescript.ch07.Exception, Memory, Performance

16

Releasing Resources

sonsor 의 동작 open, read, sensorChange 어디에서 Error 가 발생하더라도 close 가

호출되는 것을 보장

Page 17: Pro typescript.ch07.Exception, Memory, Performance

17

Garbage Collection Reference Count ( 예전 방식 )

- Reference Count 가 0 이 되었을 경우 메모리

해제

- Circular Reference 의 경우 해제되지 않을 수

있음

Page 18: Pro typescript.ch07.Exception, Memory, Performance

18

Garbage Collection Mark and Sweep ( 최신 방식 )

- Root 에서 도달 가능한 객체를 찾은 뒤 , 나머지 객체를 해제

- 수행시간은 더 걸리지만 , 메모리 누수 발생 가능성을 줄여줌

Page 19: Pro typescript.ch07.Exception, Memory, Performance

성능 최적화와 프로그램 디자인은 trade-off 관계TypeScript, JavaScript 의 경우 여러 브라우저에서 테스트 해야 함수치적인 측정이 필요

Performance성능

Page 20: Pro typescript.ch07.Exception, Memory, Performance

20

Performance Measurement대상 코드

테스트 코드

Page 21: Pro typescript.ch07.Exception, Memory, Performance

21

Performance Measurement

Page 22: Pro typescript.ch07.Exception, Memory, Performance

22

Performance Measurement

여러번 반복하여 평균을 구하는 방법으로 측정

Page 23: Pro typescript.ch07.Exception, Memory, Performance

23

Performance Measurement 2 만번 수행 결과 : 2.73 ms

Page 24: Pro typescript.ch07.Exception, Memory, Performance

24

Performance Measurement 2 만번 수행 결과 : 214.45 ms Error 가 있는 경우 수행 결과가 바뀔 수 있음

Page 25: Pro typescript.ch07.Exception, Memory, Performance

Summary

Page 26: Pro typescript.ch07.Exception, Memory, Performance

26

Summary 예외 처리를 하면 비정상적으로 종료되는 현상 방지 가능

사용자 정의 예외를 작성하면 오류의 구분이 가능

해결 가능한 예외만 catch 절에서 처리

비동기 API 사용시 try-catch 절을 이용하여 자원을 관리하는 것이 좋음

요즘 브라우저들은 mark-and-sweep 알고리즘을 이용

가비지 컬렉터를 고려해야할 만큼 성능이 중요하다면 객체를 작은 크기로 생성

최적화 작업에 앞서 수치적으로 성능 측정

Page 27: Pro typescript.ch07.Exception, Memory, Performance

Thanks

상세설명 https://goo.gl/Ptw1aI