当前位置:网站首页>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:
边栏推荐
- 面试官:JVM如何分配和回收堆外内存
- Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
- Unity项目优化案例一
- 【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
- 《天天数学》连载56:二月二十五日
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- Thinking about telecommuting under the background of normalization of epidemic | community essay solicitation
- [web security] - [SQL injection] - error detection injection
- App移动端测试【3】ADB命令
- Nine ways to define methods in scala- Nine ways to define a method in Scala?
猜你喜欢
Mongodb installation and basic operation
Low level version of drawing interface (explain each step in detail)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Microservice API gateway
Break through 1million, sword finger 2million!
[statement] about searching sogk1997 and finding many web crawler results
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
Microservice - Nacos registration center and configuration center
随机推荐
如何在本机搭建SVN服务器
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
June to - -------
阿飞的期望
初试scikit-learn库
[combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
高等数学(第七版)同济大学 习题2-1 个人解答
Mixlab编辑团队招募队友啦~~
首发!!lancet饿了么官方文档
Microservices - load balancing ribbon
Stm32f103c8t6 firmware library lighting
Go language self-study series | golang switch statement
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
Microservice - declarative interface call openfeign
Distributed task scheduling XXL job
Getting started with Message Oriented Middleware
Remote file contains actual operation
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
[statement] about searching sogk1997 and finding many web crawler results
Hibernate的缓存机制/会话级缓存机制