Heap, stack, value type, and reference type

Source: Internet
Author: User

Stack features fast and efficient, but its disadvantage is that there are limits and data is not flexible. Stack features flexibility and convenience, wide Data Adaptability, but the efficiency is reduced. Stack memory allocation is automatically released, so there is no release function. Heap space allocation is always dynamic. Although all data space will be released back to the system at the end of the program, precise memory application/release matching is the basic element of a good program.. NET.

 

Except for the interface, the types in the general type system are divided into two types: Value Type and reference type. There are some performance problems when using the reference type. Therefore, if a system uses all the reference types, the performance of this system will be greatly reduced. the NET Framework designer retains the struct data type, but there is no struct data type in java.

 

Value Type:

Including primitive type, user-defined structure, or enumeration type

Inherited from System. ValueType type

Allocate on Stack

Assign values by copying

By default, parameters are passed by value unless the ref keyword is used.

 

Reference Type:

C # pre-defined reference types include object and string. User-defined reference types can be interface type, class type, and delegate type.

Inherited from System. Object Type

Use the "new" keyword for allocation on the managed Stack

Assign values by referencing

Several references may point to the same object. Therefore, operations on one variable may affect the same object referenced by another variable.

Use references to pass variables

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.