当前位置:网站首页>面试官: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:
边栏推荐
- Detailed pointer advanced 1
- Location of software installation information and system services in the registry
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- Qt插件之自定义插件构建和使用
- Reflection on some things
- Go语言自学系列 | golang中的if else语句
- Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
- "Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
- Redis installation under windows and Linux systems
- uploads-labs靶场(附源码分析)(更新中)
猜你喜欢
【OpenCV 例程200篇】217. 鼠标交互获取多边形区域(ROI)
[web security] - [SQL injection] - error detection injection
App mobile terminal test [4] APK operation
Initial test of scikit learn Library
Shell script import and export data
Microservice - declarative interface call openfeign
Srs4.0+obs studio+vlc3 (environment construction and basic use demonstration)
App移动端测试【4】apk的操纵
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
CString getbuffer and releasebuffer instructions
随机推荐
Function introduction of JMeter thread group
How to thicken the brush in the graphical interface
Effect of ARP package on FTP dump under vxworks-6.6 system
VC下Unicode和ANSI互转,CStringW和std::string互转
Detailed pointer advanced 1
Go language self-study series | if else if statement in golang
[web security] - [SQL injection] - error detection injection
Location of software installation information and system services in the registry
CString getbuffer and releasebuffer instructions
Mongodb installation and basic operation
Microservice API gateway
Break through 1million, sword finger 2million!
嵌入式开发:避免开源软件的7个理由
The difference between calling by value and simulating calling by reference
【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
Microservices - load balancing ribbon
Large CSV split and merge
CString中使用百分号
Mb10m-asemi rectifier bridge mb10m
Driver and application communication