7

Disclaimer: I come from a PHP background.

In PHP, I could have thousands files, which are never loaded, if not needed, due to the autoloader feature (If some code is needed, it would be loaded)

How do .NET's assemblies work? Do they load all the program code into the memory on application startup?

For example, I can have some areas with multiple Controllers:

  • Area 1
    • Controller 1
  • Area 2
    • Controller 1
    • ...
    • Controller 42

If Area 2 is optional by configuration, would it be a better Idea to extract this Area into a seperate Assembly and load it if needed?

ihimv
  • 243
  • 1
  • 7
  • [Is micro-optimisation important when coding?](http://programmers.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Jan 26 '16 at 19:34
  • 1
    I don't think it is micro-optimisation. Maybe I am wrong. My Question is a simple example. In reality I am talking about 20 areas, every area containing multiple Layers (DomainService/Repository/Models/Entities etc.) @gnat – Christian Gollhardt Jan 26 '16 at 19:40
  • [Is it always wrong to optimize before profiling?](http://programmers.stackexchange.com/questions/63986/is-it-always-wrong-to-optimize-before-profiling) – gnat Jan 26 '16 at 19:42
  • 1
    I think, [this](http://programmers.stackexchange.com/a/14882/122683) is more the case of my question :) – Christian Gollhardt Jan 26 '16 at 20:07
  • 1
    Defining memory usage for memory-mapped-files isn't trivial. – CodesInChaos Jan 26 '16 at 22:42
  • @CodesInChaos: I do not understand how your comment refers to the question. – Doc Brown Jan 27 '16 at 07:26
  • @DocBrown because just as executable code, .net assemblies are demand paged, resulting in their data not being loaded unless & until it is referenced, in exactly the same way as a memory-mapped file is. – Jules Jan 27 '16 at 08:03

1 Answers1

5

Do they load all the program code into the memory on application startup?

NO

You can have as many Assemblies in your project as you need. Assemblies are referenced via the using keyword. Unused assemblies are filtered out at the build time itself. For example, you can have two referenced asssemblies as:

using System.Windows.Forms;
using System.Web.UI.WebControls;

If you haven't used assembly System.Windows.Forms, it will not affect the performance of your code but it may affect compile time and Intellisense performance.

See this StackOverflow question for unused usings.

Referenced assemblies are not immediately loaded - they are loaded on the fly as needed. So regardless of whether you have an assembly reference in a top level project, or a dependent assembly of assemblies, assemblies are typically load on an as needed basis, unless explicitly loaded by user code. The same is true of dependent assemblies. See When does a .NET Assembly Dependency get loaded for more info.


[EDIT]

The classes in any assembly are loaded only when they are used. Not all the classes will be loaded at once in the memory. Even static classes are loaded in the main memory when they are first referenced. So having bulk of unused classes in your assembly will not degrade the performance but will increase the hard disk space required(not memory requirements).

So the simple answer is NO.

ihimv
  • 243
  • 1
  • 7
  • 1
    Could not complete my answer due to stack exchnage restrictions but here are a few more helpful links for **further reading**: [Explicitly load an assembly at runtime](https://msdn.microsoft.com/en-us/library/25y1ya39.aspx) **,** [How the Runtime Locates Assemblies](https://msdn.microsoft.com/en-us/library/yx7xezcf(v=vs.110).aspx) **,** [Assemblies and the Global Assembly Cache](https://msdn.microsoft.com/en-us/library/ms173099.aspx) **,** [StackOverflow: C# Assemblies](http://stackoverflow.com/questions/2972732/what-are-net-assemblies) – ihimv Jan 28 '16 at 05:21
  • My question is more about the code in a single assembly. Is everything in it loaded to memory? In my case, if codes are used, is a runtime decission. – Christian Gollhardt Jan 28 '16 at 08:48
  • @ChristianGollhardt Are you asking whether the whole of `System.Windows.Forms` is loaded in memory if you just use one of its classes? – Alternatex Jan 28 '16 at 11:17
  • that is what he is asking and the answer is still **no** – ihimv Jan 28 '16 at 11:39