473,796 Members | 2,679 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CodeDom and GenerateInMemor y memory leak

I may have painted myself into a corner with GenerateInMemor y=true.

My app need a custom user step. Users want to code (sort of - they are not
programmers) some refinements to a search procedure. They can concoct a
moderately complex boolean expression, and that suffices. I will take their
statements, add top and bottom text, and now I can do a CodeDom compile.

I generate an assembly via CodeDom with GenerateInMemor y. I make a
delegate, and I can call my function, and all of this works fine. It runs at
good speed (no boxing of arguments), and that is what I need since I will be
looping over an array of data to be searched.

The problem is that I have a memory leak because of not unloading the
assembly. So, what I need is a new appdomain, and at this point I am stuck.
I don't know how to call CompileAssembly FromSource() with
GenerateInMemor y=True and put the generated assembly in a new appdomain. I
have no dll to load into the new appdomain.

Does anyone have any bright ideas? Working VS2005 VB source code (with the
leak) is included below. Every call to TestDynamicCode () adds two handles as
reported by the task manager. The call to CompileAssembly FromSource() is
what produces the leak. What I think I want to do is make an appdomain, tell
codedom to put my compiler output assembly in the appdomain, do my
calculations, and then unload the appdomain. I don't see how to do it.

-------------------------

Public Module DynamicCode

Public Function SourceCode() As String
' user provides the commented lines, my code provides the rest
' the user lines coded herein are just for testing
Dim a() As String = { _
"Imports System.Math", _
"Public Class DynamicClass", _
" Public Shared Function DynamicFunction ( _", _
" Byval s1 As String, _", _
" Byval s2 As String, _", _
" Byval i1 As Integer, _", _
" Byval i2 As Integer, _", _
" Byval d1 As Double, _", _
" Byval d2 As Double, _", _
" Byval d3 As Double, _", _
" Byval d4 As Double) _", _
" As Boolean", _
" Dim Qualified, b1, b2, b3, b4, b5, b6, b7, b8, b9 As Boolean", _
" Dim d As Double = Sqrt(d1)", _
" b1 = (d1>d2) ' user provides this and perhaps many others", _
" Qualified = b1 ' user provides this, the final result", _
" Return Qualified", _
" End Function", _
"End Class"}
Return Join(a, vbLf)
End Function

Public Delegate Function DynamicDelegate ( _
ByVal s1 As String, _
ByVal s2 As String, _
ByVal i1 As Integer, _
ByVal i2 As Integer, _
ByVal d1 As Double, _
ByVal d2 As Double, _
ByVal d3 As Double, _
ByVal d4 As Double) _
As Boolean ' this delegate must match the signature in SourceCode() above

Public Sub TestDynamicCode () ' success is no exceptions and no assert
failures

' make params, the compiler parameters
Dim params As New CodeDom.Compile r.CompilerParam eters
params.Generate InMemory = True ' Assembly is created in memory
params.TreatWar ningsAsErrors = False
params.WarningL evel = 4
params.Compiler Options = "/optionexplicit /optionstrict /nowarn
/optimize-"
'Dim refs() As String = {"System.dll ", "Microsoft.Visu alBasic.dll"}
'params.Referen cedAssemblies.A ddRange(refs)

' make results by compiling the source code
' CompileAssembly FromSource leaks 2 handles and about 1k memory
' i think it is because of not unloading the assembly
' however, you can't unload an assembly, you can only unload an appdomain
' everything works ok except for this leak
Dim provider As New Microsoft.Visua lBasic.VBCodePr ovider
Dim results As CodeDom.Compile r.CompilerResul ts =
provider.Compil eAssemblyFromSo urce(params, SourceCode)
provider.Dispos e()

' report compilation errors
Dim errors As String = ""
For Each err As CodeDom.Compile r.CompilerError In results.Errors
Const f As String = "Line {0}, Col {1}: Error {2} - {3}"
errors &= [String].Format(f, err.Line, err.Column, err.ErrorNumber ,
err.ErrorText) & vbLf
Next err
Debug.Assert(er rors = "", errors)

' make DynamicFunction Delegate so we can execute strongly typed and
without boxing
Dim DynamicClass As Type =
results.Compile dAssembly.GetTy pe("DynamicClas s")
Dim flags As Reflection.Bind ingFlags = Reflection.Bind ingFlags.Public Or
Reflection.Bind ingFlags.Static
Dim DynamicFunction As Reflection.Meth odInfo =
DynamicClass.Ge tMethod("Dynami cFunction", flags)
Dim DynamicFunction Delegate As DynamicDelegate =
CType([Delegate].CreateDelegate (GetType(Dynami cDelegate), Nothing,
DynamicFunction ), DynamicDelegate )
Debug.Assert(No t DynamicFunction Delegate Is Nothing)

