Newsgroup: it.comp.lang.visual-basic
Titolo: HELP! wpf e memoria RAM

Allarme!!
sto facendo un programma che fa uso del wpf per i report...
ora ho generato un report di 50 pagine zeppo di frafici (controlli
CHART) 100 grafici.
ad ogni anteprima di stampa assorbe 100mb di memoria ram!! e non
riesco a liberarla quando chiudo la finestra di anteprima. dopo 4-5
anteprime il sistema non riesce più...
come Fare?
Private Sub bttStampa_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles bttStampa.Click
        Dim layoutScenari As Window = New examplePRINT
        Dim N As Integer = 0
        For Each infr In ElencoInfr
            For Each zona In infr.Zone
                For Each sez In zona.SezOmogeneeIntervento
                    Dim collectionIRI As New Dictionary(Of Double,
Double)
                    For Each a In sez.ListaScenari(0).EvIRI
                        collectionIRI.Add(Year(Now) + a.Key,
a.Value.Valore)
                    Next
                    'If collectionIRI.Count > 20 Then Stop
                    Dim collectionPCI As New Dictionary(Of Double,
Double)
                    For Each a In sez.ListaScenari(0).EvPCI
                        collectionPCI.Add(Year(Now) + a.Key,
a.Value.Valore)
                    Next
                    'If collectionPCI.Count > 20 Then Stop
                    Dim collectionSKID As New Dictionary(Of Double,
Double)
                    For Each a In sez.ListaScenari(0).EvSKID
                        collectionSKID.Add(Year(Now) + a.Key,
a.Value.Valore)
                    Next
                    'If collectionSKID.Count > 20 Then Stop
                    Dim collectionSPESA As New Dictionary(Of Double,
Double)
                    For i = 0 To Val(TBarcotemporale.Text)
                        collectionSPESA.Add(Year(Now) + i, 0)
                    Next
                    'If collectionSPESA.Count > 20 Then Stop
                    For Each a In sez.ListaScenari(0).EvCosto
                        If a.Value.costo.ToString <> "NaN" Then
                            collectionSPESA(Year(Now) + Int(a.Key)) =
a.Value.costo
                        Else
                            collectionSPESA(Year(Now) + Int(a.Key)) =
-10000000
                        End If
                    Next
                    StampaScenari(layoutScenari, infr.Infrastruttura,
zona.nome, sez.Nome, N + 1, collection1, collection2, collection3,
collection4)
                    N += 2
                    'If N > 40 Then
                    '    GoTo line
                    'End If
                Next
            Next
        Next
line:
        layoutScenari.Show()
        GC.Collect()
    End Sub
Marco

 anteprime il sistema non riesce più...
Dal codice che hai postato non si può estrapolare gran chè, visto che il grosso del lavoro direi che viene fatto altrove... 
L'unico appunto che mi viene da fare è: perchè ridichiari ogni volta tutti i Dictionary?
Sei all'interno di un triplo ciclo For, il che vuol dire che stai moltiplicando queli oggetti a dismisura... vuisto che poi li usi solo nel blocco più interno, sposta la dichiarazione a inizio routine e fai un banale .Clear quando devi resettarli.
Non penso sposterà il problema in maniera significativa, ma intanto è qualcosa.


si hai ragione anche se, come detto da te, nel contesto generale è
poca cosa.
considera che è un codice di prima stesura e l'ottimizzazione è il mio
obiettivo.
in pratica faccio uso di sub per generare un documento fixeddocument e
altre sub per generare grafici a linea ed a colonne.
per i grafici faccio uso di documenti xaml come questo che uso come
prototipo di tutti i grafici
<UserControl x:Class="GraphControlLineSeries"
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation
"
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml
"
    xmlns:dvc="clr-
namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
        Name="GraphControl1" Height="290" Width="423"
xmlns:chartingToolkit="clr-
namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit">
    <Grid>
        <dvc:Chart Canvas.Top="20" Canvas.Left="10" Name="mcChart"
Background="Azure"
                   Foreground="DarkBlue"
                   Title="Area Chart"
                   LegendTitle="Valori" Width="Auto">
                <dvc:LineSeries Title="parametro"
          IndependentValueBinding="{Binding Path=Key}"
          DependentValueBinding="{Binding Path=Value}"
AnimationSequence="FirstToLast" BorderBrush="Azure"
BorderThickness="2" IsSelectionEnabled="True" Width="Auto">
               </dvc:LineSeries>
         </dvc:Chart>
    </Grid>
</UserControl>
anche per il documento fixeddocument faccio uso di un prototipo xaml
(examplePRINT). tralascio il codice xaml ma è un banale prototipo cone
controlli textblock, pagecontent e fixedpage.
praticamente nella sub genero tutte le pagine necessarie
        Dim PC1 As New PageContent
        PC1.Width = 950
        PC1.Height = 793
        Dim FP1 As New FixedPage
        FP1.Width = 96 * 8.26
        FP1.Height = 96 * 11.69
        FP1.Margin = New Thickness(96 * 1, 96 * 0.5, 96 * 1, 96 * 0.5)
