Kinjan Bhavsar
Kinjan Bhavsar

Reputation: 1449

How to manage or release memory while working with large data in Windows Phone 8?

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.

Upvotes: 0

Views: 111

Answers (1)

ivamax9
ivamax9

Reputation: 2629

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.

Upvotes: 1

Related Questions