Welcome to MSDN Blogs Sign in | Join | Help

Dynamic Proxy and Memory Footprint

A while back I published a post about dynamic programming with WCF using the dynamic proxy library that allows you to create WCF client dynamically at runtime. Thank you for using the sample and sending your comments. Frequently I get feedback about the memory usage of the applications using this library. It seems that if you create many dynamic proxies during the lifetime of your application, the memory footprint keeps growing. This is a common problem that you encounter when using any dynamically created assembly in your application. You need to aware of this issue and take appropriate measure to make sure that you are not leaking memory.

The dynamic proxy creates the proxy assembly at runtime; this assembly is stored in a temporary location on your file system and loaded in the memory of your app domain. Now if you do not need the dynamic proxy anymore, there is no way to unload this temporary assembly. There isn’t. You need to unload the entire app domain. So what should you do if you need to create and destroy many dynamic proxies in your application? Simply, isolate the code that is creating and using the dynamic proxy and run it in a different app domain. Once you are done, simply unload that app domain.

Here is a simple modification to the example program from the dynamic proxy library that runs the dynamic client in a new app domain. You will notice that the memory footprint of the application remains the same over large number of iterations.

class Program

{

    public static void Main(string[] args)

    {

        string serviceWsdlUri = "http://localhost:8080/WcfSamples/DynamicProxy?wsdl";

        if (args.Length > 0)

        {

            serviceWsdlUri = args[0];

        }

 

        for(int i = 0; i < 1000; i++)

        {

            AppDomain proxyDomain = AppDomain.CreateDomain("ProxyExecutionDomain");

            DynamicClient dynamicClient = new DynamicClient(serviceWsdlUri);

            proxyDomain.DoCallBack(

              new CrossAppDomainDelegate(dynamicClient.CrossAppDomainCallback));

            AppDomain.Unload(proxyDomain);

            GC.Collect();

        }

    }

}

 

[Serializable]

class DynamicClient

{

    string serviceWsdlUri;

 

    public DynamicClient(string serviceWsdlUri)

    {

        this.serviceWsdlUri = serviceWsdlUri;

    }

 

    public void CrossAppDomainCallback()

    {

        // create the dynamic proxy factory, that downloads the service metadata

        // and create the dynamic factory.

        Console.WriteLine("Creating DynamicProxyFactory for " + serviceWsdlUri);

        DynamicProxyFactory factory = new DynamicProxyFactory(serviceWsdlUri);

...

 

Published Thursday, October 16, 2008 10:45 PM by vipulm

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Vipul Modi's Blog : Dynamic Programming with WCF

Friday, October 17, 2008 2:07 AM by Vipul Modi's Blog : Dynamic Programming with WCF

# re: Dynamic Proxy and Memory Footprint

Saturday, October 18, 2008 7:03 AM by Ricardo Peres

Hi, Vipul, thanks for your post!

What if we wanted to change the DynamicProxyFactory so that it has a member field of type AppDomain where all new types (and their assemblies) are placed; DynamicProxyFactory is also changed in order to implement IDisposable, and in the Dispose() method, the AppDomain is unloaded.

I tried to change your code, but I always get a "Type is not resolved for member '_MyClassName_here_'".

Can you help?

Thanks!

Ricardo Peres

# re: Dynamic Proxy and Memory Footprint

Saturday, October 18, 2008 7:45 AM by Rahman

First, i must thank u for this great work!!!

I have a question: How can i call my service's Methods asynchronously? i want to use asynchronous operations by using this Library(Dynamic Proxy) but i haven't found any way.

Could u help me ....

thanks

Rahman

# re: Dynamic Proxy and Memory Footprint

Monday, October 20, 2008 9:55 AM by Ricardo Peres

Hi, Rahman!

You can do that easily by using delegates.

For example, suppose you have a method like this:

public void DoSomething(int i) { ... }

You can define a delegate with signature

public delegate void DoSomethingDelegate(int i);

And then invoke DoSomething asynchronously:

DoSomethingDelegate del = new DoSomethingDelegate(DoSomething);

IAsyncResult result = del.BeginInvoke(i, null /*callback*/, null /*context*/);

If you want, you can pass a pointer to a method as the callback argument, it will be called when the asynchronous call terminates.

Hope this helps!

Ricardo Peres

# re: Dynamic Proxy and Memory Footprint

Tuesday, October 21, 2008 6:55 AM by Rahman

Hi, Ricardo

Thanks for your Reply! it was useful But i should tell u that i meant How could i call asynchronous WCF service Methods BY Using DynamicProxy Library!!! because in DynamicProxy Library we call methods a little different(using DynamicProxy.CallMethod() )!

I don't know maybe your way solves it but i changed WCF DynamicProxy Library and i added a property that asynchronous Operations are generated Dynamically for WCF service methods!

i will published the changed library in the CodeProject Site very Soon.

I Hope it will be useful ...

R. Khanipour

# re: Dynamic Proxy and Memory Footprint

Tuesday, November 04, 2008 1:20 AM by vipulm

You can update the code to set appropriate options so that async operations are generated. You can then use the CallMethod to call the Begin/End methods or add a wrapper BeginCallMethod/EndCallMethod.

# re: Dynamic Proxy and Memory Footprint

Thursday, November 13, 2008 2:02 AM by Werner

Hi Vipul,

Thank you for a great solution!

I'd like to decorate the generated code in the assembly with an attribute (i.e. AllowPartiallyTrustedCallersAttribute) but couldn't figure out how to do that.

May I ask you for some hints to go about that?

Thank you, Werner

# re: Dynamic Proxy and Memory Footprint

Friday, November 14, 2008 4:15 AM by Werner

Hi again,

I found it out. In case someone else needs that:

codecompileunit.AssemblyCustomAttributes.Add(new CodeAttributeDeclaration(..here goes attribute))

Enjoy,

Werner

# re: Dynamic Proxy and Memory Footprint

Thursday, December 18, 2008 10:15 PM by Andrew

Does this only run on Framework 3.5 ?

How can I run it on framework 2.0 ?

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker