gerenciamento de memória em swift - the weak, the strong, and the unowned

70
Txai Wieser Swift: Gerenciamento de Memória

Upload: txai-wieser

Post on 20-Mar-2017

279 views

Category:

Mobile


0 download

TRANSCRIPT

Txai Wieser

Swift: Gerenciamento de Memória

Txai Wieser

• Estudante de engenharia de computação na UFRGS.

• Desenvolvedor iOS

BallphyFityItWarrenRecolorindo o ReinoRepel

Swift: Gerenciamento de Memória

MANUAL

Gerenciador de Memória

AUTOMÁTICO

release

retain

MANUAL

Gerenciador de Memória

Programador

alloc

AUTOMÁTICO

heap

ObjectAlloc

vmmap

MallocScribble

free

malloc

release

retainautoreleasepool

MANUAL

destroy

Gerenciador de Memória

Programador

alloccopy

autoreleaseAUTOMÁTICO

assign

__autoreleasing

__unsafe_unretained

Gerenciador de Memória

heap

ObjectAlloc

vmmap

MallocScribble

free

malloc

release

retainautoreleasepool

MANUAL

destroy

Programador

alloccopy

autoreleaseAUTOMÁTICO

assign

__autoreleasing

__unsafe_unretained

Programador Garbage Collector

AUTOMÁTICOMANUAL

Gerenciador de Memória

Programador Garbage Collector

AUTOMÁTICOMANUAL

Gerenciador de Memória

(+)determinístico

(-)trabalhoso

(+)automático

(-)afeta o runtime

Programador Garbage Collector

AUTOMÁTICO

Compilador

MANUAL

Gerenciador de Memória

ARC

(+)determinístico

(-)trabalhoso

(+)automático

(-)afeta o runtime

ARC Garbage Collector

Tracing garbage collection is a form of automatic memory management that consists of determining which objects should be deallocated by tracing which objects are reachable by a chain of references f r o m c e r t a i n “ r o o t ” objects, and considering the rest as “garbage” and collecting them.

Reference counting is a technique of storing the number of references, pointers, or handles to a resource such as an object, block of memory, disk space or other resource. It may a l s o r e f e r , m o r e specifically, to a garbage collection algorithm that uses these reference counts to deallocate objects which are no longer referenced.

ref1

nil

Automatic Reference Counting strong

ref1

nil

Automatic Reference Counting strong

1

ref1

nil

Automatic Reference Counting strong

1ref2

ref1

nil

Automatic Reference Counting strong

2ref2

ref1

nil

Automatic Reference Counting strong

1ref2

ref1

nil

Automatic Reference Counting strong

0ref2

ref1

Automatic Reference Counting strong

ref2

!?

Ciclos de Referências

pessoa carro

weakARC (Vida Independente)

1 1

pessoa carro

weakARC (Vida Independente)

2 2

pessoa carro

weakARC (Vida Independente)

1 1

nil nil

pessoa carro

weakARC (Vida Independente)

1 1

nil nil

weakARC (Vida Independente)

pessoa carro

1 2

weakARC (Vida Independente)

pessoa carro

nil nil

0 1

weakARC (Vida Independente)

pessoa carro

nil nil

0

nil

weakARC (Vida Independente)

pessoa carro

nil nil

weak(Vidas Independentes)

unownedARC (Vida Dependente)

pessoa

2

1

unownedARC (Vida Dependente)

pessoa

1

1

nil

unownedARC (Vida Dependente)

pessoa

1

1

unownedARC (Vida Dependente)

0

1

pessoanil

unowned(Vida Dependente)

cena

{…}

Closures

2

1

cena

{…}

Closures

2

1

cena

{…}

Closures

1

1

nil

cena

{…}

Closures

2

1

cena

{…}

Closures

2

1

Closures

Closures

Closures(Lista de Captura)

Prevenção

• Relações de Posse

• NSTimer

Cuidado Use logs:

-(void)dealloc {

NSLog(@"object is being deallocated"); }

deinit { println("object is being deallocated") }

• Closures

• Referencias para classes

• UIAlertController

strong weak unowned

Value Types Reference Types

✅ ⚠

debugging

Referências:

• https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

• http://www.ibm.com/developerworks/library/mo-ios-memory/

Thanks ;)

github.com/txaidw

twitter.com/txaidw