Talking trash

download Talking trash

of 79

  • date post

    20-Jan-2015
  • Category

    Technology

  • view

    5.317
  • download

    4

Embed Size (px)

description

Presentation given at Flash the City conference in New York, 2011

Transcript of Talking trash

  • 1. Talking Trash Michael Labriola Senior Consultant Digital Primates @mlabriola Page 0 of 59

2. Who am I?

  • Michael Labriola
  • Senior Consultant
  • Digital Primates
  • Client side architect specializing in Adobe Flex
    • Lead architect and developer of FlexUnit 4
    • Benevolent Dictator of the Open Spoon Project
  • Team Mentor
  • Architect of applications deployed worldwide
  • Fan of disassembling things

Page 2 of 59 3. What are we going to cover?

  • Memory in Flash Player
  • Garbage Collection as it exists today
  • A tiny bit of coming soon
  • Some wild speculation on whats next

Page 3 of 59 4. Disclaimer

  • I am going to tell you lies.
  • Even at this depth, we will need to gloss over some details and treat them in a simplified manner to make it through.
  • Further, most of this has been learned by reading code from the Tamarin project. It is likely the same as in Flash Player, but I cant know for sure.

Page 3 of 59 5. Memories

  • You have some choices in (programming) life.
  • You can take full responsibility for asking for memory and returning it when you are done.
  • You can employ another entity to do that work for you.
  • You can do a combination of those two, but seriously, lets keep this simple.

Page 3 of 59 6. What is GC

  • GC (Garbage Collection) is a way to automatically manage memory.
  • As an oversimplification lets think about it this way
  • GC proxies our access to memory:

Page 3 of 59 Memory GC Program Code 7. Why Proxy?

  • By proxying our access to the memory our program needs, GC can keep track of what objects are using memory. This removes the responsibility of remembering when and where to free (deallocate) memory.
  • To understand what GC is trying to do for you, lets look at what can go wrong if you try to do it manually.

Page 3 of 59 8. Dangling References

  • If you manually allocate and free memory you can cause dangling references. What happens if the memory below does not exist when it is accessed?

Page 3 of 59 Memory p1 p2 memory = allocate( size ); p1 = memory; p2 = memory; free( memory ); trace( p1.toString() ); trace( p2.toString() ); 9. Double Free

  • What happens if you accidently try to free the same memory twice? What if it has already been reused?

Page 3 of 59 Memory2 memory1 = allocate( size ); free( memory1 ); memory2 = allocate( size ); free( memory1 ); 10. Real Leaks

  • What happens if you dont free the memory before you lose access to it?

Page 3 of 59 Memory2 p1 p1 = allocate( size ); p2 = allocate( size ); p1 = p2; Memory1 11. GC Instead

  • GC takes care of these situations by determining when a chunk of memory is no longer accessible and freeing it.
  • This is helpful, but GC will never understand your program logic, meaning that it will need to do more work to determine things you already know (i.e., you no longer need a chunk of memory).
  • So, GC has advantages but will also necessarily be slower and more complicated.

Page 3 of 59 12. GC How

  • The rest of this presentation is about that complication.
  • It is about how GC decides when and where to allocate and free memory and all of the overhead and processes that go into that decision.

Page 3 of 59 13. Allocation

  • Flash Player uses a page allocator (GCHeap) to allocate large chunks from system memory (megabytes). In turn GCHeap gives 4k chunks (pages) to the GC memory manager as needed.

Page 3 of 59 System Memory 4k 4k 4k GCHeap GC 14. Pages

  • Those 4k pages are then used by GC to provide memory for many objects in the system.

Page 3 of 59 4k GC Object A Object B Object C 15. Much

  • The memory we are discussing right now is called heap memory. It is responsible for *much* but not all of the storage in the system
  • For our purposes, there are two types of memory we care about: heap and stack.

Page 3 of 59 16. Stack

  • A stack is a data structure (think pile of papers on your desk) which is managed by two operations: pushing and popping. Pushing means adding something to the top stack, popping means removing.

In this data structure, the only way to get something in the middle is the remove the things above it. Item 1 Item 2 Item 1 Item 2 Item 3 17.

  • Now imagine removing (popping) item 3 and adding a new (pushing) item 4. In Flash Player we have a memory structure like this, affectionately called the stack.

