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 allocate memory for object creation in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In order to solve the problem of how to allocate memory for object creation in Java, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. Pointer collision

Assuming that the memory in the java heap is absolutely regular, with all used memory on one side and free memory on the other side, with a pointer in the middle as an indicator of the demarcation point, the memory allocated by the object simply moves the pointer to the free space by a distance equal to the size of the object.

2. Free list

If the memory in the java heap is not regular and the used memory and free memory are interlaced, the virtual machine must maintain a list of which memory is available, find a large enough space from the list to allocate to the object instance, and update the records on the list.

In the case of concurrency, threads are not safe, and memory may be allocated to object A, the pointer has not been modified, and object B uses the original pointer to allocate memory at the same time.

Solution:

1. CAS virtual machine uses CAS + failed retry to ensure the atomicity of update operation.

2. Local thread allocation cache (TLAB), that is, each thread allocates a small piece of memory in the heap beforehand, and which thread allocates memory on the TLAB of which thread. Only when the TLAB runs out and allocates the new TLAB, will you need to use the 1 method. Whether the virtual machine uses TLAB or not can be set by the-XX:+/-UseTLAB parameter.

This is the answer to the question about how to create and allocate memory for objects in Java. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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

Internet Technology

Wechat

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

12
Report