Space occupied by C # code

Source: Internet
Author: User
Tags creative commons attribution

Is the code take up space, if a program initializes the code that requires 100M, then after his initialization, the code will not work, he will occupy space? This article has been tested to find that the code will also occupy space.

I wrote 2k garbage code and put him on a project BHGPSWNB, using another project to quote him. Do you think the software needs a lot of memory to put the code when it is running?

The project referencing the garbage program is Reklnma, first just add the project reference in the reference, and then in the code that does not use BHGPSWNB this project, I run the following code

        static void Main(string[] args)        {            Console.ReadKey();        }

Consumes 7 M of memory, and if you run a 2k garbage code BHGPSWNB program, you need 8M. Using the method is to create a class, which is one of the garbage code, so you need to put the DLL in memory.

        static void Main(string[] args)        {            var ablkekbuuimc = new Ablkekbuuimc();            ablkekbuuimc.Aaxfyerenjmfe(2);            Console.ReadKey();        }

If you feel that the memory required to create a class is too large, then I use the following code, just get a type, but the memory required is 8M because the program will load another program

        static void Main(string[] args)        {            Type t = typeof(Ablkekbuuimc);            Console.ReadKey();        }

Here is a different way to write, remove the direct reference to the garbage program. Using the Load method to load, you can see the garbage program BHGPSWNB 8 m, the general library is not so large.

        static void Main(string[] args)        {            var file = new FileInfo("BhgpsWnb.exe");            Assembly.LoadFile(file.FullName);            Console.ReadKey();        }

The result is more than 8M of memory, so the code also needs memory, and once loaded it is not unloaded from the assembly.

If the assembly is loaded, loading the assembly requires a lot of memory, even if the uninstaller is not used

        static void Main(string[] args)        {            var app = Load();            GC.Collect();            GC.WaitForFullGCComplete();            Console.ReadKey();            AppDomain.Unload(app);            Console.ReadKey();            GC.Collect();            GC.WaitForFullGCComplete();        }        private static AppDomain Load()        {            var file = new FileInfo("BhgpsWnb.exe");            var otherAssemblyBytes = File.ReadAllBytes(file.FullName);            var app = AppDomain.CreateDomain("BhgpsWnb");            app.Load(otherAssemblyBytes);            return app;        }

The above code uses the load to need 20M of memory, after the use of Unload after the actual memory is not reduced, it is recommended not to use the assembly loading method, this method uses a lot of memory.

Can be loaded by specifying the name, you can see the following code needs to use less memory, need 9M, but after Unload did not reduce memory

        static void Main(string[] args)        {            var app = Load();            GC.Collect();            GC.WaitForFullGCComplete();            Console.ReadKey();            AppDomain.Unload(app);            GC.Collect();            GC.WaitForFullGCComplete();            Console.ReadKey();        }        private static AppDomain Load()        {            var file = new FileInfo("BhgpsWnb.exe");            var app = AppDomain.CreateDomain("BhgpsWnb", null, file.DirectoryName, file.DirectoryName, false);            app.Load("BhgpsWnb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");            return app;        }

So in loading the DLL, do not use the file as a byte read out, and then load, this method requires a lot of memory.


This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Welcome to reprint, use, republish, but be sure to keep the article Attribution Lindesi (including Link: http://blog.csdn.net/lindexi_gd), not for commercial purposes, based on the modified works of this article must be issued with the same license. If you have any questions, please contact me.

Space occupied by C # code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.