This is a migrated thread and some comments may be shown as answers.

how to dispose memory after closing tabitem?

6 Answers 679 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sarah
Top achievements
Rank 1
Sarah asked on 05 Oct 2011, 09:11 PM
i using telerik silverlight trial version.

I using a tab control.
By making each tab is added to dedicated memory.
Each tab bind to a View model .
How can I close a tab with slot to release it?
please help me for after closeing a tabitem, dispose view model.

my code:

    Private Sub AddTab(ByVal obj As Object, ByVal Header As String)
  
        CType(Me.DataContext, MainViewModel).MyHeader = Header
        CType(Me.DataContext, MainViewModel).AddItem(Nothing)
        Dim r As New RadTabItem
        r = tabControl.Item(tabControl.Items.Count - 1)
        r.Content = obj
        tabControl.SelectedIndex = tabControl.Items.Count - 1
 
    End Sub
 
 
.
.
.
    Private Sub Archive_ItemDoubleClick(ByVal sender As Object, ByVal e As Telerik.Windows.RadRoutedEventArgs) Handles Archive.ItemDoubleClick
 
        Select Case CType(e.Source, RadTreeViewItem).Tag
 
            Case "UserProfile"
                AddTab(New vUserProfile, "My Profile")
            Case "ChangePassword"
                AddTab(New vUserChangePassword, "Change Password")
            Case Else
 
        End Select
 
    End Sub
 
.
.
.


<UserControl.Resources>
        <local:ValueInverter x:Key="Inverter" />
        <local:vmMainPage  x:Key="vmMainPage"  />
        <Style x:Key="CloseButton" TargetType="Button">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid Background="Transparent" Width="14" Height="14">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                                                                        Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                                                        Storyboard.TargetName="FocusEllipse">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ColorAnimation Duration="0" To="#FFDC3030"
                                                                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                                                        Storyboard.TargetName="FocusEllipse" />
                                            <ColorAnimation Duration="0" To="White"
                                                                                                        Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
                                                                                                        Storyboard.TargetName="path" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames
                                                                                                        Storyboard.TargetProperty="(UIElement.Visibility)"
                                                                                                        Storyboard.TargetName="FocusEllipse">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ColorAnimation Duration="0" To="Black"
                                                                                                        Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)"
                                                                                                        Storyboard.TargetName="FocusEllipse" />
                                            <ColorAnimation Duration="0" To="White"
                                                                                                        Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)"
                                                                                                        Storyboard.TargetName="path" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled" />
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused" />
                                    <VisualState x:Name="Unfocused" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Ellipse x:Name="FocusEllipse" Fill="#FFF13535" Visibility="Collapsed" />
                            <Path x:Name="path" Data="{TemplateBinding Content}" Stroke="#FF898888"
                                                                        HorizontalAlignment="Center" VerticalAlignment="Center"
                                                                        StrokeThickness="1" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <telerik:ContainerBindingCollection x:Key="ContainerBindings">
            <telerik:ContainerBinding PropertyName="IsSelected"
                                        Binding="{Binding IsSelected, Mode=TwoWay}" />
        </telerik:ContainerBindingCollection>
        <DataTemplate x:Key="TabItemTemplate"
                                telerik:ContainerBinding.ContainerBindings="{StaticResource ContainerBindings}">
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                <TextBlock Text="{Binding Header}" />
                <Button Command="{Binding RemoveItemCommand}" Style="{StaticResource CloseButton}"
                                                Margin="10,0,0,0" Content="M0,0 L6,6 M6, 0 L0,6" ToolTipService.ToolTip="Close" />
 
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ContentTemplate">
            <Grid />
        </DataTemplate>
    </UserControl.Resources>
 
 
 
 
   <telerik:RadTabControl Margin="3" x:Name="tabControl" ItemsSource="{Binding Tabs}"
                                    HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                                    ItemTemplate="{StaticResource TabItemTemplate}" FontFamily="Tahoma" >
                        </telerik:RadTabControl>

6 Answers, 1 is accepted

Sort by
0
sara
Top achievements
Rank 1
answered on 09 Oct 2011, 09:13 AM
The code I gave you.
And now I have problems.

I've attached an example. Every time a form is opened
A new tab is added to the tab control
About 30 megabytes of system memory can be added.
And closing the tab, it will not be released memory!
Who can help?


my sample:
http://www.mediafire.com/?j9bk0q4z607ss3s
0
Petar Mladenov
Telerik team
answered on 10 Oct 2011, 05:31 PM
Hello Sara,

Unfortunately, we were unable to open your solution. Is it possible  for you to send us a runnable sample showing this memory problem. This will be highly appreciated and we would be better able to investigate this. Thank you in advance.

All the best,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
sara
Top achievements
Rank 1
answered on 11 Oct 2011, 10:44 AM
hello petar .

i attach sample project:
http://www.4shared.com/file/ALbjHxfS/Laboratory.html
please see my problem.
0
Petar Mladenov
Telerik team
answered on 14 Oct 2011, 12:11 PM
Hi Sara,

 We can see that this application was used in SL 5 environment and that is why we could not open it the first time. On a side note,  I want to ensure you that all our Silverlight 4 controls will be fully compatible with the forthcoming SL5 official release. We are closely working with Microsoft to ensure that.
We tested you application in SL4 environment and with ANTS Memory Profiler. I can assure you that when a RadTabItems are closed they are removed from memory shortly after that. The big problem in you r application is that when you create a ViewModel for the RadTabItem, you are creating a 1 MILION strings in memory, which is very heavy:

  Case "Form"
                AddTab(New vForm, "form")
........
Partial Public Class vForm
    Inherits UserControl
 
    Public Sub New()
        InitializeComponent()
        Dim vm As New vmSample
        Me.DataContext = vm
    End Sub
........
Public Class vmSample
    Inherits vmBase
 
 
    Dim list1 As New List(Of String)
    Public Sub New()
          
        For i = 0 To 1000000
            list1.Add(i.ToString)
        Next
 
    End Sub
Removing this lines (the 1 milion strings generation) results in better performance and faster garbage collection.


All the best,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
sara
Top achievements
Rank 1
answered on 14 Oct 2011, 04:37 PM
Thank you for your attention.
I put this code to build 1 million string.
My problem is exactly the same, After I get out of system memory, That free memory is.
How can I do this a million string of free memory?
i want dispose list1 object after closing Radtabitem.
please help me.
0
Petar Mladenov
Telerik team
answered on 19 Oct 2011, 04:52 PM
Hi Sara,

 We tested the solution again and we are sure that the RadTabItems and their ViewModels "vmSample"-s are successfully removed from memory. Furthermore, the invoking of  GC.Collect() works fine as you can see in the attached sample ( by pressing the "GC" button after removing the tabs). Please let us know if you need more info on this.

Best wishes,
Petar Mladenov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
TabControl
Asked by
Sarah
Top achievements
Rank 1
Answers by
sara
Top achievements
Rank 1
Petar Mladenov
Telerik team
Share this question
or