0

I have a Windows Phone 8 app which has ListBox to display some items for user. Now, all of a sudden, there were more than 300 items for a particular user and when I was displaying it using ListBox, the app crashed both in emulator and device due to memory exception.

DataTemplate for my listbox is below:

<ListBox x:Name="testListBox"
         Grid.Row="1"
         toolkit:TiltEffect.IsTiltEnabled="True"
         HorizontalContentAlignment="Center"
         ItemContainerStyle="{StaticResource GenericListBoxContainerStyle}"
         SelectedItem="{Binding}"
         SelectionChanged="testListBox_SelectionChanged">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="0,2,0,2"
                                      Background="White">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Image Width="80"
                                           Height="60"
                                           Stretch="Fill"
                                           Margin="4,0,4,0"
                                           Source="{Binding file_url, Converter={StaticResource Imageconverter}}"></Image>
                                    <StackPanel Grid.Column="1"
                                                Margin="0,8,0,8">

                                        <TextBlock Margin="2"
                                                   Style="{StaticResource HeaderContentStyle}"
                                                   >
                                        <Run Text=""/>
                                        <Run Text="{Binding test_id}"/>
                                        <Run Text="-"/>
                                        <Run Text="{Binding name}"/>

                                        </TextBlock>

                                        <TextBlock Text="{Binding location}"
                                                   Margin="2"
                                                   Style="{StaticResource DescriptionContentStyle}" />
                                    </StackPanel>
                 </Grid>
         </DataTemplate>
      </ListBox.ItemTemplate>

Can anyone suggest, how can I manage or release memory so that my application doesn't crash and does not show OutofMemory Exception.

Kinjan Bhavsar
  • 1,439
  • 28
  • 58

1 Answers1

1

Maybe you shouldn't show all items when page load, create simple paging which will be control via buttons or scrolling with dynamically loading items. Maybe it will be helpful:

Paging data / infinite scrolling on Windows Phone

Also maybe problem can be in pictures, look at this question :

Why do I get an OutOfMemoryException when I have images in my ListBox?

Check problem with pictures and then add paging or scrolling and I think your problem will go away.

Community
  • 1
  • 1
ivamax9
  • 2,601
  • 24
  • 33
  • Thanks Chase. I will check this. Also, i am not aware how to do paging of data. – Kinjan Bhavsar Jul 02 '15 at 09:30
  • 1
    Pagination can be realized in many ways. One of simplest - it's add two buttons (next,prev) and add handlers for them which will change for example current 5 items of your ListBox on next or prev 5 items from your items collection. Operations on collections I think I shouldn't explain for you, you can do it in many ways. Good luck. Please vote main answer if it was helpful. – ivamax9 Jul 02 '15 at 09:40
  • Thanks Chase. Will surely check this. – Kinjan Bhavsar Jul 02 '15 at 09:41
  • is there a way to release memory while displaying all items? – Kinjan Bhavsar Jul 07 '15 at 08:32
  • Pagination or scrolling with dynamic loading elements give you possibility to prevent creating problems with release memory, because normally this problems shouldn't exist. About ways to release memory or how prevent it in another way you can read if you go by second attached link. – ivamax9 Jul 07 '15 at 09:48