Index: /branches/thasling/DistributedGA/DistributedGA.Core/DistributedGA.Core.csproj
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/DistributedGA.Core.csproj	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/DistributedGA.Core.csproj	(revision 14253)
@@ -64,4 +64,5 @@
     <Compile Include="Interface\IPeerListManager.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Util\ByteArrayWrapper.cs" />
     <Compile Include="Util\SizedConcurrentQueue.cs" />
   </ItemGroup>
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/PeerNetworkMessageHandler.cs	(revision 14253)
@@ -22,11 +22,9 @@
     //two queues are used to gather and and provide population more efficiently
     private object activeQueueLocker = new Object();
-    private SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>> writeQueue;
-    private SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>> readQueue;
+    private SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>> writeQueue;
+    private SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>> readQueue;
 
     //uses IMessageService for recieving population from one peer at once
     private IMessageService host = null;
-
-    private double communicationRate;
 
     public event EventHandler<Exception> ExceptionOccurend;
@@ -40,8 +38,6 @@
         }; // TODO: get own peerinfo
 
-        this.communicationRate = communicationRate;
-
-        writeQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
-        readQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
+        writeQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
+        readQueue = new SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
         writeQueue.Limit = messageCacheCapacity;
         readQueue.Limit = writeQueue.Limit;
@@ -57,6 +53,5 @@
         sender.Init(ownInstance, messageCacheCapacity);
 
-      }
-      catch (Exception ex) {
+      } catch (Exception ex) {
         AddError("PeerNetworkMessageHandler.Init", ex);
       }
@@ -68,11 +63,10 @@
         sender.Dispose();
         peerListManager.Dispose();
-      }
-      catch (Exception ex) {
+      } catch (Exception ex) {
         AddError("PeerNetworkMessageHandler.Dispose", ex);
       }
     }
 
-    public void PublishDataToNetwork(byte[] data) {
+    public void PublishDataToNetwork(ByteArrayWrapper data) {
       try {
         var allPeers = peerListManager.GetPeerList();
@@ -80,20 +74,18 @@
           try {
             sender.SendData(peer, data);
-          }
-          catch (Exception ex) {
+          } catch (Exception ex) {
             AddError("PeerNetworkMessageHandler.PublishDataToNetwork(during sending to one peer!)", ex);
           }
         }
 
-      }
-      catch (Exception ex) {
+      } catch (Exception ex) {
         AddError("PeerNetworkMessageHandler.PublishDataToNetwork", ex);
       }
     }
 
-    public List<KeyValuePair<PeerInfo, byte[]>> GetDataFromNetwork() {
+    public List<KeyValuePair<PeerInfo, ByteArrayWrapper>> GetDataFromNetwork() {
       try {
-        List<KeyValuePair<PeerInfo, byte[]>> res = new List<KeyValuePair<PeerInfo, byte[]>>();
-        KeyValuePair<PeerInfo, byte[]> item;
+        List<KeyValuePair<PeerInfo, ByteArrayWrapper>> res = new List<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
+        KeyValuePair<PeerInfo, ByteArrayWrapper> item;
         lock (activeQueueLocker) {
           //changing the current queue for storing items to send
@@ -111,6 +103,5 @@
         }
         return res;//.ToArray();
-      }
-      catch (Exception ex) {
+      } catch (Exception ex) {
         AddError("PeerNetworkMessageHandler.GetDataFromNetwork", ex);
         return null;
@@ -142,6 +133,5 @@
           .Select(ip => ip.ToString())
           .First(str => str.StartsWith(ipPrefix));
-      }
-      catch { return null; }
+      } catch { return null; }
     }
 
