About heap and stack in. Net memory

Source: Internet
Author: User

At noon today, Teacher Li Jianzhong sent a message about the good class performance or the good struct performance (in other words, the good heap performance? Or is stack performance good ?) TopicWeiboThis has aroused heated discussions among cool developers, including Lao Zhao. As a background of. NET development, struct is generally considered to have better performance than class, becauseProgramIn the memory, struct is kept on the stack, while class is stored on the managed stack, and the stack performance is higher than the stack performance. Why, based on the following considerations (note:Here we will discuss the heap and stack in the. NET application memory, not the heap and stack in the data structure.):

1) stack space in. NET is self-maintained by memory, which is cleared when used up;

2). Net requires the Garbage Collector to recycle and release the heap space. When the Garbage Collector starts the recycle job, it will pause all the threads of your program andAlgorithmFind and delete objects that are no longer used in the program. GC then organizes all the remaining objects in the heap to save space, and adjusts all the pointers related to these objects in the stack and heap. This process is very performance-consuming, obviously it will be much harder than the value type in the memory automatic management stack.

If you understand the above, you may think that stack has better performance than class, but the question of "taking it for granted" is often not as simple as "taking it for granted, therefore, Lao Zhao asked, "When struct is passed, the entire object is copied, and the class is referenced, it seems that the copy reference cost is lower. Why do we say s performance is high? "?

Another important problem is that the value type may not always be stored on the stack,Value types and pointers are always placed in the places where they are declared.. For more information, clickArticleRead.

In object-oriented design, when we encounter questions like struct or class, besides considering the above mentioned performance, I think we should also consider the level of abstraction of your design objects. Objects with complicated logic and high abstraction are designed as classes (structures do not support inheritance of these features ). It is reasonable to design an object that only stores values and does not copy a large number of objects into a structure.

So should I use struct or class? I 'd like to hear a Summary of the problem that Mr. Li Jianzhong finally threw out: discussing whether the class performance is good or the struct performance is good, just like discussing whether "giving birth to a child is mainly man's credit or woman's credit. Put it simply: the heap can be used for a long life cycle (shared access among several stacks and globally held), and the stack can be used for a short life cycle (only in your own stack, without copying, do not pass, do not save to set ). Therefore, heap + Stack has the best performance.

If you have a better understanding and understanding of this issue, please leave a message for discussion to help you better understand the difference between stack and stack to write a higher qualityCodeProgrammer.

Postscript: Today, Lao Zhao sent a lot of Weibo posts to discuss this issue. You can visit Lao Zhao's Weibo posts to discuss the problem of class and struct being superior or inferior, lao Zhao's current point of view is "the conclusion of this question is that there is basically no conclusion", but we must choose one of them in class and struct during program design, weigh and decide who to choose.

Reference: C # heap (ing) vs stack (ing) in. Net: Part I

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.