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