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 memory allocation in Java

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

Share

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

This article focuses on "how to understand memory allocation in Java". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the memory allocation in Java.

As we all know, JVM is an abstract computer that enables computers to run programs. JVM loads code, validates code, executes code, manages memory (allocates memory from OS, manages Java allocation (including heap compression and garbage collection), and provides a runtime environment.

Memory allocation is part of the JVM [Java virtual machine]. Therefore, it is important to understand why Java needs JVM and no other languages need VM at this point. In many other languages, compilers generate machine code for specific systems, but in Java, the Java compiler always generates code for virtual machines (called JVM) rather than for specific systems. Is JVM, which has platform-dependent tags that make Java (a platform-independent programming language) possible. JVM converts Java bytecode into a machine-interpretable language. The two main functions of JVM are to allow Java programs to run on any device or operating system and to manage and optimize program memory.

Memory management is a form of resource management applied to computer memory. The basic requirement of memory management is to provide a way to allocate some of the memory to the program at the request of the program and release it for reuse when it is no longer needed.

As mentioned above, we will continue to learn about the concept of how to allocate memory and what errors can occur if there is insufficient memory. Memory allocation is the operation of allocating physical or virtual memory address space to a process.

JVM memory is divided into several parts: heap, non-heap, and others.

Heap memory is the runtime data area from which memory is allocated for all instances and arrays of java classes. The heap is created when JVM starts, and the heap size may increase or decrease as the application runs.

Non-heap memory is created at JVM startup and stores the structure of each class, such as field and method data and inline strings. [String Interning is a way to store only one copy of each different string value, immutable. Applying String.intern () to several strings ensures that all strings with the same content share the same memory]. The other memory is used to store the JVM code itself, the JVM internal structure, the loaded profile proxy code, and data. The two basic methods of memory allocation are static and dynamic memory allocation.

Static memory allocation allocates memory before execution, while dynamic memory allocation allocates memory to processes during execution.

Memory allocation is a general aspect of the term "binding". Suppose we declare a variable of a class type and create a reference to it. In order to store any value in these variables, we must allocate memory for it. Therefore, we can refer to memory allocation as the act of assigning memory addresses to variable properties.

Eg: int a = 10

Static memory allocation means that when the compiler compiles the program and generates the target file, the linker merges the target file into an executable file and loads the executable file into the main memory with the help of the loader. Before statically allocating execution, you must know the amount of data the process needs to make it more efficient so that the process can be executed more quickly. Dynamic memory allocation occurs during program execution. Here, when entities are used for the first time during program execution, memory is allocated to them. It reduces memory waste because it allocates the exact memory space required by the entity.

Memory management is a function of the operating system that processes main memory and moves processes back and forth during execution. Some of the basic concepts dealing with memory management may be process address space, static | dynamic loading and linking.

(1) process address space: it is a set of logical addresses referenced by a process in its code. The operating system is responsible for mapping logical addresses to physical addresses when memory is allocated to the program. Three types of addresses are used before and after the memory allocation is completed. Symbolic address, relative address and physical address... Symbolic addresses are the addresses used by variable names, constants, and instruction tags in the source code. A relative address is an address that is translated from a symbolic address at compile time. When the program is loaded into the main memory, the loader generates a physical address. The virtual and physical addresses remain the same at compile time, but are different at run time.

(2) static and dynamic loading and linking: when developing a program, you should decide whether to load statically or dynamically. If we have to load the program statically, the entire program will be compiled and linked at compile time without leaving any external module dependencies. The linker combines the target program with other necessary target modules and logical addresses. If we are loading a dynamic program, the compiler will compile the program and a reference to the required module dependencies. The program will provide. When using static links, the linker combines all other modules required by the program into a single executable program to avoid any runtime dependencies. In the case of dynamic linking, you do not need to link the actual module or library to the program, but you can attach a reference to the thm.

Memory is not allocated when the object is declared, but is allocated when referencing. For memory allocation of objects, the new () method is used, so objects are always allocated in heap memory. Java memory allocation is divided into "heap", "stack", "code" and "static" sections for effective management. the "code" section contains bytecode, the "stack" contains methods, local variables and reference variables, the "heap" part contains "objects" and may also contain reference variables, and the "static" section contains only static data.

The most common error in memory allocation is "java.lang.OutOfMemoryError", which is usually raised when JVM cannot allocate objects due to insufficient memory and the garbage collector cannot use any more memory.

At this point, I believe you have a deeper understanding of "how to understand the memory allocation in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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