Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the memory consumption of code?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to understand the memory consumption of code?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Memory management in different languages

Different programming languages have their own memory management methods.

The application and release of this kind of memory heap space is entirely on your own.

Java relies on JVM for memory management, does not understand the mechanism of jvm memory management, and is likely to cause memory leaks or memory overflows due to some incorrect code writing.

Python memory management is managed by the private heap space, where all python objects and data structures are stored. The programmer does not have access to the heap and only the interpreter can operate.

For example, Python is all objects and encapsulates memory operations very well, so the basic data types of python will use much more memory than storing pure data types. For example, we all know that it takes four bytes to store int data, but using Python to apply for an object to store data takes much more than four bytes.

Memory Management of C++

Take C++ as an example to introduce the memory management of the programming language.

If we write C++ programs, we should know the concepts of stack and heap. The memory space needed for the program to run is divided into fixed parts and variable parts, as follows:

The memory consumption of the fixed part does not change with the running of the code, while the variable part does.

More specifically, the amount of memory consumed by a program compiled by CumberCraft + is divided into the following parts:

Stack: automatically allocated and released by the compiler, storing the parameter values of the function, the values of local variables, etc., in a manner similar to the stack in the data structure.

Heap: generally, it is allocated and released by the programmer. If the programmer does not release it, the program may be reclaimed by OS at the end of the program.

Uninitialized data area (Uninitialized Data): stores uninitialized global and static variables

Initialization data area (Initialized Data): stores initialized global and static variables

Program code area (Text): the binary code that stores the body of a function

The space occupied by both the code area and the data area is fixed, and the space occupied is very small, so the memory consumed at run time mainly depends on the variable part.

In the variable part, the stack interval data is automatically reclaimed after the execution of the code block, while the heap interval data needs to be recycled by the programmer, so it is the birthplace of the memory leak.

For Java and Python, there is no need for programmers to think about memory leaks, and virtual machines do these things.

How to calculate how much memory a program takes up

If you want to figure out how much memory your program will consume, you must know the size of the data type you define, as follows:

Note there are two differences in the diagram. Why does a 64-bit pointer take up 8 bytes while a 32-bit pointer takes up 4 bytes?

One byte accounts for 8 bits, then 4 bytes is 32 bits, and the size of the data that can be stored is 2 ^ 32, that is, the size of 4G space, that is, you can find a memory address the size of 4G space.

The computers we use now are generally 64-bit, so the compilers are also 64-bit.

Computers with 64-bit operating systems have more than 4 gigabytes of memory, that is, if the pointer size is still 4 bytes, you can no longer address all memory addresses. so the 64-bit compiler uses 8-byte pointers to find all memory addresses.

Note that 2 ^ 64 is a very large number, which is enough to find an address.

Memory alignment

Let's introduce another important knowledge point in memory management: memory alignment.

Don't think that memory alignment can only be achieved with CAccord +. Memory alignment is required for all programming languages that can cross platforms. Java and Python are the same.

And this is the question that the interviewer likes to ask very much in the interview, that is: why is there memory alignment?

There are two main reasons

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Platform reason: not all hardware platforms can access any data on any memory address, and some hardware platforms can only take certain types of data at certain addresses, otherwise a hardware exception is thrown. In order for the same program to run on multiple platforms, memory alignment is required.

Hardware reason: after memory alignment, the speed of CPU accessing memory is greatly improved.

Can you take a look at the size of each data type output by this C++ code?

Struct node {int num; char cha;} st; int main () {int a; char b; cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report