1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace TEST_MEMORY
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
EventHandler tick = (ss, ee) =>
{
Process currentProc = Process.GetCurrentProcess();
long value = currentProc.PrivateMemorySize64;
long v = ((value / 1000) / 1000);
this.mem.Text = v.ToString();
this.PART_Graph.Add(v);
};
timer.Tick += tick;
tick(null, null);
timer.Start();
Button_Click(null, null);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync();
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
//GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency;
//GC.Collect(2);
//GC.Collect(1);
GC.Collect();
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
Thread.Sleep(500);
}
}
#region Mode
private ObservableCollection<String> _Mode = null;
public ObservableCollection<String> Mode
{
get {
if (_Mode == null)
{
_Mode = new ObservableCollection<string>();
_Mode.Add("Simple");
_Mode.Add("Parent");
_Mode.Add("Message");
_Mode.Add("Message+parent");
}
return _Mode;
}
}
#endregion
#region SelectedIndex
private int _SelectedIndex = 0;
public int SelectedIndex
{
get { return _SelectedIndex; }
set { _SelectedIndex = value; OnPropertyChanged("SelectedIndex"); }
}
#endregion
private void Button_Create(object sender, RoutedEventArgs e)
{
if (Collection == null)
{
Collection = new ObservableCollection<object>();
}
this.PART_Rec.Visibility = Visibility.Visible;
App.DoEvents();
for (int i = 0; i < 9000; i++)
{
Toto t = null;
// simple
if (SelectedIndex == 0)
{
t = new Toto { Name = "alpha " + i };
}
// parent
else if (SelectedIndex == 1)
{
t = new Toto { Name = "alpha " + i, Parent = this };
}
// message
else if (SelectedIndex == 2)
{
t = new Toto { Name = "alpha " + i };
t.Changed += t_Changed;
}
// message + parent
else if (SelectedIndex == 3)
{
t = new Toto { Name = "alpha " + i, Parent = this };
t.Changed += t_Changed;
}
Collection.Add(t);
}
this.PART_Rec.Visibility = Visibility.Hidden;
App.DoEvents();
}
void t_Changed(object sender, EventArgs e)
{
}
public ObservableCollection<Object> _Collection = null;
public ObservableCollection<Object> Collection
{
get { return _Collection; }
private set { _Collection = value; OnPropertyChanged("Collection"); }
}
private void Button_Clear(object sender, RoutedEventArgs e)
{
Collection.Clear();
//Collection = null;
}
Random r = new Random();
private void Button_Add(object sender, RoutedEventArgs e)
{
double v = r.Next(300);
this.PART_Graph.Add(v);
}
private void Button_Fixe(object sender, RoutedEventArgs e)
{
this.PART_Graph.Fixed();
this.fixe.Text = this.mem.Text;
}
private void Button_Unfixe(object sender, RoutedEventArgs e)
{
this.PART_Graph.Unfixed();
this.fixe.Text = "---";
}
Stopwatch sw = new Stopwatch();
private void Button_Run(object sender, RoutedEventArgs e)
{
sw.Reset();
sw.Start();
this.IsEnabled = false;
App.DoEvents();
TH(5);
for (int i = 0; i < 20; i++)
{
this.info.Text = "Iteration : " + (i + 1);
App.DoEvents();
Button_Create(null, null);
TH(2);
Button_Clear(null, null);
Button_Clear(null, null);
Button_Clear(null, null);
TH(5);
if (i == 0)
{
Button_Fixe(null, null);
}
}
// attente stabilisation
TH(60);
sw.Stop();
this.time.Text = sw.Elapsed.ToString();
this.IsEnabled = true;
}
private void TH(long sleepInSec)
{
for (int i = 0; i < sleepInSec * 10; i++)
{
Thread.Sleep(100);
this.time.Text = sw.Elapsed.ToString();
App.DoEvents();
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Toto
{
public event EventHandler Changed;
public string Name { get; set; }
public object Parent { get; set; }
public override string ToString()
{
return Name;
}
}
} |
Partager