##内存泄露java代码,实验对象 byte数组
import java.util.ArrayList;
import java.util.List;public class OutOfMemoryTest {public static void main(String[] args) {List<byte[]> bytes = new ArrayList<>();while(true) {byte[] b = new byte[1024 * 1024 * 10];bytes.add(b);}}}
##gdb掉用栈
Thread 2 "java" hit Breakpoint 1, MemAllocator::Allocation::check_out_of_memory (this=0x7ffff7bfe5a0) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/gc/shared/memAllocator.cpp:126
126 if (!_thread->in_retryable_allocation()) {
(gdb) bt
#0 MemAllocator::Allocation::check_out_of_memory (this=0x7ffff7bfe5a0) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/gc/shared/memAllocator.cpp:126
#1 0x00007ffff677afee in MemAllocator::Allocation::~Allocation (this=0x7ffff7bfe5a0, __in_chrg=<optimized out>)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/gc/shared/memAllocator.cpp:84
#2 0x00007ffff677a0ab in MemAllocator::allocate (this=0x7ffff7bfe620) at /home/yym/openjdk17/jdk17-master/src/hotspot/share/gc/shared/memAllocator.cpp:373
#3 0x00007ffff6019669 in CollectedHeap::array_allocate (this=0x7ffff00453d0, klass=0x100040820, size=1310722, length=10485760, do_zero=true, __the_thread__=0x7ffff0028920)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/gc/shared/collectedHeap.inline.hpp:41
#4 0x00007ffff6b8bcae in TypeArrayKlass::allocate_common (this=0x100040820, length=10485760, do_zero=true, __the_thread__=0x7ffff0028920)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/oops/typeArrayKlass.cpp:93
#5 0x00007ffff60b06c8 in TypeArrayKlass::allocate (this=0x100040820, length=10485760, __the_thread__=0x7ffff0028920)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/oops/typeArrayKlass.hpp:68
#6 0x00007ffff685bfc1 in oopFactory::new_typeArray (type=T_BYTE, length=10485760, __the_thread__=0x7ffff0028920)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/memory/oopFactory.cpp:93
#7 0x00007ffff63685ea in InterpreterRuntime::newarray (current=0x7ffff0028920, type=T_BYTE, size=10485760)at /home/yym/openjdk17/jdk17-master/src/hotspot/share/interpreter/interpreterRuntime.cpp:259
#8 0x00007fffe1023c72 in ?? ()
#9 0x00007ffff7ab2c00 in TemplateInterpreter::_active_table () from /home/yym/openjdk17/jdk17-master/build/linux-x86_64-server-slowdebug/jdk/lib/server/libjvm.so
#10 0x00007fffe1023bf1 in ?? ()
#11 0x00007ffff7bfe7a0 in ?? ()
#12 0x00007fffd9c0f272 in ?? ()
#13 0x00007ffff7bfe808 in ?? ()
#14 0x00007fffd9c0f300 in ?? ()
#15 0x0000000000000000 in ?? ()
##InterpreterRuntime::newarray JVM字节码指令new数组,CollectedHeap::array_allocate分配数组空间。MemAllocator::allocate内存分配
##~Allocation()析构函数,调用check_out_of_memory() 查看*_obj_ptr是否为空,为空抛出内存溢出
oop obj() const { return *_obj_ptr; }
~Allocation() {if (!check_out_of_memory()) {verify_after();notify_allocation();}}bool MemAllocator::Allocation::check_out_of_memory() {JavaThread* THREAD = _thread; // For exception macros.assert(!HAS_PENDING_EXCEPTION, "Unexpected exception, will result in uninitialized storage");if (obj() != NULL) {return false;}const char* message = _overhead_limit_exceeded ? "GC overhead limit exceeded" : "Java heap space";std::cout << "@@@@yym%%%%" << message << std::endl;if (!_thread->in_retryable_allocation()) {// -XX:+HeapDumpOnOutOfMemoryError and -XX:OnOutOfMemoryError supportreport_java_out_of_memory(message);if (JvmtiExport::should_post_resource_exhausted()) {JvmtiExport::post_resource_exhausted(JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_JAVA_HEAP,message);}oop exception = _overhead_limit_exceeded ?Universe::out_of_memory_error_gc_overhead_limit() :Universe::out_of_memory_error_java_heap();THROW_OOP_(exception, true);} else {THROW_OOP_(Universe::out_of_memory_error_retry(), true);}
}