Return to Video

Improve Your Code To Reduce Churn

  • 0:00 - 0:03
    We can make a small adjustment in
    our code to prevent excessive churn.
  • 0:03 - 0:05
    Let's take a look at a comparison view.
  • 0:05 - 0:08
    Rather than concatenate one cell value
    at a time to build each row, let's use
  • 0:08 - 0:12
    a StringBuilder instance, and construct
    each row using a single string.
  • 0:12 - 0:14
    Note that the StringBuilder's
    instantiated outside the loop,
  • 0:14 - 0:16
    and thus its memory is allocated once.
  • 0:16 - 0:18
    And then we simply use
    it as a buffer for
  • 0:18 - 0:21
    each iteration of the loop where we
    first clear it and then we append
  • 0:21 - 0:24
    a single string of ints to represent
    the row for that loop iteration.
  • 0:24 - 0:27
    Now see the instructor notes for
    more details into this code segment.
  • 0:27 - 0:28
    All right, now it's time to verify.
  • 0:28 - 0:31
    You want to go ahead and
    load the improved branch of code,
  • 0:31 - 0:36
    which is called memory_churn_optimized,
    into both trace view and memory monitor
  • 0:36 - 0:40
    to confirm we've reduced the amount of
    GC's occurring in the short time window.
  • 0:40 - 0:43
    You may also use allocation
    tracker to verify.
  • 0:43 - 0:44
    If you use allocation tracker,
  • 0:44 - 0:48
    or if you got something unexpected
    in trace view, or memory monitor,
  • 0:48 - 0:51
    share a screenshot of your
    output in the discussion forums.
  • 0:51 - 0:52
    We're interested in
    seeing what you've got.
  • 0:53 - 0:59
    Now for us, even with these changes,
    the Perf pirate still pauses.
  • 0:59 - 1:00
    But this time for less time.
  • 1:01 - 1:03
    Now at this point, this also might mean
    that this function is probably a good
  • 1:03 - 1:05
    candidate to be backgrounded.
Title:
Improve Your Code To Reduce Churn
Description:

04-15 Improve Your Code To Reduce Churn

more » « less
Video Language:
English
Team:
Udacity
Project:
ud825 - Android Performance
Duration:
01:05

English, British subtitles

Revisions