当前位置:网站首页>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:
边栏推荐
- Unreal_DataTable 实现Id自增与设置RowName
- First knowledge of database
- MB10M-ASEMI整流桥MB10M
- PHP二级域名session共享方案
- Reflection on some things
- Three dimensional reconstruction of deep learning
- Persisting in output requires continuous learning
- The mixlab editing team is recruiting teammates~~
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- Distributed task scheduling XXL job
猜你喜欢

【Proteus仿真】74HC595+74LS154驱动显示16X16点阵

“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Jmeter线程组功能介绍

【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)

Microservice - fuse hystrix

初试scikit-learn库

面试官:JVM如何分配和回收堆外内存

ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装

"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
随机推荐
Microservice API gateway
【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事
深度学习之三维重建
How idea starts run dashboard
PHP中register_globals参数设置
NSQ源码安装运行过程
Microservice - fuse hystrix
Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
The difference between calling by value and simulating calling by reference
Shell script import and export data
Go语言自学系列 | golang switch语句
App mobile terminal test [5] file writing and reading
深入理解 SQL 中的 Grouping Sets 语句
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
June to - -------
首发!!lancet饿了么官方文档
Detailed explanation of four modes of distributed transaction (Seata)
[combinatorics] combinatorial identities (review of eight combinatorial identities | product of combinatorial identities 1 | proof | use scenario | general method for finding combinatorial numbers)