English 字幕

← Spotting Leaks In Memory Monitor


Showing Revision 4 created 05/24/2016 by Udacity Robot.

  1. All right, so
    let's talk about memory leaks.
  2. Memory leaks are sneaky.
  3. They can be slow and insidious,
    sometimes taking days or
  4. weeks before you even
    realize that you have one.
  5. In fact, you might only realize
    memory's an issue when your users start
  6. complaining about mysterious slowdowns
    that happen after using your app.
  7. Don't let this happen to you.
  8. Fortunately, with some patience,
    a perf mindset, and the right tools,
  9. you'll have the opportunity to abolish
    these leaks from your app, period.
  10. We'll use Memory Monitor to watch
    the behavior of a leak in action, and
  11. then in the next video, we'll use Heat
    Viewer to gain initial confirmation.
  12. Now let's look at a micro example
    of what a leak can look like, and
  13. see how the SDK tools can
    help us identify such a leak.
  14. In this example, we're going to go
    ahead and rotate the device for
  15. a few minutes and
    profile it with Memory Monitor.
  16. This is by design to showcase a common
    leak situation that can arise during
  17. the creation and
    destruction of an activity.
  18. We can intentionally trigger this cycle
    by changing the device's orientation.
  19. And yes, I know, it may seem that
    this is a totally weird thing to do,
  20. but we're going to do this to
    demonstrate how a leak may happen and
  21. to show how they can be slow and
  22. Now, in the first pass, the leak slowly
    consumes the free memory available to
  23. your app, until eventually it causes
    a garbage collection or GC event.
  24. More important, the key thing to
    notice is, the garbage collector
  25. isn't able to reclaim that much
    energy due to the leak in the app.
  26. And then, eventually,
  27. a second GC event occurs much sooner,
    about 30 seconds later.
  28. Now, notice when the leak
    consumes all of the free memory,
  29. Android actually adjusts and
    grants the app a higher memory ceiling.
  30. While this is a nice adjustment by
    the system, if the leak isn't fixed,
  31. it will keep consuming memory until the
    system can no longer allocate any more.
  32. This will slow the performance
    of the device and
  33. eventually lead to your app crashing.
  34. You can wait a little bit longer,
    and the third GC will occur.
  35. And then a fourth somewhat
    similar to the first two.
  36. Now, as you can see,
    the pattern continues, and
  37. more memory is allocated by the system.
  38. You can also similar
    behavior using Heat Viewer.