Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

windows phone 7 - wp7 - TextBlock with a lot of text - huge memory usage - how to avoid it?

I have a problem in my app and I don't know if it is normal or not. I have a textblock in my application that needs to display a large amount of text (2000-4000 chars). Anyway there is a limit of 2048 pixels I think so my text is cropped, this is no problem, I use this : http://blogs.msdn.com/b/priozersk/archive/2010/09/08/creating-scrollable-textblock-for-wp7.aspx .

The problem is that the longer the text the more memory it consumes. Without the very long text hack from the link above, the textblock consumes around 10mb of memory! If I use the ScrollableTextBlock from the link above, the amount of memory is going even further and can reach up to 30-40 mb.. There is no limit. So it seems that the memory usage is related with the area that is drawn...

Is there a way to reduce memory usage for long texts? Has BitmapCach anything to do with this problem and can I disable it? You can easily reproduce this problem by just adding a textblock with a very long text and you can check memory usage with this code, you will see that with just 1 textblock with long text, the peak memory goes up by 10mb or more:

        long deviceTotalMemory = (long)DeviceExtendedProperties.GetValue("DeviceTotalMemory");
        long applicationCurrentMemoryUsage = (long)DeviceExtendedProperties.GetValue("ApplicationCurrentMemoryUsage");
        long applicationPeakMemoryUsage = (long)DeviceExtendedProperties.GetValue("ApplicationPeakMemoryUsage");

        Debug.WriteLine("### deviceTotalMemory             : " + deviceTotalMemory);
        Debug.WriteLine("### applicationCurrentMemoryUsage : " + applicationCurrentMemoryUsage);
        Debug.WriteLine("### applicationPeakMemoryUsage    : " + applicationPeakMemoryUsage);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I came across similar issues when creating Overflow7

The problems I encountered were to do with the fact that if you use a StackPanel inside a ScrollViewer, then the ScrollViewer insists that all of the StackPanel is rendered, not just the visible portion.

I read around and there were 2 general solutions:

  • use UI virtualization techniques - e.g. VirtualizingStackPanel
  • use data virtualization techniques - e.g. crafting your own paging

To get round this in Overflow7 I made use of ListBoxes instead of the ScrollViewer/StackPanel combo. The internals part of ListBox use a VirtualizingStackPanel - and this VirtualizingStackPanel renders just whats on the screen, not the entire scrollable client area.

This was a bit "hacky" but worked well. If you have time, then I believe a better solution would be to improve the ScrollableTextBlock implementation so that it uses VirtualizingStackPanel - there are good posts about how to use this on (for example) WPF VirtualizingStackPanel for increased performance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...