当前位置:网站首页>Deep understanding of JVM (III) - memory structure (III)
Deep understanding of JVM (III) - memory structure (III)
2022-06-30 18:03:00 【Zhaoliwen is a pig】
In depth understanding of JVM( 3、 ... and )—— Memory structure ( 3、 ... and )
List of articles
6、 Direct memory
- Belongs to the operating system , Common in NIO In operation , For data buffers
- Higher allocated recovery costs , But the read-write performance is high
- Not subject to JVM Memory recovery management
File reading and writing process

Used DirectBuffer

Direct memory is the operating system and Java Code An area that can be accessed , There is no need to copy code from system memory to Java Heap memory , So it improves efficiency
Release principle
Direct memory recycling is not through JVM Garbage collection to release , But through unsafe.freeMemory To manually release
adopt
// adopt ByteBuffer apply 1M Of direct memory
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(_1M);
Apply for direct memory , but JVM It does not reclaim the contents of direct memory , How does it achieve recycling ?
allocateDirect The implementation of the
public static ByteBuffer allocateDirect(int capacity) {
return new DirectByteBuffer(capacity);
}
DirectByteBuffer class
DirectByteBuffer(int cap) { // package-private
super(-1, 0, cap, cap);
boolean pa = VM.isDirectMemoryPageAligned();
int ps = Bits.pageSize();
long size = Math.max(1L, (long)cap + (pa ? ps : 0));
Bits.reserveMemory(size, cap);
long base = 0;
try {
base = unsafe.allocateMemory(size); // Application memory
} catch (OutOfMemoryError x) {
Bits.unreserveMemory(size, cap);
throw x;
}
unsafe.setMemory(base, size, (byte) 0);
if (pa && (base % ps != 0)) {
// Round up to page boundary
address = base + ps - (base & (ps - 1));
} else {
address = base;
}
cleaner = Cleaner.create(this, new Deallocator(base, size, cap)); // By virtual reference , To release direct memory ,this An actual object that is a virtual reference
att = null;
}
Here's a call to Cleaner Of create Method , And the background thread will also monitor the virtual referenced objects , If the actual object of the virtual reference ( Here is DirectByteBuffer) After being recycled , Will call Cleaner Of clean Method , To clear the memory occupied in direct memory
public void clean() {
if (remove(this)) {
try {
this.thunk.run(); // call run Method
} catch (final Throwable var2) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
if (System.err != null) {
(new Error("Cleaner terminated abnormally", var2)).printStackTrace();
}
System.exit(1);
return null;
}
});
}
Of the corresponding object run Method
public void run() {
if (address == 0) {
// Paranoia
return;
}
unsafe.freeMemory(address); // Free up memory occupied in direct memory
address = 0;
Bits.unreserveMemory(size, capacity);
}
Summary of the recycling mechanism of direct memory
- Used Unsafe Class to complete the allocation and recycling of direct memory , Recycling requires active invocation freeMemory Method
- ByteBuffer The implementation of uses Cleaner( Virtual reference ) To detect ByteBuffer. once ByteBuffer Be recycled , Then by ReferenceHandler To call Cleaner Of clean Method call freeMemory To free up memory
边栏推荐
- Development: how to install offline MySQL in Linux system?
- Tubes响应性数据系统的设计与原理
- Flutter custom component
- Splitting.js文本标题缓慢加载js特效
- Booking UI effect implemented by svg
- 联想“双平台”运维解决方案 助力智慧医疗行业智慧管理能力全面提升
- Parker Parker sensor p8s-grflx
- Inventory in the first half of 2022: summary of major updates and technical points of 20+ mainstream databases
- 5G商用三年,未来创新何去何从?
- MSF后渗透总结
猜你喜欢

Rainbow Brackets 插件的快捷键

What will be the game changes brought about by the meta universe?

4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
![[zero basic IOT pwn] environment construction](/img/3b/a0689a1570fcc40bb9a5a4e9cdc63c.png)
[zero basic IOT pwn] environment construction

Cloud practice of key business migration of Internet of things by well-known Internet housing rental service companies

Shortcut keys for the rainbow brackets plug-in

Login box tricks

Radio and television 5g officially set sail, attracting attention on how to apply the golden band

新技能:通过代码缓存加速 Node.js 的启动

Vue3 reactive database
随机推荐
Ardunio esp32 obtains real-time temperature and humidity in mqtt protocol (DH11)
中基协:推荐使用电子合同
TCP session hijacking based on hunt1.5
每日面试1题-蓝队基础面试题-应急响应(1)应急响应基本思路流程+Windows入侵排查思路
TFTP下载kernel,nfs挂载文件系统
分享 5 大常用的特征选择方法,机器学习入门必看!!!
基于SSH的客户关系CRM管理系统
China Infrastructure Development Association: electronic contract is recommended
Lenovo's "dual platform" operation and maintenance solution helps to comprehensively improve the intelligent management ability of the intelligent medical industry
Map集合
知名互联网房屋租赁服务公司物联网关键业务迁移上云实践
新技能:通过代码缓存加速 Node.js 的启动
后渗透之文件系统+上传下载文件
Acwing game 57
Daily interview 1 question - basic interview question of blue team - emergency response (1) basic idea process of emergency response +windows intrusion screening idea
MySQL之零碎知识点
Apache parsing vulnerability (cve-2017-15715)_ Vulnerability recurrence
MSF后渗透总结
Network: principle and practice of server network card group technology
Course design for the end of the semester: product sales management system based on SSM