O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
•
•
•
•
•
•
•
•

•

•
•
•
•
•

Roots
•
•
•

Roots
•
•
•

Roots
+
+
+

-

-
•
•

root

1

2

0

1

2

0

1

0

2

3

1
•
•

root

1

2

0

1

2

0

1

0

2

3

1
•
•

root

1

1

2

1

1

0

2

1
•
•

root

1

1

2

1

1

1
•
•

root

1

1

2

1

1

1

1

1

1

1
+
+
+
-

-
•
•
•

•
•
•
old space
root

a

b

new space
c

d
•
•
•

•
•
•
old space
root

a

b

new space
c

d
•
•
•

•
•
•
old space
b

new space
d

root

a

c
•
•
•

•
•
•

new space

old space
root

a

c
+
+
+
+
-
Tracing
Collection style

Reference
counting

Copying
collections

batch

incremental

copy

long

short

long

Real Time
...
Roots

Processes
global

stack
stack
stack

CPU registers
Roots

Processes
global

stack
stack
stack

CPU registers
Roots

Processes
global

stack
stack
stack

CPU registers
•

public class Fin
{
public FileStream fs;

Fin()
{
fs = new FileStream("text.txt", FileMode. Create);
}
~Fin()
{
fs.Clos...
•
•

•
•

•
•

•
•
a

b

d

g

c

e

h

Finalization
queue

f

i

F-reachable
queue
c
d
e
a

b

d

g

h

c

e

h

Finalization
queue

f

i

F-reachable
queue
c
d
e
a

b

d

g

h

c

e

h

Finalization
queue

f

i

F-reachable
queue
d

a

b

d

h

c

e

c

f

e

h

F-reachable
queue

Finalization
queue
d

a

b

d

h

f

e

e

h

F-reachable
queue

Finalization
queue
d

a

b

h

f

d

Finalization
queue
h

F-reachable
queue
•

•

~Fin()
{
someGlobalVar = this;
}
•
•
•

•
•
•

•
a

b

c

d

e

d

e

0
a

b

c

0
a

b

1

d

0
a

b

d

f

g

h

b

f

d

d

2

g

k

i

h

0

1
a

j

j

k

0

1
a

i

g

i

1

0
•

•
•

•
•
•
•

public void WriteToFile(string s)
{
TextWriter tw = new StreamWriter("text.txt", true);
tw.Write("new text");
TextW...
•

// For not-sealed classes
protected virtual void Dispose(bool disposing) { }
// For sealed classes
private void Dispose...
•
•

•

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
void Dispose(bool disposing)
{
if (disposing)
{
// Managed resources
}
// Unmanaged resources
}

•
~Fin()
{
Dispose(false)...
•

using(TextWriter tw = new StreamWriter("text.txt", true))
{
tw.Write("new text");
}
•

•
•

•
•

•
•

•
•
•
•

•
•

•
•
•

•
•

"Incremental garbage collection fixes the problem by dividing
the work of a GC into smaller pieces. Rather than do...
•

•
•

•
Garbage Collection in .NET
Garbage Collection in .NET
Próximos SlideShares
Carregando em…5
×

Garbage Collection in .NET

768 visualizações

Publicada em

Memory management algorithms overview, explanation how garbage collector works in .NET? comparing to other systems.

Publicada em: Tecnologia
  • Seja o primeiro a comentar

Garbage Collection in .NET

  1. 1. • • • • • •
  2. 2. • • • • •
  3. 3. • • • Roots
  4. 4. • • • Roots
  5. 5. • • • Roots
  6. 6. + + + - -
  7. 7. • • root 1 2 0 1 2 0 1 0 2 3 1
  8. 8. • • root 1 2 0 1 2 0 1 0 2 3 1
  9. 9. • • root 1 1 2 1 1 0 2 1
  10. 10. • • root 1 1 2 1 1 1
  11. 11. • • root 1 1 2 1 1 1 1 1 1 1
  12. 12. + + + - -
  13. 13. • • • • • • old space root a b new space c d
  14. 14. • • • • • • old space root a b new space c d
  15. 15. • • • • • • old space b new space d root a c
  16. 16. • • • • • • new space old space root a c
  17. 17. + + + + -
  18. 18. Tracing Collection style Reference counting Copying collections batch incremental copy long short long Real Time no yes no Delayed Reclamation yes no no none high low yes no yes Pause Times Cost per mutation Collects cycles
  19. 19. Roots Processes global stack stack stack CPU registers
  20. 20. Roots Processes global stack stack stack CPU registers
  21. 21. Roots Processes global stack stack stack CPU registers
  22. 22. • public class Fin { public FileStream fs; Fin() { fs = new FileStream("text.txt", FileMode. Create); } ~Fin() { fs.Close(); } }
  23. 23. • • • • • • • •
  24. 24. a b d g c e h Finalization queue f i F-reachable queue
  25. 25. c d e a b d g h c e h Finalization queue f i F-reachable queue
  26. 26. c d e a b d g h c e h Finalization queue f i F-reachable queue
  27. 27. d a b d h c e c f e h F-reachable queue Finalization queue
  28. 28. d a b d h f e e h F-reachable queue Finalization queue
  29. 29. d a b h f d Finalization queue h F-reachable queue
  30. 30. • • ~Fin() { someGlobalVar = this; }
  31. 31. • • • • • • •
  32. 32. a b c d e d e 0 a b c 0 a b 1 d 0
  33. 33. a b d f g h b f d d 2 g k i h 0 1 a j j k 0 1 a i g i 1 0
  34. 34. • • • •
  35. 35. • • • public void WriteToFile(string s) { TextWriter tw = new StreamWriter("text.txt", true); tw.Write("new text"); TextWriter tw2 = new StreamWriter("text.txt", true); //??? }
  36. 36. • // For not-sealed classes protected virtual void Dispose(bool disposing) { } // For sealed classes private void Dispose(bool disposing) { } • • •
  37. 37. • • • public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
  38. 38. void Dispose(bool disposing) { if (disposing) { // Managed resources } // Unmanaged resources } • ~Fin() { Dispose(false); }
  39. 39. • using(TextWriter tw = new StreamWriter("text.txt", true)) { tw.Write("new text"); }
  40. 40. • • • • • •
  41. 41. • • • •
  42. 42. • • • • •
  43. 43. • • • "Incremental garbage collection fixes the problem by dividing the work of a GC into smaller pieces. Rather than do a 500 millisecond garbage collection, an incremental collector might divide the work into fifty slices, each taking 10ms to complete. In between the slices, Firefox is free to respond to mouse clicks and draw animations.“ http://blog.mozilla.org/javascript/2012/08/28/incremental-gc-in-firefox-16/
  44. 44. • • • •

×