Is memory management in different languages ​​quite similar to the transfer of my knowledge?

I'm just starting to learn programming. And at the moment I know a little memory management in Objective-C. It was not easy to learn.

So, just out of curiosity, is the memory management used in major languages ​​like C, C ++, Java, etc., in any way similar to what I learned?

+4
source share
6 answers

Memory management takes place in two different ways: unmanaged and managed.

Unmanaged is C / C ++ where the programmer is responsible for allocating memory.

Managed is like Java / .Net, where the memory is allocated for you but cleared by the virtual machine ("garbage collection").

In these two options you will find many options.

+14
source

No, it can vary significantly between platforms - and even on the same platform there may be different options. (e.g. in C ++, you can use automatic pointers, GC Boehm, etc.)

Java and .NET have basically the same memory management, mind you.

+3
source

No, that’s different.

Java and .NET have the concept of automatic memory management, which includes garbage collectors. The implementation of garbage collectors again varies from language to language and from platform to platform.

C / C ++ does not have automatic memory management, and the programmer is responsible for programming.

In short, it is different for different languages.

+3
source

I don’t know how memory is managed in objective-c, but C and C ++ use manual memory management, and Java has a built-in Garbage assembly and does not allow manual memory management. Therefore, they are very different.

0
source

G'day

The only thing that can be said about changing memory management in different languages ​​is goals. Namely:

  • provide a fixed piece of memory for the playback process,
  • protect memory outside of this piece from access to the process,
  • provide a dynamic distribution mechanism for variables / objects / functions / etc. in the process,
  • ensure that the memory allocations for these elements are performed at reasonable boundaries, a reasonable creature in terms of the processor,
  • provides a mechanism to free memory as needed,
  • clean (garbage collection) unused objects,
  • combines fragmented memory into adjacent pools of occupied memory,
  • and etc.

Different languages ​​and runtimes provide mechanisms for implementing at least some of these functions.

NTN

amuses

0
source

The approaches to memory management vary widely across languages ​​and platforms, not only at the level of visibility and control of the programmer, but also at implementation.

However, the basics of allocating and freeing memory are about the same when you go to the OS level. Of course, there are differences, tweaks and optimizations, but usually the programmer does not need to deal with such details.

Objective-C is an interesting hybrid, since version 2.0 of the language has been added to the garbage collection, but retains the ability to use reference counting (save / release / auto-advertisement). In fact, the same code can work in any mode, depending on the compilation flags and settings of other code loaded in the same process. This is not typical for programming languages ​​- usually you get either managed (automatic) or uncontrolled (manual) based on the code you write, and sometimes the language / platform does not allow you to choose at all (for example, Java).

One taste is not necessarily better than another, and there is still religious debate about whether "real programmers [don't use] garbage collection" really, but don't worry too much about it. General knowledge of how different approaches to memory management never harm anyone, and, as a rule, it’s enough to understand the approach to the language (s) in which you are coding.

0
source

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


All Articles