Altering the Stack Item 1 Item 2 Item 1 Item 2 Item 3 Item 1 Item 2 Item 4 18. Local Variable Declaration

  • As you declare local method variables in ActionScript, they are pushed onto the stack.

function doThing() { var a:int; var b:int; var c:Number; } a b c 19. Method Calls

  • As you call methods, each of their local variables are pushed onto a stack as well.

function doThing() { var a:int; var b:int; var c:Number; someMethod(); } function someMethod() { var x:int; } a b c x 20. Method Calls

  • There is also other information contained on the stack about where to return when this method is complete.

function doThing() { var a:int; var b:int; var c:Number; someMethod(); } function someMethod() { var x:int; } a b c x doThing 21. Method Calls

  • Stack memory is frequently reused as items are pushed onto and popped off the stack.

function doThing() { var a:int; var b:int; var c:Number; someMethod1(); someMethod2(); } function someMethod()1 { var x:int; } function someMethod2() { var y:int; } a b c y doThing 22. Instances

  • References to complex objects may also exist on the stack, but memory allocated for those objects comes from the heap.

Stack Memory Heap Memory function doThing() { var a:int; var o:Object; o = new Object(); } a o Object A 23. Instances

  • Arguments passed to methods are also pushed into stack memory.

Stack Memory Heap Memory function doThing() { var a:int; var o1:Object; o1 = new Object(); doIt( o1 ); } function doIt( o2 ) { var b:int; } Object A a o1 doIt o2 b 24. Instances

  • When a method returns, the stack is unwound so that the memory can be reused.

function doThing() { var a:int; var o1:Object; o1 = new Object(); doIt( o1 ); } function doIt( o2 ) { var b:int; } Stack Memory Heap Memory a o1 Object A 25. Freeing

  • Now that we have memory and its all in use, we need to find a way to deallocate (free) it.
  • The premise behind GC is that we only free memory that is no longer needed. So how do we figure out how things are no longer needed?
  • First, some terms.

Page 3 of 59 26. Roots

  • In traditional programming models, you manage your own memory. Lets call this unmanaged memory.
  • In a managed memory model, we ask the system to manage the lifetime (birth through death) of our memory.
  • The first chunk of managed memory asked for from unmanaged memory is called a root, or a GCRoot.
  • The garbage collector is aware of all roots. They serve as a starting point for things we are about to cover.

Page 3 of 59 27. Hybrid

  • The current Flash Player is aconservative mark and sweep garbage collector with deferred reference counting.
  • Lets take those in reverse.

Page 3 of 59 28. Reference Counting

  • Reference counting is a very easy-to-understand way of keeping track of objects that can be removed from the system.
  • Imagine each object has an extra property called refCount that keeps track of the number of references to that object.

Page 3 of 59 29. Reference Counting

  • Looking at the following code, how many references does the object assigned toahave? How many doesbhave? For our purposes, assumeaandbare global variables.

Page 3 of 59 ObjectA a = new ObjectA(); b = new ObjectB(); b.prop = a ; ObjectB a b ObjectA: 2 ObjectB: 1 30. Reference Counting

  • What if we setato null? Can anything be collected?

Page 3 of 59 ObjectA a = new ObjectA(); b = new ObjectB(); b.prop = a; a = null; ObjectB a b ObjectA: 1 ObjectB: 1 31. Reference Counting

  • What if instead we setbto null? Can anything be collected?

Page 3 of 59 ObjectA a = new ObjectA(); b = new ObjectB(); b.prop = a; b = null; ObjectB a b ObjectA: 2 ObjectB: 0 32. Reference Counting

  • What if instead we setaandbto null? Notice how this will eventually cascade?

Page 3 of 59 ObjectA a = new ObjectA(); b = new ObjectB(); b.prop = a; a = b = null; ObjectB a b ObjectA: 1 ObjectB: 0 33. Circular Reference

  • What ifaalso referencesb ? Can these ever be collected via this technique?

Page 3 of 59 ObjectA a = new ObjectA(); b = new ObjectB(); b.prop = a; a.prop = b; a = b = null; ObjectB a b ObjectA: 1 ObjectB: 1 34. Reference Counting Issues

  • Obviously the circular reference issue is a problem for reference counting, but there are some others too.
  • It takes storage to keep