' prove that it works, raise the loop limit to measure execution speed
For i As Integer = 1 To 100
Dim a As Double = Rnd(1)
Dim b As Double = Rnd(1)
Dim c As Double = 0
Dim bResult As Boolean = DynamicFunction Delegate("", "", 1, 2, a, b,
c, c)
Debug.Assert(bR esult = (a b))
Next i

End Sub

End Module

Oct 17 '07 #1
1 4897
On Oct 17, 8:01 am, AMercer <AMer...@discus sions.microsoft .comwrote:
I may have painted myself into a corner with GenerateInMemor y=true.

My app need a custom user step. Users want to code (sort of - they are not
programmers) some refinements to a search procedure. They can concoct a
moderately complex boolean expression, and that suffices. I will take their
statements, add top and bottom text, and now I can do a CodeDom compile.

I generate an assembly via CodeDom with GenerateInMemor y. I make a
delegate, and I can call my function, and all of this works fine. It runs at
good speed (no boxing of arguments), and that is what I need since I will be
looping over an array of data to be searched.

The problem is that I have a memory leak because of not unloading the
assembly. So, what I need is a new appdomain, and at this point I am stuck.
I don't know how to call CompileAssembly FromSource() with
GenerateInMemor y=True and put the generated assembly in a new appdomain. I
have no dll to load into the new appdomain.

Does anyone have any bright ideas? Working VS2005 VB source code (with the
leak) is included below. Every call to TestDynamicCode () adds two handles as
reported by the task manager. The call to CompileAssembly FromSource() is
what produces the leak. What I think I want to do is make an appdomain, tell
codedom to put my compiler output assembly in the appdomain, do my
calculations, and then unload the appdomain. I don't see how to do it.

-------------------------

Public Module DynamicCode

Public Function SourceCode() As String
' user provides the commented lines, my code provides the rest
' the user lines coded herein are just for testing
Dim a() As String = { _
"Imports System.Math", _
"Public Class DynamicClass", _
" Public Shared Function DynamicFunction ( _", _
" Byval s1 As String, _", _
" Byval s2 As String, _", _
" Byval i1 As Integer, _", _
" Byval i2 As Integer, _", _
" Byval d1 As Double, _", _
" Byval d2 As Double, _", _
" Byval d3 As Double, _", _
" Byval d4 As Double) _", _
" As Boolean", _
" Dim Qualified, b1, b2, b3, b4, b5, b6, b7, b8, b9 As Boolean", _
" Dim d As Double = Sqrt(d1)", _
" b1 = (d1>d2) ' user provides this and perhaps many others", _
" Qualified = b1 ' user provides this, the final result", _
" Return Qualified", _
" End Function", _
"End Class"}
Return Join(a, vbLf)
End Function

Public Delegate Function DynamicDelegate ( _
ByVal s1 As String, _
ByVal s2 As String, _
ByVal i1 As Integer, _
ByVal i2 As Integer, _
ByVal d1 As Double, _
ByVal d2 As Double, _
ByVal d3 As Double, _
ByVal d4 As Double) _
As Boolean ' this delegate must match the signature in SourceCode() above

Public Sub TestDynamicCode () ' success is no exceptions and no assert
failures

' make params, the compiler parameters
Dim params As New CodeDom.Compile r.CompilerParam eters
params.Generate InMemory = True ' Assembly is created in memory
params.TreatWar ningsAsErrors = False
params.WarningL evel = 4
params.Compiler Options = "/optionexplicit /optionstrict /nowarn
/optimize-"
'Dim refs() As String = {"System.dll ", "Microsoft.Visu alBasic.dll"}
'params.Referen cedAssemblies.A ddRange(refs)

' make results by compiling the source code
' CompileAssembly FromSource leaks 2 handles and about 1k memory
' i think it is because of not unloading the assembly
' however, you can't unload an assembly, you can only unload an appdomain
' everything works ok except for this leak
Dim provider As New Microsoft.Visua lBasic.VBCodePr ovider
Dim results As CodeDom.Compile r.CompilerResul ts =
provider.Compil eAssemblyFromSo urce(params, SourceCode)
provider.Dispos e()

' report compilation errors
Dim errors As String = ""
For Each err As CodeDom.Compile r.CompilerError In results.Errors
Const f As String = "Line {0}, Col {1}: Error {2} - {3}"
errors &= [String].Format(f, err.Line, err.Column, err.ErrorNumber ,
err.ErrorText) & vbLf
Next err
Debug.Assert(er rors = "", errors)

