当前位置:网站首页>Interviewer: how does the JVM allocate and recycle off heap memory
Interviewer: how does the JVM allocate and recycle off heap memory
2022-07-03 16:13:00 【InfoQ】
JVM Memory model

How to allocate off heap memory
The first way :ByteBuffer#allocateDirect
// Distribute 10M Of memory
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10 * 1024 * 1024);The second way :Unsafe#allocateMemory
public class Test {
private static Unsafe unsafe = null;
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
// Distribute 10M Of memory
Field getUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
getUnsafe.setAccessible(true);
unsafe = (Unsafe)getUnsafe.get(null);
// After allocating memory, return the address of memory
long address = unsafe.allocateMemory(10 * 1024 * 1024);
}
}How to reclaim off heap memory
The first way :Unsafe#freeMemory
public class Test {
private static Unsafe unsafe = null;
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
// Distribute 10M Of memory
Field getUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
getUnsafe.setAccessible(true);
unsafe = (Unsafe)getUnsafe.get(null);
// After allocating memory, return the address of memory
long address = unsafe.allocateMemory(10 * 1024 * 1024);
// Reclaim the allocated off heap memory
unsafe.freeMemory(address);
}
}The second way :JVM Reclaim off heap memory

- JVM perform Full GC when DirectByteBuffer To recycle , After recycling Clearner There is no reference relationship
- Next time GC when Cleaner Objects in the ReferenceQueue in , At the same time Cleaner Remove from list
- Last call unsafe#freeMemory Clear out of heap memory
Be careful
Be careful 1:
Be careful 2:
边栏推荐
- The mixlab editing team is recruiting teammates~~
- Multithread 02 thread join
- How idea starts run dashboard
- ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
- 关于网页中的文本选择以及统计选中文本长度
- Reflection on some things
- PHP CI(CodeIgniter)log级别设置
- [list to map] collectors Tomap syntax sharing (case practice)
- A Fei's expectation
- Microservice API gateway zuul
猜你喜欢

Shell script import and export data

Project -- high concurrency memory pool

初试scikit-learn库

nifi从入门到实战(保姆级教程)——flow

The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)

深入理解 SQL 中的 Grouping Sets 语句

Mongodb installation and basic operation

Stm32f103c8t6 firmware library lighting

Microservice - Nacos registration center and configuration center

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
随机推荐
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
半监督学习
近视:摘镜or配镜?这些问题必须先了解清楚
Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
首发!!lancet饿了么官方文档
PHP二级域名session共享方案
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
TCP拥塞控制详解 | 3. 设计空间
高等数学(第七版)同济大学 习题2-1 个人解答
Project -- high concurrency memory pool
用通达信炒股开户安全吗?
[list to map] collectors Tomap syntax sharing (case practice)
Shell script import and export data
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
EditText request focus - EditText request focus
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
Remote file contains actual operation
Break through 1million, sword finger 2million!
相同切入点的抽取
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)