C # Object Memory model

Source: Internet
Author: User

Object Memory model

Object memory model for C #
The main purpose of writing this blog is to deepen their understanding, if there is no place, please forgive me.

Object memory model for C #:

One, stack memory and heap memory
1. Stack memory

Automatically allocated and released by the compiler, mainly to save some local variables, function parameters, such as int a = 10, then the compiler will automatically open a stack of content to store the variable A.
2. Heap Memory

Manually applied and released by programmers, in C + +, with the New keyword application, the compiler will not be freed, must be released through Delete, for C #, through the new keyword, because of the compiler's garbage collection mechanism, programmers do not need to manually free memory. For example, we declare an object zhangsan,student Zhangsan = new Student () for class student, first, the compiler allocates a memory storage variable Zhangsan on a stack, Then a piece of memory is created on the heap to store the student object, and finally the address on the heap is stored in the variable Zhangsan, if we create an object lisi,student lisi= new Student (), then Lisi = Zhangsan, The only thing that changes is the Lisi value stored on the stack, which is the address of the object pointed to on the heap, as shown in:

3, stack memory and heap memory comparison

Memory allocation

Stack: LIFO, the size of the corresponding type is automatically assigned by the compiler, the size of the allocation is limited by the size of the stack;

Heap: Discretionary allocation, manually requested by the programmer to specify the size of the allocated size is limited by virtual memory.

Efficiency

Stack: High

Heap: Low relative stack

Second, value types and reference types

1. Value type

A value type variable stores the value of a variable and is stored directly in the stack memory;

2. Reference type

A reference type variable stores the address of the memory where the variable resides, the actual data of the reference type variable is stored in the heap memory, the variable itself is stored in the stack memory, the address to the heap is stored, usually four bytes, and an address value is saved.

Value types in C #: Struct,enum (for int,float types, both struct types)

Reference type: Class,delegate,array,interface

As shown in the following examples:

Third, deep copy and shallow copy

We often encounter this problem in programming, we already have an object A, and object a already has some specific values, now we want to create a copy of the object B, we want to manipulate object B without changing the value of object A, which means that object A and object B are two completely independent objects, This is a deep copy. Deep copy Concept: The source object and the Copy object are independent of each other, and any one of the objects changes will not affect the other object.

When two objects point to the same address, if we change the value of one of the objects, the other object is also changed accordingly, which is a shallow copy. The concept of a shallow copy: When two objects are not completely "detached" in a shallow copy, changing the contents of the top-level object without affecting the other object, but changing the contents of the sub-object, two objects are changed at the same time.

The result of this difference is whether copying the memory or copying the pointer when copying the sub-object. A deep copy allocates a memory space for a sub-object and copies the contents of it; a shallow copy simply points the pointer to the original child object.

Something to watch out for

(1): String object is a reference object, but very special, it behaves like a value object, that is, it is assigned, split, merge, not the original string operation, but rather return a new string object

(2): Array object is a reference object, when it is assigned, it actually returns another copy of the source object, so if you want to make a real copy of the array object (deep copy), then you need to create a new copy of the object, and then copy the value of the source array into the destination object

(go) C # object memory model

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.