当前位置:网站首页>面试官:JVM如何分配和回收堆外内存
面试官:JVM如何分配和回收堆外内存
2022-07-03 16:04:00 【InfoQ】
JVM内存模型

如何分配堆外内存
第一种方式:ByteBuffer#allocateDirect
//分配10M的内存
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(10 * 1024 * 1024);第二种方式:Unsafe#allocateMemory
public class Test {
private static Unsafe unsafe = null;
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//分配10M的内存
Field getUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
getUnsafe.setAccessible(true);
unsafe = (Unsafe)getUnsafe.get(null);
//分配完内存返回内存的地址
long address = unsafe.allocateMemory(10 * 1024 * 1024);
}
}如何回收堆外内存
第一种方式:Unsafe#freeMemory
public class Test {
private static Unsafe unsafe = null;
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
//分配10M的内存
Field getUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
getUnsafe.setAccessible(true);
unsafe = (Unsafe)getUnsafe.get(null);
//分配完内存返回内存的地址
long address = unsafe.allocateMemory(10 * 1024 * 1024);
//回收分配的堆外内存
unsafe.freeMemory(address);
}
}第二种方式:JVM回收堆外内存

- JVM执行Full GC时会将DirectByteBuffer进行回收,回收之后Clearner就不存在引用关系
- 再下一次发生GC时会将Cleaner对象放入ReferenceQueue中,同时将Cleaner从链表中移除
- 最后调用unsafe#freeMemory清除堆外内存
注意点
注意点1:
注意点2:
边栏推荐
- "Remake Apple product UI with Android" (3) - elegant statistical chart
- CString getbuffer and releasebuffer instructions
- 相同切入点的抽取
- do{}while()的妙用
- Please be prepared to lose your job at any time within 3 years?
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
- Colab works with Google cloud disk
- MB10M-ASEMI整流桥MB10M
- Unity功能——Unity离线文档下载及使用
- 无心剑中译泰戈尔《漂鸟集(1~10)》
猜你喜欢

Jmeter线程组功能介绍

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14

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

How are integer and floating-point types stored in memory

CString的GetBuffer和ReleaseBuffer使用说明

Unity功能——Unity离线文档下载及使用

Semi supervised learning

远程文件包含实操

使用AUR下载并安装常用程序

Please be prepared to lose your job at any time within 3 years?
随机推荐
[system safety] 43 PowerShell malicious code detection series (5) automatic extraction of ten thousand words from abstract syntax tree
Three dimensional reconstruction of deep learning
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
坚持输出需要不断学习
Get the executable path through the process PID (queryfullprocessimagename)
【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示
Microservice API gateway zuul
Vs2017 is driven by IP debugging (dual machine debugging)
Microservice API gateway
Project -- high concurrency memory pool
Go语言自学系列 | golang中的if else语句
How to use AAB to APK and APK to AAB of Google play apps on the shelves
Shell script import and export data
Break through 1million, sword finger 2million!
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
Problems of CString in multithreading
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Backtracking method to solve batch job scheduling problem
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表