Why do unmanaged languages ​​offer the ability to manually delete objects?

Suppose you want to write a high-performance method that processes a large data set. Why should developers not enable manual memory management, and not force a switch to C or C ++?

void Process()
{
    unmanaged
    {
        Byte[] buffer;
        while (true)
        {
            buffer = new Byte[1024000000];

            // process

            delete buffer;
        } 
    }   
}
+3
source share
6 answers

, ( , GC) , , . GC, , .

, #, , , :

void Process()
{
    unsafe
    {
        byte* buffer;
        while (true)
        {
            buffer = Marshal.AllocHGlobal(1024000000);

            // process

            Marshal.FreeHGlobal(buffer);
        } 
    }   
}

, , C/++, # - buffer[i] buffer+i .

+13

, , , C ++. .

: . , , , ++, .

. , , , . #, #. , . , , , , C ++, . ( ++, , #. .)

, . C90 , Fortran , C- . , .

# , . , -, . , , . , .

, - , : , . .

+3

, ?

0

.NET , , . , ( ), , , .

, , , .

0

, , , , GC , ( ) , .

0

For the same reason, most cores will not allow you to plan your own threads. Since you really don't need 99.99 %% of time, and exposing this functionality, the rest of the time will only tempt you to do something potentially stupid / dangerous.

If you really need fine grain memory management, write this section of code in something else.

0
source

Source: https://habr.com/ru/post/1725074/


All Articles