473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Metaprogramming


If you do metaprogramming in C#, is the generated code garbage collected or
does it leak indefinitely?

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Oct 4 '07 #1
12 1650
Jon Harrop wrote:
>
If you do metaprogramming in C#, is the generated code garbage collected
or does it leak indefinitely?
Can you give an example?

Regards,

Mads

--
Med venlig hilsen/Regards

Systemudvikler/Systemsdeveloper cand.scient.dat, Ph.d., Mads Bondo
Dydensborg
Dansk BiblioteksCenter A/S, Tempovej 7-11, 2750 Ballerup, Tlf. +45 44 86 77
34
Oct 4 '07 #2
Regardless of how they come to be (Reflection.Emit, etc), dynamic
assemblies are treated the same as regular assemblies; there is no
good way to unload an assembly from an active appdomain. You can,
however, load it into a second app-domain and then kill the app-
domain. A good bet is to (for instance) use a static cache so that you
don't end up creating the same code over and over, but simply re-use
the assembly you emitted earlier.

Executing code is treated as normal; instances will be collected (or
not) using the same rules.

Marc

Oct 4 '07 #3
On Thu, 04 Oct 2007 06:04:01 +0100, Jon Harrop wrote:
If you do metaprogramming in C#, is the generated code garbage collected
or does it leak indefinitely?
Without providing more detail, I find it difficult to be sure what you're
really trying to do.

If you dynamically create and load assemblies, you should probably load
them into a new appdomain. You cannot unload an assembly, but you can
discard an appdomain and discarding the appdomain will discard all the
assemblies loaded into it with it.

Ben
Oct 4 '07 #4
On Oct 4, 9:02 am, Ben Schwehn <b...@bschwehn.dewrote:
On Thu, 04 Oct 2007 06:04:01 +0100, Jon Harrop wrote:
If you do metaprogramming in C#, is the generated code garbage collected
or does it leak indefinitely?

Without providing more detail, I find it difficult to be sure what you're
really trying to do.
I think he's trying to prove that F# is superior to C#. That's Jon
Harrop's usual purpose on this group ;)

Jon

Oct 4 '07 #5
I think he's trying to prove that F# is superior to C#. That's Jon
Harrop's usual purpose on this group ;)
Interstingly, I think it is a valid discussion - but the important
context is: "at what?". Functional languages do have advantages,
especially in areas such as science / modelling - however, it isn't
necessarily a great fit for the typical business application.

It is *certainly* a much more meaningful discussion than the far-too-
regular VB vs C# ;-p

Marc

Oct 4 '07 #6
On Oct 4, 10:29 am, Marc Gravell <marc.grav...@gmail.comwrote:
I think he's trying to prove that F# is superior to C#. That's Jon
Harrop's usual purpose on this group ;)

Interstingly, I think it is a valid discussion - but the important
context is: "at what?". Functional languages do have advantages,
especially in areas such as science / modelling - however, it isn't
necessarily a great fit for the typical business application.
Absolutely.
It is *certainly* a much more meaningful discussion than the far-too-
regular VB vs C# ;-p
True. I just wish Jon would get into it properly rather than just
asking leading questions: "If you do X does C# behave badly", "Is C#
going to give way to F#" etc.

Jon

Oct 4 '07 #7
"Jon Skeet [C# MVP]" <sk***@pobox.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.com...
On Oct 4, 10:29 am, Marc Gravell <marc.grav...@gmail.comwrote:
I think he's trying to prove that F# is superior to C#. That's Jon
Harrop's usual purpose on this group ;)

Interstingly, I think it is a valid discussion - but the important
context is: "at what?". Functional languages do have advantages,
especially in areas such as science / modelling - however, it isn't
necessarily a great fit for the typical business application.

Absolutely.
>It is *certainly* a much more meaningful discussion than the far-too-
regular VB vs C# ;-p

True. I just wish Jon would get into it properly rather than just
asking leading questions: "If you do X does C# behave badly", "Is C#
going to give way to F#" etc.

Jon
Agree. In this way he's actually doing more harm than good for F# community.
There are better ways to evangelize new ideas.

Oct 4 '07 #8
Jon Skeet [C# MVP] wrote:
True. I just wish Jon would get into it properly rather than just
asking leading questions: "If you do X does C# behave badly", "Is C#
going to give way to F#" etc.
You forget that he thinks ML is more used than C#.

:-)

