C, Python, Java memory management differences __python

Source: Internet
Author: User
Tags garbage collection
A, C + + compiled by the program memory is divided into the following parts 1, stack area (stack)-by the compiler to automatically allocate the release, store the function of the parameter values, local variables, and so on.  The operation is similar to the stack in the data structure. 2, heap area (heap)-Generally by the programmer assigned to release, if the programmer does not release, the program at the end may be reclaimed by the OS.  Note that it is different from the heap in the data structure, the distribution is similar to the list, hehe. 3, Global area (static)--------------------------------------------------------------- -The system releases 4 after the program is finished, the literal constant area-the constant string is here. When the program is finished, the system releases 5, the code area of the program-the binary code that holds the function body.
Example program This is written by a predecessor, very detailed//main.cpp int a = 0; Global initialization region char *P1;  Global uninitialized Zone main () {int b; stack char s[] = "abc"; stack char *p2; stack char *p3 = "123456"; 123456\0 in the constant area, p3 on the stack.  static int c = 0; global (static) initialization area P1 = (char *) malloc (10);  P2 = (char *) malloc (20);  Areas that are allocated 10 and 20 bytes are in the heap area. strcpy (P1, "123456");  123456\0 is placed in a constant area, and the compiler may optimize it with the "123456" that the P3 points to as a place. }
Two python Python is implemented in C. In the Python world, everything is an object. Integers, strings, types (such as String types) are objects. The "Class" object in the object-oriented theory is implemented in Python through objects within Python. In Python, an object is a piece of memory that is applied on the heap by a struct in C. All built-in type objects, such as integer-type objects, string-type objects, are statically initialized. In Python, once an object is created, its size in memory is constant (and the Java The object in the heap new is dynamically allocating memory at run time. If you want to accommodate objects with variable-length data, you can maintain only one area of memory that points to a variable size within the object. Like Java and C #, Python provides a garbage collection (GC) mechanism for memory. All memory management mechanisms in Python have two sets of implementations. These two sets of implementations are controlled by the compiler symbol PYMALLOC_DEBUG. When the symbol is defined, use the memory management mechanism in debug mode. This mechanism, in addition to the normal memory management actions, also records a lot of memory information to facilitate Python debugging during development. When the symbol is not defined, Python's memory management mechanism only carries out normal memory management actions.
Three, Java Java to divide the memory into 4 parts  : 
1, Stack area (stacksegment)-Automatically assigned by the compiler release, store the function of the parameter value, local variable value, etc., after the completion of the specific method The system automatically frees the JVM memory resources. 2. Heap area (heapsegment)-typically released by programmers, storing objects and arrays created by new, the JVM does not periodically view the object and reclaims it if no reference is directed to the object. 3. Static area (datasegment)-holds global variables, static variables and string constants, does not release 4, code area (codesegment)-holds binary code for the method in the program, and multiple objects share a code space area.
Java automatically manages stacks and heaps, and programmers cannot directly set stacks or heaps.         When you define a variable in a block of code, Java allocates memory space for the variable in the stack, and when the variable is scoped, Java automatically releases the memory space allocated for the variable, which can be used as an immediate alternative.
        After an array or object is generated in the heap, you can also define a special variable in the stack so that the value of the variable in the stack equals the first address of the array or object in the heap memory, and the variable in the stack becomes the reference variable for the array or object. An important peculiarity of the

Stack is that the data in the stack can be shared. Note that this sharing of data is different from the two-object reference to an object, because the modification of a does not affect B, it is done by the compiler, and it helps save space. While an object reference variable modifies the internal state of the object, it affects another object reference variable.

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.