当前位置:网站首页>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:
边栏推荐
猜你喜欢

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

NSQ源码安装运行过程
![[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)](/img/04/460734209ec315c5c02cb3fae4bf0e.png)
[200 opencv routines] 217 Mouse interaction to obtain polygon area (ROI)

探索Cassandra的去中心化分布式架构

请做好3年内随时失业的准备?

深度学习之三维重建

半监督学习

Getting started with Message Oriented Middleware
![[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)](/img/68/3721975cf33fcfacc28dc4d3d6a5ca.jpg)
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)

Redis installation under windows and Linux systems
随机推荐
Getting started with Message Oriented Middleware
Hibernate的缓存机制/会话级缓存机制
嵌入式开发:避免开源软件的7个理由
深度学习之三维重建
如何在本机搭建SVN服务器
几种常见IO模型的原理
Page dynamics [2]keyframes
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Principles of several common IO models
About text selection in web pages and counting the length of selected text
Jmeter线程组功能介绍
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
面试官:JVM如何分配和回收堆外内存
MongoDB 的安装和基本操作
突破100万,剑指200万!
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
Please be prepared to lose your job at any time within 3 years?
【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事
Golang 匿名函数使用