Hello la zone,
J’ai un soucis plutôt étrange avec le remoting de .Net 2.0 et le garbage collector.
J’explique la situation :
J’ai une méthode, appelée par remoting qui envoie une grosse quantité de données (en fait un XmlDocument de ± 90Mo). Le transfère fonctionne correctement mais une fois le transfère terminé, la mémoire sur le serveur n’est pas libérée et si je rappelle la méthode quelque fois supplémentaires j’arrive à saturation du serveur et je finis par avoir une OutOfMemoryException qui est levée.
Histoire d’essayer de refaire le même scénario en version plus simple, j’ai créé un projet avec ces classes (tirées d’un exemple de MSDN modifié, voir en bas de thread).
En gros ce que fait le code c’est que je crée un tableau de byte de 60Mo et je l’envoie par remoting entre un serveur et un client. Du côté du serveur, pas de soucis, sur ProcessExplorer on voit bien les 60Mo être mis en mémoire puis libérés, mais du côté client, les 60Mo restent en mémoire et ne sont jamais libérés.
J’ai réussi à résoudre le problème en appelant le garbage collector “a la main” et en demandant au thread d’attendre que les objets soient collectés, mais je n’aime pas trop cette solution… ça me semble un peu “cracra”.
Alors je voudrais savoir si l’un d’entre vous a déjà vu cette erreur et s’il existe un moyen pour passer outre?
J’ai mis la solution dans une archive sur rapidshare ici.
Objet remoting (Marshal) :
[codebox]using System;
using System.Collections.Generic;
using System.Text;
namespace Remote
{
public class ServiceClass : MarshalByRefObject
{
private DateTime m_startTime;
public ServiceClass()
{
Console.WriteLine("ServiceClass created without constructor. Instance hash is " + GetHashCode().ToString());
m_startTime = DateTime.Now;
}
~ServiceClass()
{
Console.WriteLine("I'm being collected after " + (new TimeSpan(DateTime.Now.Ticks - m_startTime.Ticks)).ToString() + " seconds.");
}
public DateTime GetServerTime()
{
Console.WriteLine("Time requested by a client.");
return DateTime.Now;
}
public byte[] GetBigByteArray()
{
Console.WriteLine("Big byte array requested");
byte[] byteArray = new byte[60000000];
byteArray[0] = byte.Parse("4");
byteArray[1] = byte.Parse("2");
return byteArray;
}
public int InstanceHash
{
get { return GetHashCode(); }
}
}
}[/codebox]
Serveur :
[codebox]using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using Remote;
using System.Runtime.Remoting.Channels.Http;
namespace Server
{
class Program
{
static void Main(string[] args)
{
HttpChannel channel = new HttpChannel(8080);
ChannelServices.RegisterChannel(channel, false);
ServiceClass object1 = new ServiceClass();
// Creates the single instance of ServiceClass. All clients
// will use this instance.
ObjRef ref1 = RemotingServices.Marshal(object1, "object1uri");
Console.WriteLine("ObjRef.URI: " + ref1.URI);
Console.WriteLine("Running. Press Enter to end publication.");
Console.ReadLine();
// This unregisters the object from publication, but leaves
// the channel listening.
RemotingServices.Disconnect(object1);
Console.WriteLine();
Console.WriteLine("Disconnected the object. Client now receives a RemotingException.");
Console.WriteLine("Press Enter to unregister the channel.");
Console.ReadLine();
// At this point, the ServerClass object still exists. The server
// could republish it.
// This unregisters the channel, but leaves the application
// domain running.
ChannelServices.UnregisterChannel(channel);
Console.WriteLine("Unregistered the channel. Client now receives a WebException.");
// The ServerClass object still exists. The server could
// reregister the channel and republish the object.
Console.WriteLine("The host application domain is still running. Press Enter to stop the process.");
Console.ReadLine();
// The ServiceClass object's Finalize method writes a message to
// the console. A single object will almost always succeed in
// running its Finalize method before the Console is finalized;
// in a larger application, you could ensure that all objects
// finalize before the application ends by calling the garbage
// collector and waiting.
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
}[/codebox]
Client :
[codebox]using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using Remote;
namespace Client
{
class Program
{
[MTAThread]
static void Main(string[] args)
{
HttpChannel channel = new HttpChannel();
ChannelServices.RegisterChannel(channel, false);
// Registers the remote class. (This could be done with a
// configuration file instead of a direct call.)
RemotingConfiguration.RegisterWellKnownClientType(
Type.GetType("Remote.ServiceClass, remote"),
"http://localhost:8080/object1uri");
// Instead of creating a new object, this obtains a reference
// to the server's single instance of the ServiceClass object.
Console.WriteLine("Press 'd' for the server date and time; 'b' for the big byte array, 'q' to quit.");
bool continueTest = true;
while (continueTest)
{
ServiceClass object1 = new ServiceClass();
ConsoleKeyInfo keyPressed = Console.ReadKey();
if (keyPressed.KeyChar == 'd')
{
try
{
Console.WriteLine("ServerTime: " + object1.GetServerTime());
}
catch (Exception ex)
{
Console.WriteLine("Exception of type: " + ex.ToString() + " occurred.");
Console.WriteLine("Details: " + ex.Message);
}
}
else if (keyPressed.KeyChar == 'b')
{
try
{
byte[] byteArray = object1.GetBigByteArray();
Console.WriteLine(String.Format("First 2 bytes of the array : {0}{1}\nbyteArray.Length : {2}", byteArray[0], byteArray[1], byteArray.Length));
}
catch (Exception ex)
{
Console.WriteLine("Exception of type: " + ex.ToString() + " occurred.");
Console.WriteLine("Details: " + ex.Message);
}
}
else if (keyPressed.KeyChar == 'q')
{
continueTest = false;
}
else
{
Console.WriteLine("Press 'd' for the server date and time; 'b' for the big byte array, 'q' to quit.");
}
//GC.Collect();
//GC.WaitForPendingFinalizers();
}
}
}
}
[/codebox]
