C # Language Specification--1.4 automatic memory management

Source: Internet
Author: User
Tags array manual empty execution garbage collection require requires return
Specification

Manual memory management requires developers to manage the allocation and recycling of memory blocks. Manual memory management can be time-consuming and cumbersome. Automatic memory management is provided in C #, freeing developers from this heavy task. In most cases, automatic memory management can improve code quality and developer productivity, and will not adversely affect presentation capability or performance.

Example

Using System;

public class Stack

{

Private Node-i = null;

public bool Empty {

get {

return (a = null);

}

}

public Object Pop () {

if (i = null)

throw new Exception ("Can" t Pop from a empty Stack. ");

else {

Object temp = i. Value;

A. Next;

return temp;

}

}

public void Push (object o) {

The new Node (O, a)

}

Private Class Node

{

Public Node Next;

public object Value;

Public Node (object value): This (value, null) {}

Public node (object value, Node next) {

Next = next;

Value = value;

}

}

}

Shows a Stack class, which is actually a linked table of Node instances. Node instances are created in the Push method and are garbage collected when they are no longer needed. When any code is no longer likely to access a Node instance, the instance becomes the object of garbage collection. For example, when an item is removed from a Stack, the associated Node instance conforms to the garbage collection criteria and waits to be reclaimed.

Example

Class Test

{

static void Main () {

Stack s = new stack ();

for (int i = 0; i < i++)

S.push (i);

s = null;

}

}

Shows the code that uses the Stack class. The code creates a Stack class, initializes the class with 10 elements, and assigns it a null value. After assigning a null value to the variable s, Stack and the associated 10 Node instances become eligible for garbage collection. The garbage collector can immediately clear the above instance, but it does not require that it be cleared immediately.

The underlying garbage collector for the C # service can work by moving objects in memory, which is not visible to most C # developers. C # provides the ability to write "unsafe" code for developers who are typically satisfied with automatic memory management, but sometimes require precise control or subtle performance tuning. Such code can directly handle pointer types and object addresses. However, C # requires programmers to fix objects and temporarily prevent the garbage collector from moving them.

From the perspective of developers and users, this "unsafe" code feature is actually a "safe" feature. Unsafe code must be clearly marked with modifiers unsafe in code so that developers cannot inadvertently use unsafe language features, and the compiler and execution engine work together to ensure that unsafe code cannot impersonate security code. These restrictions limit the use of unsafe code to situations where the code is trusted.

Example

Using System;

Class Test

{

static void Writelocations (byte[] arr) {

unsafe {

Fixed (byte* Parray = arr) {

byte* Pelem = Parray;

for (int i = 0; i < arr. Length; i++) {

byte value = *pelem;

Console.WriteLine ("Arr[{0}] at 0x{1:x} is {2}",

I, (UINT) Pelem, value);

pelem++;

}

}

}

}

static void Main () {

byte[] arr = new byte[] {1, 2, 3, 4, 5};

Writelocations (arr);

}

}

A method named Writelocations is displayed. It contains an unsafe block that holds an array instance and then accesses the elements of the array one at a time using a pointer operation. The index, value, and location of each array element are written to the console. Here is an example of a possible output:

Arr[0] at 0x8e0360 is 1

ARR[1] at 0x8e0361 is 2

ARR[2] at 0x8e0362 is 3

ARR[3] at 0x8e0363 is 4

ARR[4] at 0x8e0364 is 5

Of course, the exact memory location may vary depending on the application's execution.



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.