@@ -149,5 +139,5 @@
       if (e != null && e.Sender.ProblemInstance.Equals(ownInstance.ProblemInstance)) {
         lock (activeQueueLocker) {
-          writeQueue.Enqueue(new KeyValuePair<PeerInfo, byte[]>(e.Sender, e.data));
+          writeQueue.Enqueue(new KeyValuePair<PeerInfo, ByteArrayWrapper>(e.Sender, ByteArrayWrapper.CreateByteArrayWrapper(e.data)));
         }
 
@@ -160,6 +150,5 @@
         try {
           peerListManager.SendLogToServer(string.Concat("Source: ", source, ", Exception: ", ex.Message));
-        }
-        catch { }
+        } catch { }
       }
     }
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfMessageSender.cs	(revision 14253)
@@ -1,5 +1,7 @@
 ﻿using System;
+using System.CodeDom;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Linq;
 using System.ServiceModel;
 using System.Threading.Tasks;
@@ -15,16 +17,17 @@
 
     //providing two queues for faster access
-    private SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>> bufferedMessagesWrite;
-    private SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>> bufferedMessagesRead;
+    private SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>> bufferedMessagesWrite;
+    private SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>> bufferedMessagesRead;
 
     private Timer timer; //sends cached messages to network in background
 
     private Object timerLock = new Object();
+    private bool isActive = false;
 
     public void Init(PeerInfo source, int messageCacheCapacity) {
       myself = source;
-      bufferedMessagesRead = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
+      bufferedMessagesRead = new SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
       bufferedMessagesRead.Limit = messageCacheCapacity;
-      bufferedMessagesWrite = new SizedConcurrentQueue<KeyValuePair<PeerInfo, byte[]>>();
+      bufferedMessagesWrite = new SizedConcurrentQueue<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
       bufferedMessagesWrite.Limit = bufferedMessagesRead.Limit;
       timer = new Timer(1000 * 10); //each 10 seconds
@@ -33,6 +36,6 @@
     }
 
