Wp7 - TextBlock with lots of text - huge memory - how to avoid this?

I have a problem in my application and I do not know if this is normal or not. I have a text block in my application that should display a large amount of text (2000-4000 characters). Anyway, there is a limit of 2048 pixels, I think my text is cropped, this is not a 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 a very long text hack with the link above, the text block consumes about 10 MB of memory! If I use ScrollableTextBlock from the link above, the amount of memory will be even larger and reach 30-40 mb. No limit. So it seems that memory usage is related to the allocated area ...

Is there a way to reduce memory usage for long texts? Does BitmapCach have anything related to this problem, and can I disable it? You can easily reproduce this problem by simply adding a text block with very long text, and you can check the memory usage with this code, you will see that with one text block with long text, the peak memory increases by 10 mb 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); 
+3
source share
3 answers

I encountered similar problems while creating Overflow7

The problems I ran into are related to the fact that if you use the StackPanel inside the ScrollViewer, the ScrollViewer insists that the entire StackPanel is displayed, not just the visible part.

I read, and there were two general solutions:

  • use UI virtualization methods - for example, VirtualizingStackPanel
  • Use data virtualization methods - for example, creating your own paging.

To get around this in Overflow7, I used ListBoxes instead of compiling ScrollViewer / StackPanel. The inside of the ListBox uses a VirtualizingStackPanel - and this VirtualizingStackPanel only displays what is displayed on the screen, and not the entire scrollable client area.

It was a bit “hacked,” but it worked fine. If you have the time, I think that the best solution would be to improve the implementation of ScrollableTextBlock so that it uses VirtualizingStackPanel - there are good posts on how to use this (for example) WPF VirtualizingStackPanel to improve performance

+3
source

Large amounts of text in a single control typically require more memory than expected. As mentioned earlier, you can print text or perform dynamic loading, where only packets of text for the visible area are loaded. This way you will not store a large string in memory.

In your case, caching will involve reusing and reloading the content when the user switches to another page, rather than the initial process of loading and processing the content.

0
source

I know this is an old question, however I wanted to add another solution.

http://blogs.msdn.com/b/stankovski/archive/2013/08/27/yet-another-scrollable-textblock-for-windows-phone.aspx

To accomplish my task, I encapsulated the logic of "splitting" into a separate class that outputs the output as a list of strings. Then you can associate this list with your favorite ListBox and voila control, you have a text block. Separation logic has been optimized for performance, so you get much better processing time and then Alex's ScrollableTextBlock. In addition, since you can bind a list to any ListBox control that supports virtualization, you will have a much more conservative amount of memory.

0
source

Source: https://habr.com/ru/post/894353/


All Articles