当前位置:网站首页>面试官: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:
边栏推荐
- 嵌入式开发:避免开源软件的7个理由
- Semi supervised learning
- Reading notes of "micro service design" (Part 2)
- Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Persisting in output requires continuous learning
- Stm32f103c8t6 firmware library lighting
- CString getbuffer and releasebuffer instructions
- Redis高可用与持久化
- Detailed pointer advanced 1
猜你喜欢
Embedded development: seven reasons to avoid open source software
Wechat payment -jsapi: code implementation (payment asynchronous callback, Chinese parameter solution)
Brush questions -- sword finger offer
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
Myopia: take off or match glasses? These problems must be understood clearly first
使用AUR下载并安装常用程序
About text selection in web pages and counting the length of selected text
Project -- high concurrency memory pool
Subclass hides the function with the same name of the parent class
CString的GetBuffer和ReleaseBuffer使用说明
随机推荐
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Microservice - fuse hystrix
Unity function - unity offline document download and use
App移动端测试【3】ADB命令
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
Break through 1million, sword finger 2million!
相同切入点的抽取
利用MySQL中的乐观锁和悲观锁实现分布式锁
Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
Detailed explanation of string function and string function with unlimited length
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
Go language self-study series | if else if statement in golang
uploads-labs靶场(附源码分析)(更新中)
Go语言自学系列 | golang switch语句
Project -- high concurrency memory pool
Approval process design
Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
近视:摘镜or配镜?这些问题必须先了解清楚
Distributed task scheduling XXL job