-    public void SendData(PeerInfo destination, byte[] data) {
-      bufferedMessagesWrite.Enqueue(new KeyValuePair<PeerInfo, byte[]>(destination, data));
+    public void SendData(PeerInfo destination, ByteArrayWrapper data) {
+      bufferedMessagesWrite.Enqueue(new KeyValuePair<PeerInfo, ByteArrayWrapper>(destination, data));
     }
 
@@ -44,22 +47,35 @@
 
     private void GenerateSendingTasks(object sender, ElapsedEventArgs e) {
+      if (isActive) return;
       lock (timerLock) {
-        //changing the queues...
-        var tmp = bufferedMessagesRead;
-        bufferedMessagesRead = bufferedMessagesWrite;
-        bufferedMessagesWrite = tmp;
-        List<KeyValuePair<PeerInfo, byte[]>> messages = new List<KeyValuePair<PeerInfo, byte[]>>();
-        while (!bufferedMessagesRead.IsEmpty) {
-          KeyValuePair<PeerInfo, byte[]> message;
-          if (bufferedMessagesRead.TryDequeue(out message)) {
-            messages.Add(message);
-            //Task.Factory.StartNew(() => SendDataFromQueue(message.Key, message.Value), TaskCreationOptions.LongRunning);
+        try {
+          isActive = true;
+          //changing the queues...
+          var tmp = bufferedMessagesRead;
+          bufferedMessagesRead = bufferedMessagesWrite;
+          bufferedMessagesWrite = tmp;
+          List<KeyValuePair<PeerInfo, ByteArrayWrapper>> messages = new List<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
+          while (!bufferedMessagesRead.IsEmpty) {
+            KeyValuePair<PeerInfo, ByteArrayWrapper> message;
+            if (bufferedMessagesRead.TryDequeue(out message)) {
+              messages.Add(message);
+              //Task.Factory.StartNew(() => SendDataFromQueue(message.Key, message.Value), TaskCreationOptions.LongRunning);
+            }
           }
-        }
-        //now: merge them and start sending tasks
+          //now: merge them and start sending tasks
 
-        List<KeyValuePair<PeerInfo, byte[][]>> mergedMessages = MergeMessages(messages);
-        foreach (var item in mergedMessages) {
-          Task.Factory.StartNew(() => SendDataFromQueue(item.Key, item.Value), TaskCreationOptions.LongRunning);
+          List<KeyValuePair<PeerInfo, ByteArrayWrapper[]>> mergedMessages = MergeMessages(messages);
+
+          var runningTasks = new List<Task>();
+          foreach (var item in mergedMessages) {
+            var itemToSend = item;
+            var t = Task.Factory.StartNew(() => SendDataFromQueue(itemToSend.Key, itemToSend.Value),
+              TaskCreationOptions.LongRunning);
+            runningTasks.Add(t);
+          }
+
+          Task.WaitAll(runningTasks.ToArray());
+        } finally {
+          isActive = false;
         }
       }
@@ -71,10 +87,9 @@
     /// <param name="messages">a list with multiple messages to the same  destination</param>
     /// <returns></returns>
-    private List<KeyValuePair<PeerInfo, byte[][]>> MergeMessages(List<KeyValuePair<PeerInfo, byte[]>> messages) {
-      List<KeyValuePair<PeerInfo, byte[][]>> res = new List<KeyValuePair<PeerInfo, byte[][]>>();
-      Dictionary<PeerInfo, List<byte[]>> cache = new Dictionary<PeerInfo, List<byte[]>>();
+    private List<KeyValuePair<PeerInfo, ByteArrayWrapper[]>> MergeMessages(List<KeyValuePair<PeerInfo, ByteArrayWrapper>> messages) {
+      Dictionary<PeerInfo, List<ByteArrayWrapper>> cache = new Dictionary<PeerInfo, List<ByteArrayWrapper>>();
       foreach (var messagePackage in messages) {
         if (!cache.ContainsKey(messagePackage.Key)) {
-          cache.Add(messagePackage.Key, new List<byte[]>());
+          cache.Add(messagePackage.Key, new List<ByteArrayWrapper>());
         }
         //for (int i = 0; i <= messagePackage.Value.GetUpperBound(0); i++) {
@@ -82,14 +97,6 @@
         //}
       }
-      //now we have a dictionary with all messages per destionation
-      //so: create a byte[][] again and return
-      foreach (var dest in cache.Keys) {
-        byte[][] messagesPerDest = new byte[cache[dest].Count][];
-        for (int i = 0; i <= messagesPerDest.GetUpperBound(0); i++) {
-          messagesPerDest[i] = cache[dest][i];
-        }
-        res.Add(new KeyValuePair<PeerInfo, byte[][]>(dest, messagesPerDest));
-      }
-      return res;
+
+      return cache.Keys.Select(dest => new KeyValuePair<PeerInfo, ByteArrayWrapper[]>(dest, cache[dest].ToArray())).ToList();
     }
 
@@ -99,5 +106,5 @@
     /// <param name="destination">destination peer</param>
     /// <param name="data">data to send</param>
-    private void SendDataFromQueue(PeerInfo destination, byte[][] data) {
+    private void SendDataFromQueue(PeerInfo destination, ByteArrayWrapper[] data) {
       try {
         Console.WriteLine(string.Format("Sending data to {0}:{1} in the background...", destination.IpAddress, destination.Port));
@@ -117,11 +124,10 @@
         using (var myChannelFactory = new ChannelFactory<IMessageContract>(binding, endpoint)) {
           using (IClientChannel client = (IClientChannel)myChannelFactory.CreateChannel()) {
-            for (int i = 0; i < data.GetUpperBound(0) + 1; i++) {
-              ((IMessageContract)client).SendData(myself, data[i]); //maybe exception...
+            for (int i = 0; i < data.GetLength(0); i++) {
+              ((IMessageContract)client).SendData(myself, data[i].Array); //maybe exception...
             }
           }
         }
-      }
-      catch (Exception ex) {
+      } catch (Exception ex) {
         //ignore 
         Console.WriteLine(ex.Message);
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Implementation/WcfPeerListManager.cs	(revision 14253)
@@ -58,5 +58,5 @@
     }
 
-    private List<PeerInfo> ChoosePeersForMessaging(ref List<PeerInfo> allPeers) {
+    private List<PeerInfo> ChoosePeersForMessaging(List<PeerInfo> allPeers) {
       Shuffle<PeerInfo>(allPeers);
       int toTake = Convert.ToInt32(allPeers.Count * communicationRate) + 1;
@@ -64,5 +64,5 @@
         toTake = 1;
       }
-      return allPeers.Take(toTake).ToList(); ;
+      return allPeers.Take(toTake).ToList();
     }
 
@@ -89,5 +89,5 @@
             }
           }
-          cachedPeerList = ChoosePeersForMessaging(ref allPeers);
+          cachedPeerList = ChoosePeersForMessaging(allPeers);
         }
         catch { } //nothing to do
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageHandler.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageHandler.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageHandler.cs	(revision 14253)
@@ -2,4 +2,5 @@
 using System.Collections.Generic;
 using DistributedGA.Core.Domain;
+using DistributedGA.Core.Util;
 
 namespace DistributedGA.Core.Interface {
@@ -9,7 +10,7 @@
     void Init(string lanIpPrefix, string contactServerUrl, string problemInstance, int messageCacheCapacty, double communicationRate);
 
-    void PublishDataToNetwork(byte[] data);
+    void PublishDataToNetwork(ByteArrayWrapper data);
 
-    List<KeyValuePair<PeerInfo, byte[]>> GetDataFromNetwork();
+    List<KeyValuePair<PeerInfo, ByteArrayWrapper>> GetDataFromNetwork();
 
     PeerInfo GetPeerInfo();
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageSender.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageSender.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Interface/IMessageSender.cs	(revision 14253)
@@ -1,3 +1,4 @@
 ﻿using DistributedGA.Core.Domain;
+using DistributedGA.Core.Util;
 
 namespace DistributedGA.Core.Interface {
@@ -7,5 +8,5 @@
     void Init(PeerInfo source, int messageCacheCapacity);
 
-    void SendData(PeerInfo destination, byte[] data);
+    void SendData(PeerInfo destination, ByteArrayWrapper data);
 
     void Dispose();
Index: /branches/thasling/DistributedGA/DistributedGA.Core/Util/ByteArrayWrapper.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Core/Util/ByteArrayWrapper.cs	(revision 14253)
+++ /branches/thasling/DistributedGA/DistributedGA.Core/Util/ByteArrayWrapper.cs	(revision 14253)
@@ -0,0 +1,31 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace DistributedGA.Core.Util {
+  public class ByteArrayWrapper {
+    public static int AliveCounter;
+    public static int AllocatedCounter;
+    public static int FreedCounter;
+
+    public static ByteArrayWrapper CreateByteArrayWrapper(byte[] arr) {
+      return new ByteArrayWrapper(arr);
+    }
+
+    public byte[] Array { get; private set; }
+
+    private ByteArrayWrapper(byte[] arr) {
+      Interlocked.Increment(ref AliveCounter);
+      Interlocked.Increment(ref AllocatedCounter);
+      this.Array = arr;
+    }
+
+    ~ByteArrayWrapper() {
+      Interlocked.Decrement(ref AliveCounter);
+      Interlocked.Increment(ref FreedCounter);
+    }
+  }
+}
Index: /branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.Hive/P2PMigrationAnalyzer.cs	(revision 14253)
@@ -28,4 +28,5 @@
 using DistributedGA.Core.Implementation;
 using DistributedGA.Core.Interface;
+using DistributedGA.Core.Util;
 using DistributedGA.Hive;
 using HeuristicLab.Common;
@@ -119,4 +120,7 @@
       Parameters.Add(new ValueParameter<StringValue>("JobGUID", "", new StringValue(Guid.NewGuid().ToString())));
       Parameters.Add(new ValueParameter<ILog>("Log", "The log", new Log(1000)));
+      Parameters.Add(new ValueParameter<IntValue>("ByteArraysAllocated", new IntValue(0)));
+      Parameters.Add(new ValueParameter<IntValue>("ByteArraysFreed", new IntValue(0)));
+      Parameters.Add(new ValueParameter<IntValue>("ByteArraysAlive", new IntValue(0)));
 
       var validValues = new ItemSet<EnumValue<MigrationStrategy>>();
@@ -145,5 +149,5 @@
       var communicationRate = ((PercentValue)Parameters["CommunicationRate"].ActualValue).Value;
       var messageCacheCapacity = ((IntValue)Parameters["MessageCacheCapacity"].ActualValue).Value;
-      h.Init(lanIpPrefix, contactServerUri, problemInstance, (int)(100 * messageCacheCapacity), (int)(100 * communicationRate));
+      h.Init(lanIpPrefix, contactServerUri, problemInstance, messageCacheCapacity, (int)(100 * communicationRate));
       h.ExceptionOccurend += ExceptionThrown;
     }
@@ -221,5 +225,5 @@
               HeuristicLab.Persistence.Default.Xml.XmlGenerator.Serialize(msgScope, stream);
               message = stream.GetBuffer();
-              h.PublishDataToNetwork(message);
+              h.PublishDataToNetwork(ByteArrayWrapper.CreateByteArrayWrapper(message));
             }
           }
@@ -230,5 +234,5 @@
           // recieve
           var message = h.GetDataFromNetwork();
-          List<KeyValuePair<PeerInfo, byte[]>> immigrants = new List<KeyValuePair<PeerInfo, byte[]>>();
+          List<KeyValuePair<PeerInfo, ByteArrayWrapper>> immigrants = new List<KeyValuePair<PeerInfo, ByteArrayWrapper>>();
           //limit number of immigrants to use
           if (noOfEmigrants < message.Count) {
@@ -264,5 +268,5 @@
           var qualityTranslatedName = QualityParameter.TranslatedName;
           foreach (var msg in immigrants) {
-            using (var stream = new MemoryStream(msg.Value)) {
+            using (var stream = new MemoryStream(msg.Value.Array)) {
               var immigrantScope = HeuristicLab.Persistence.Default.Xml.XmlParser.Deserialize<IScope>(stream);
 
@@ -290,4 +294,9 @@
 
       MigrationIterations.Value++;
+
+      ((IValueParameter<IntValue>)Parameters["ByteArraysAllocated"]).Value.Value = ByteArrayWrapper.AllocatedCounter;
+      ((IValueParameter<IntValue>)Parameters["ByteArraysFreed"]).Value.Value = ByteArrayWrapper.FreedCounter;
+      ((IValueParameter<IntValue>)Parameters["ByteArraysAlive"]).Value.Value = ByteArrayWrapper.AliveCounter;
+
       return base.Apply();
     }
Index: /branches/thasling/DistributedGA/DistributedGA.sln
===================================================================
--- /branches/thasling/DistributedGA/DistributedGA.sln	(revision 14252)
+++ /branches/thasling/DistributedGA/DistributedGA.sln	(revision 14253)
@@ -1,7 +1,5 @@
 ﻿
 Microsoft Visual Studio Solution File, Format Version 12.00
-# Visual Studio 2013
-VisualStudioVersion = 12.0.21005.1
-MinimumVisualStudioVersion = 10.0.40219.1
+# Visual Studio 2012
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DistributedGA.Test", "DistributedGA.Test\DistributedGA.Test.csproj", "{E0E91C06-C56A-454F-9F7C-3FA7AE7F920E}"
 EndProject
@@ -50,6 +48,3 @@
 		HideSolutionNode = FALSE
 	EndGlobalSection
-	GlobalSection(Performance) = preSolution
-		HasPerformanceSessions = true
-	EndGlobalSection
 EndGlobal