Arne
Oct 4 '07 #9
Ben Schwehn wrote:
On Thu, 04 Oct 2007 06:04:01 +0100, Jon Harrop wrote:
>If you do metaprogramming in C#, is the generated code garbage collected
or does it leak indefinitely?

Without providing more detail, I find it difficult to be sure what you're
really trying to do.

If you dynamically create and load assemblies, you should probably load
them into a new appdomain. You cannot unload an assembly, but you can
discard an appdomain and discarding the appdomain will discard all the
assemblies loaded into it with it.
Right. I think I'm getting to grips with this. So you must deallocate code
explicitly yourself, i.e. it is not garbage collected. To be able to
deallocate code there is the technical issue of wrapping your assembly in
an appdomain.

I read somewhere that types cannot be collected but that makes sense because
(I believe) the .NET platform prohibits structural subtyping. So all types
are regarded as different even if they are structurally identical, e.g. if
you define an (int, float) pair in two different assemblies then they are
considered to be different types. So types have the same place as code
in .NET from the point of view of allocation, and deallocating an appdomain
will deallocate the types it defines.

One final question: is code deallocation safe? So, if function A in assembly
A' calls function B in assembly B' and you deallocate the appdomain
containing B' before calling A, do you get a nice exception or a nasty
access violation?

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Oct 4 '07 #10
On Oct 4, 11:47 pm, Jon Harrop <j...@ffconsultancy.comwrote:

<snip>
One final question: is code deallocation safe? So, if function A in assembly
A' calls function B in assembly B' and you deallocate the appdomain
containing B' before calling A, do you get a nice exception or a nasty
access violation?
You'll get an AppDomainUnloadException.

Jon

Oct 5 '07 #11
Jon Skeet [C# MVP] wrote:
On Oct 4, 11:47 pm, Jon Harrop <j...@ffconsultancy.comwrote:
>One final question: is code deallocation safe? So, if function A in
assembly A' calls function B in assembly B' and you deallocate the
appdomain containing B' before calling A, do you get a nice exception or
a nasty access violation?

You'll get an AppDomainUnloadException.
Ok.

Can you garbage collect generated code by putting it in an app domain and
wrapping the app domain in a class that uses a finalizer to explicitly
deallocate the app domain?

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Oct 6 '07 #12
Jon Harrop <jo*@ffconsultancy.comwrote:
You'll get an AppDomainUnloadException.

Ok.

Can you garbage collect generated code by putting it in an app domain and
wrapping the app domain in a class that uses a finalizer to explicitly
deallocate the app domain?
I guess you probably could, yes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 6 '07 #13

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

Similar topics

12
by: Dave | last post by:
Would people agree with the statement that to a large degree, using template metaprogramming techniques turns a C++ compiler into a C++ interpreter (but just for the metaprogrammed portions of the...
9
by: Rock Johnson | last post by:
I understand that template metaprogramming is a technique that allows for calcualations to occur at compile-time rather than run-time. Can someone explain what is the benefit of this, and when it...
21
by: Protoman | last post by:
I've been looking at template metaprogramming. It seems really cool, make the compiler do most of the work. I have very simple program that uses TMP,it calculates the square of a number, but it...
3
by: wakun | last post by:
Hi there, I am working a project in numerical computation in which iterative method is applied for solving equation. The problem is so big and slow. Few days ago, I found a paper on...
9
by: PengYu.UT | last post by:
Hi, I have the code below this email. I want to replace the last 4 lines with a Metaprogramming loop to get something like the following (I don't know the syntax). Is it possible? for type in...
7
by: Joe | last post by:
Hi, I found a concept named template metaprogramming that can be used in C+ + code at compile-time. I am a beginner at C++. But I am a programmer on the .NET platform. Do you know if template...
5
by: iapx86 | last post by:
My parser project calls for a computed goto (see code below). The C preprocessor delivers the desired result, but is ugly. Template metaprogramming delivers results I do not understand. Can...
9
by: andrew cooke | last post by:
Hi, Thanks for the help a couple of days ago. I completed what I was doing and wrote a summary which I've posted at http://acooke.org/cute/PythonMeta0.html (it's kind of long to post here). I...
16
by: Wilson | last post by:
Hi all, I have an interesting problem that I'm hoping can be solved with metaprogramming, but I don't know how far Python supports code generation (and I don't know if I'm taking the correct...
12
by: nooneinparticular314159 | last post by:
Hello. If I declare the following: template<int a, int b, int SomeArray> class DoSomething{ public: .. .. ..
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.