ed all'interno delle pagine inserisco due grafici per pagina con una
sub tipo questa
Sub CreaGraficoLinee(ByRef GCLS As GraphControlLineSeries, ByVal
indicatore As String, ByVal minY As Double, ByVal maxY As Double,
ByVal collection As Dictionary(Of Double, Double), ByVal
collectionSoglia1 As Dictionary(Of Double, Double), ByVal
collectionSoglia2 As Dictionary(Of Double, Double), ByVal
collectionSoglia3 As Dictionary(Of Double, Double))
        Dim LS As
System.Windows.Controls.DataVisualization.Charting.LineSeries
        LS = New
System.Windows.Controls.DataVisualization.Charting.LineSeries
        LS.IndependentValueBinding = New
System.Windows.Data.Binding("Key")
        LS.DependentValueBinding = New
System.Windows.Data.Binding("Value")
        Dim dps1 As New Style(GetType(LineDataPoint))
        dps1.Setters.Add(New Setter(LineDataPoint.OpacityProperty,
0.01))
        dps1.Setters.Add(New Setter(LineSeries.BackgroundProperty,
Media.Brushes.YellowGreen))
        LS.DataPointStyle = dps1
        LS.Title = "Soglia di Attenzione"
        GCLS.mcChart.Series.Add(LS)
        LS = New
System.Windows.Controls.DataVisualization.Charting.LineSeries
        LS.IndependentValueBinding = New
System.Windows.Data.Binding("Key")
        LS.DependentValueBinding = New
System.Windows.Data.Binding("Value")
        Dim dps2 As New Style(GetType(LineDataPoint))
        dps2.Setters.Add(New Setter(LineDataPoint.OpacityProperty,
0.01))
        dps2.Setters.Add(New Setter(LineSeries.BackgroundProperty,
Media.Brushes.Yellow))
        LS.DataPointStyle = dps2
        LS.Title = "Soglia di Intervento Ottimale"
        GCLS.mcChart.Series.Add(LS)
        LS = New
System.Windows.Controls.DataVisualization.Charting.LineSeries
        LS.IndependentValueBinding = New
System.Windows.Data.Binding("Key")
        LS.DependentValueBinding = New
System.Windows.Data.Binding("Value")
        Dim dps3 As New Style(GetType(LineDataPoint))
        dps3.Setters.Add(New Setter(LineDataPoint.OpacityProperty,
0.01))
        dps3.Setters.Add(New Setter(LineSeries.BackgroundProperty,
Media.Brushes.Red))
        LS.DataPointStyle = dps3
        LS.Title = "Soglia Critica"
        GCLS.mcChart.Series.Add(LS)
        DirectCast(GCLS.mcChart.Series(0), LineSeries).ItemsSource =
collection
        DirectCast(GCLS.mcChart.Series(1), LineSeries).ItemsSource =
collectionSoglia1
        DirectCast(GCLS.mcChart.Series(2), LineSeries).ItemsSource =
collectionSoglia2
        DirectCast(GCLS.mcChart.Series(3), LineSeries).ItemsSource =
collectionSoglia3
        DirectCast(GCLS.mcChart.Series(0), LineSeries).Title =
"Indicatore"
        DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).Title =
"Evoluzione Indicatore"
        Dim ax As
System.Windows.Controls.DataVisualization.Charting.LinearAxis = New
System.Windows.Controls.DataVisualization.Charting.LinearAxis
        ax.Orientation = DataVisualization.Charting.AxisOrientation.X
        Dim ay As
System.Windows.Controls.DataVisualization.Charting.LinearAxis = New
System.Windows.Controls.DataVisualization.Charting.LinearAxis
        ay.Orientation = DataVisualization.Charting.AxisOrientation.Y
        'Dim ch As
System.Windows.Controls.DataVisualization.Charting.Chart = New
System.Windows.Controls.DataVisualization.Charting.Chart
        DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).Axes.Add(ax)
        DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).Axes.Add(ay)
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Title =
"ANNI"
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).FontSize
= 12
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).FontWeight
= FontWeights.Bold
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Minimum
= collection.Keys(0)
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Maximum
= collection.Keys.Last
        'DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).AxisLabelStyle.Setters.Add(New
Setter(DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(0),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).RenderTransformProperty,
New RotateTransform(90))) ' New AxisLabel With {.FontWeight =
FontWeights.Bold})
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(1),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Title =
indicatore
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(1),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).FontSize
= 12
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(1),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).FontWeight
= FontWeights.Bold
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(1),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Minimum
= minY
        DirectCast(DirectCast(GCLS.mcChart,
System.Windows.Controls.DataVisualization.Charting.Chart).ActualAxes(1),
System.Windows.Controls.DataVisualization.Charting.LinearAxis).Maximum
= maxY
    End Sub
alla chiusura del documento wpf poi, iimposto tutte le pagine ed i
relativo controlli a nothing (è stato un tentativo di distruggere i
controlli ma la situzione non'è cambiata. tentativo per sopperire alla
mancanza del dispose in wpf)
For Each p In mfd.Pages
                Dim mfp = p.Child
                For Each c In mfp.Children
                    c = Nothing
                Next
                p = Nothing
            Next
            Me.Resources.Clear()
            GC.Collect()
ho fato qualche ricerca su internet e sembra che in wpf non c'è
bisogno del dispose per le risorse si liberano da sole alla
chiusura..solo che a me no!!