Heap memory
Native memory
Java 本地内存
Unsafe allocateMemory、reallocateMemory、freeMemory、setMemory、copyMemory。
public class Block { private static Unsafe unsafe; { try { unsafe = getUnsafe(); } catch (Throwable e) { throw new Error("fatal: " + e.getMessage(), e); } } private int size; private long p; private int offset; public static Block newInstance(int size) { return new Block(size); } /** * 2 * 1024 * 1024 * * @param size * @throws Throwable */ public Block(int size) { p = unsafe.allocateMemory(size); this.size = size; offset = 0; } public static Unsafe getUnsafe() throws Throwable { Class<?> unsafeClass = Unsafe.class; for (Field f : unsafeClass.getDeclaredFields()) { if ("theUnsafe".equals(f.getName())) { f.setAccessible(true); return (Unsafe) f.get(null); } } throw new IllegalAccessException("no declared field: theUnsafe"); } public synchronized byte[] get() { int nbytes = offset; byte[] bytes = new byte[nbytes]; for (int i = 0; i < nbytes; i++) { bytes[i] = unsafe.getByte(p + i); } return bytes; } public synchronized void put(byte[] bytes) { int retainBytes = size - offset; int nbytes = retainBytes > bytes.length ? bytes.length : retainBytes; for (int i = 0; i < nbytes; i++) { unsafe.putByte(p + offset++, bytes[i]); } } public synchronized boolean available() { return p != -1; } public synchronized void deallocate() { unsafe.freeMemory(p); p = -1; offset = -1; } }
@Test public void test() throws Throwable { Block block = Block.newInstance(2 * 1024 * 1024); System.out.println(block.available()); block.put("greeting".getBytes()); byte[] bytes = block.get(); String s = new String(bytes); System.out.println(s); block.deallocate(); System.out.println(block.available()); block.put("helloworld".getBytes()); }
测试代码输出:
true
greeting
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5584, tid=0x000012a0
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5584.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
greeting
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5584, tid=0x000012a0
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5584.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
@Test public void test1() throws Throwable { Block block = Block.newInstance(5); System.out.println(block.available()); block.put("greeting".getBytes()); byte[] bytes = block.get(); String s = new String(bytes); System.out.println(s); block.deallocate(); System.out.println(block.available()); block.put("helloworld".getBytes()); }
测试代码输出
true
greet
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5412, tid=0x00001074
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5412.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
greet
false
#
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d37c4a6, pid=5412, tid=0x00001074
#
# JRE version: Java(TM) SE Runtime Environment (8.0_101-b13) (build 1.8.0_101-b13)
# Java VM: Java HotSpot(TM) Client VM (25.101-b13 mixed mode, sharing windows-x86 )
# Problematic frame:
# V [jvm.dll+0x13c4a6]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# D:\home\admin\workstation\java\javatest\hs_err_pid5412.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
Direct memory
相关推荐
【情况四】:`java.lang.OutOfMemoryError: Direct buffer memory` 这种情况发生在NIO(非阻塞I/O)中,因为直接缓冲区内存分配失败。可以通过设置`-XX:MaxDirectMemorySize`参数来限制直接内存大小,如`-XX:...
1. 堆内存(Heap Memory):这是Java对象的主要存储区域,所有的类实例和数组都在堆中分配内存。堆内存是JVM中最大的一块内存,由所有线程共享。Java垃圾回收机制主要负责堆内存的管理,通过标记-清除、复制、标记-...
Java虚拟机(JVM)将内存分为几个区域:堆(Heap)、栈(Stack)、方法区(Method Area)、程序计数器(PC Register)和本地方法栈(Native Method Stack)。其中,堆和栈是最常引发内存溢出的区域。 1. **堆内存...
本机内存是指除了堆和栈之外,JVM使用的其他内存区域,如方法区(Method Area)、程序计数器(Program Counter Register)、本地方法栈(Native Method Stack)和直接内存(Direct Memory)。其中,直接内存是在JDK 1.4中新...
【Java虚拟机内存溢出分析】:当遇到`java.lang.OutOfMemoryError: unable to create new native thread`错误时,这通常表示系统无法为新的Java线程分配足够的内存,即操作系统层面的资源耗尽,而非Java堆内存不足。...
7. **直接内存(Direct Memory)**:不属于JVM规范定义的内存区域,但使用频繁,特别是在NIO(New Input/Output)中。直接内存是在堆外分配的,避免了Java堆和本地堆之间的数据复制,从而提高了性能。不过,直接内存...
除了堆和栈之外,Java应用程序还可以使用本机内存(Native Memory)。本机内存是指Java虚拟机以外的内存区域,主要用于存储直接缓冲区(Direct Buffers)等对象。直接缓冲区是Java NIO框架的一部分,它可以绕过JVM的...
7. **直接内存(Direct Memory)** - 非JVM规范定义,但与JVM性能密切相关。使用Native分配的内存,可绕过JVM内存分配,提高性能,但可能导致内存溢出。 了解这些内存区域后,我们还需要关注以下关键概念: - **...
7. **直接内存(DirectMemory)**:非JVM规范定义的内存区域,但可以通过NIO等技术直接分配堆外内存。过度使用可能导致OutOfMemoryError。 **对象访问机制**: Java对象访问涉及到栈、堆和方法区。当创建一个对象,...
直接内存(Direct Memory)通常通过Java NIO类与本地代码交互时使用,它不是JVM直接管理的堆内存,但是它可能导致OutOfMemoryError。 10. Java Native Interface(JNI):JNI是Java提供的一种标准编程接口,用于...
1. **直接内存(Direct Memory)**:Java的NIO(New Input/Output)库支持直接内存分配,数据可以直接在物理内存中读写,减少了Java对象之间的内存拷贝,提升了性能。使用`java.nio.DirectByteBuffer`类可以创建直接...
9. Java.lang.OutOfMemoryError: Failed to Allocate an Object in Native Memory 错误 * 原因分析:无法在 native 内存中分配对象 * 解决方案:增加 native 内存的大小、检查 native 内存的使用情况、优化 native ...
直接内存(Direct Memory)是一块物理内存,它不是JVM虚拟机的运行时数据区的一部分,但是它经常被用来存储大型数据。直接内存可以通过Unsafe类或Netty框架来操作。 OutOfMemoryError OutOfMemoryError是一种常见...