当前位置:网站首页>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:
边栏推荐
- Go language self-study series | golang switch statement
- Multithread 02 thread join
- [combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
- Microservice API gateway
- Microservice - fuse hystrix
- PHP CI(CodeIgniter)log级别设置
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- [proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
- Mb10m-asemi rectifier bridge mb10m
- TCP擁塞控制詳解 | 3. 設計空間
猜你喜欢

关于网页中的文本选择以及统计选中文本长度

Brush questions -- sword finger offer
![App mobile terminal test [5] file writing and reading](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [5] file writing and reading

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

Microservice API gateway zuul

Myopia: take off or match glasses? These problems must be understood clearly first

Deep understanding of grouping sets statements in SQL

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

嵌入式开发:避免开源软件的7个理由
![[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix](/img/d6/3c21c25f1c750f17aeb871124e80f4.png)
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
随机推荐
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
Pandora IOT development board learning (HAL Library) - Experiment 5 external interrupt experiment (learning notes)
Unity项目优化案例一
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)
Distributed task scheduling XXL job
About text selection in web pages and counting the length of selected text
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
初试scikit-learn库
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
PHP CI(CodeIgniter)log级别设置
Colab works with Google cloud disk
Remote file contains actual operation
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
[proteus simulation] 8 × 8LED dot matrix screen imitates elevator digital scrolling display
Microservices Seata distributed transactions
How to use AAB to APK and APK to AAB of Google play apps on the shelves
面试官:JVM如何分配和回收堆外内存