' make DynamicFunction Delegate so we can execute strongly typed and
without boxing
Dim DynamicClass As Type =
results.Compile dAssembly.GetTy pe("DynamicClas s")
Dim flags As Reflection.Bind ingFlags = Reflection.Bind ingFlags.Public Or
Reflection.Bind ingFlags.Static
Dim DynamicFunction As Reflection.Meth odInfo =
DynamicClass.Ge tMethod("Dynami cFunction", flags)
Dim DynamicFunction Delegate As DynamicDelegate =
CType([Delegate].CreateDelegate (GetType(Dynami cDelegate), Nothing,
DynamicFunction ), DynamicDelegate )
Debug.Assert(No t DynamicFunction Delegate Is Nothing)

' prove that it works, raise the loop limit to measure execution speed
For i As Integer = 1 To 100
Dim a As Double = Rnd(1)
Dim b As Double = Rnd(1)
Dim c As Double = 0
Dim bResult As Boolean = DynamicFunction Delegate("", "", 1, 2, a, b,
c, c)
Debug.Assert(bR esult = (a b))
Next i

End Sub

End Module
I don't have the time at the moment to work up a complete example, but
here are some general thoughts on what you are going to have to do to
accomplish your goal.

Since you have no way of specifying the appdomain for the in memory
assembly, then you're going to have to actually compile the assembly
in the target domain. While this may sound straight forward, it
isn't... The reason is that you're going to have to set up a method
to communicate with this domain with out actually referencing ANY
types that are in the dynamic assembly. Why? While, as soon as you
do, then it will also be loaded into your main appdomain, and you
won't be able to remove it from memory...

The trick is to create a class in a separate dll that inherits from
MarshalByRefObj ect that acts as the actual class loader. Then you can
do something like this in your code:

Dim compileDomain As AppDomain = AppDomain.Creat eDomain
("CompileDomain ")
Dim loader As ClassLoader = DirectCast
(compileDomain. CreateInstanceA ndUnwrap("Loade rNamespace",
"LoaderNamespac e.ClassLoader")

loader.DoYourWo rk("paramerter s here")
AppDomain.Unloa d(compileDomain )

That's pretty rough, but the idea is that you only reference the class
loader type. Then, you create an instance of that in the new domain
(which is what the CreateInstanceA ndUnwrap method does for you). At
this point all your calls ot the loader are then marshaled into the
new appdomain via remoting - which shouldn't cause you much of a
performance issue IF you can engineer your Loader class in such a way
that you don't have to make to many calls to it. In other words, keep
the interface chunky, not chatty.

--
Tom Shelton

Oct 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3414
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
17
4816
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is easier to reproduce (e.g.: remote machine not available). After about 1 day, I get a usage of 300MB of memory. I have used .NET Memory Profiler tool to try to see where the leak is located. For all the leaky instances, I can see the following (I...
1
5571
by: fred_mumble | last post by:
I have been testing various techniques to run "script" code which will be stored in a SQL database and executed dynamically at runtime. The scripts are essentially business rules that result in a report being generated based on test parameters input by a user - 4 batches of scripts containing multiple functions will be run each time a user runs a new test. The reason the scripts are run in 4 batches is due to the fact that each batch is...
20
8119
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex matches are called within a loop (like if or for). E.g. for(int i = 0; i < 10; i++) { Regex r = new Regex();
1
2294
by: John Livermore | last post by:
Hello, We have an application that uses CodeDom for dynamic in-memory compilation of code. The same code (library) is used by both an ASP.net web site and a Windows Service. The code runs fine in the web environment, but I get file IO errors related to temporary files when it is run in by the Service. I realize that CodeDom has to create temporary files even when doing in-memory compilation. However, by default, the service runs...
2
8538
by: JT | last post by:
Sorry if my frustration shows through in this. I have seen posts and discussions dating back to 2001 and still haven't seen any "good" example of a tutorial on CodeDOM. I've seen very many that give the same Console.WriteLine("Hello, World."); example. If that's all you want to do, then maybe it's adequate. The very first thing I've tried to do is use a MessageBox instead. When I include System.dll in .ReferencedAssemblies I get an...
22
9365
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a memory leak someplace. I can not detect the memory leak by running several reports by hand, but when I run tha app as a servrice and process few hundred reports there is significant memory leak. The application can consume over 1GB of memory where it...
0
1430
balabaster
by: balabaster | last post by:
I don't know if anyone's got any clue about this, but I have the following expression evaluator which makes use of the CodeDom to evaluate a VB.NET expression at runtime. This gives the system administrator the ability to add ad-hoc expressions onto the system, and when users execute these expressions, they are prompted for data for the arguments. Function Evaluate(ByVal Expression As String, ByVal Args() As Object) As Double If...
0
1191
by: thisismykindabyte | last post by:
Hello, I was wondering if anyone might know the easiest way to access static methods through a namespace(MyNamespace) from CodeDom generated code? Basically I have this in my MyNamespace.Form: //////// Most of this below is almost useless jargon just to give an idea of what values are set or not public CodeDom.Compiler.CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.