- 浏览: 427150 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
Glogo:
楼主您好,我在试验您的代码的时候发现新开的三个子线程并没有一直 ...
java 高并发 ReentrantLock -- 可重入的锁 -
univasity:
最近发觉也被限速了,投诉一下就好一会~~ 看来明天又要和电信M ...
ADSL上网速度慢 都是帐号限速惹的祸 -
liuyuanhui0301:
java 高并发 ReentrantLock -- 可重入的锁 -
dang_java:
呵.很好的说明文档.
JXTA技术与应用发展 -
helloqidi:
谢谢,学习了
SQL中exists和in的区别
One of the biggest strength of the Java Platform is the implementation of an automatic memory management in the Java Virtual Maschine. Everybody who has programmed with languages like C/C++ knows about the problems of managing memory allocation and deallocation in the code. With Java problems like deallocating memory too early (corrupted pointer) or too late (memory leak) cannot occur by specification. The question is: Why am I writing these blog entries?
The problem is that even with an implicit memory management integrated, Java cannot prevent application of being corrupt in sense of memory management, even it is not allowed to explicitly allocate memory in Java. The result of such wrongly programmed code normally is an exception of type: java.lang.OutOfMemoryError.
This part of the blog series about Java OutOfMemoryError, will introduce the Java Memory Architecture in detail and shows in which memory areas an java.lang.OutOfMemoryError can occur. Details about the cause of these errors and the tools and methods for analysis will be covered in later entries.
Lets start by looking at the Javadoc of java.lang.OutOfMemoryError:
Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
This description copied from the actual Java API Documentation (Version 6) is not only very short, but in my point of view incomplete and therefore wrong. This description does only cover the heap of the JVM – as we will learn later, OutOfMemoryError can also occur in different areas of the JVMs memory. These errors are not mentioned in the Javadoc, but you can see them every day in real world applications.
The architecture of Java’s memory management is defined for all JVM implementations in the Java Virtual Maschine Specification. Chapters 3.5 Runtime Data Areas and 3.6 Frames are the most relevant for memory architecture. For a better understanding, I’ve drawn the following picture as a summary of the chapters on memory areas in a JVM.
We can basically distinguish memory areas that are available for all threads in a JVM and those memory areas that are exclusively accessible from only one thread. The two areas that are available from all threads are the Method Area and the Heap.
The method area is responsible for storing class information. The Class-Loader will load the bytecode of a class and will pass it to the JVM. The JVM will generate an internal class representation of the bytecode and store it in the method area. The internal representation of a class will have the following data areas:
-
Runtime Constant Pool
-
Method Code
The implementation (code) of all methods of this class including constructors etc.
-
Attributes
A list of all named attributes of this class.
-
Fields
Values of all fields of this class as references to the Runtime Constant Pool.
The method area can be part of the heap and will be created at runtime. The size of the method area can be static or dynamic and it does not have to provide a Garbage Collector.
The second memory area that is available for all threads inside the JVM is the Heap. The Java heap manages instances of classes (objects) and arrays at runtime. The heap will be created at JVM startup and the size can be static or dynamic. The JVM specification mandates a Garbage Collection mechanism for reclaiming the memory of an object on the Java heap. The implementation of the Garbage Collector is not specified, but it is not allowed to provide the programmer with an explicit mechanism for deallocating the memory of an object.
Lets have a look at the Sun HotSpot implementation as an example:
The heap is devided into two generations: The Young Generation and the Tenured Generation. The details of this “generational heap” are not relevant in the context of Java OutOfMemoryError as the design is driven by optimizations of the Garbage Collection algorithm. The method area is implemented as a separated part: The Permanent Generation. All details about configuration and monitoring of these generations will be covered in the third part of this series: “JVM Monitoring and Configuration”.
This example of the Sun HotSpot JVM memory architecure shows that the JVM specification defines how the memory inside a JVM is organized in general, but leaves enough room for implementation specific optimizations.
In addition to the heap and method area, that are available for all threads of a JVM, every thread also has exclusivly access to memory that is created for each thread:
-
PC Register
-
Java Virtual Maschine Stack
-
Native Methode Stack
Until now you should have get an overview of the Java Memory Model including its different memory areas – this is essential, because now we will take a closer look at our java.lang.OutOfMemoryError. As mentioned before the Javadoc of this exception is not very meaningful, but the Java Virtual Maschine specification defines exactly when and where Java OutOfMemoryError can occur. The difficulty is that theses errors can occur in every memory area I’ve described before. Let’s have a look at the Sun HotSpot JVM and its concrete implementation of OutOfMemoryError errors.
In the heap we get an OutOfMemoryError, if the garbage collector cannot reclaim enough memory for a new object. In such situation the Sun HotSpot JVM shows this error message:
Exception in thread "main": java.lang.OutOfMemoryError: Java heap space
A alternative for this is
Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit
if the application tries to create an array on the heap that is bigger than the total heap size.
If there is not enough memory in the method area for creating a new class, the Sun HotSpot implementation gets an error in the permanent generation:
Exception in thread "main": java.lang.OutOfMemoryError: PermGen space
Both kinds of OutOfMemoryError occur very often in real life and the reasons for them are very different and will be covered in later blog entries.
OutOfMemory errors in thread exclusive memory areas occur less frequently and are identified by the following error messages in the Sun HotSpot JVM:
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
Exception in thread "main": java.lang.OutOfMemoryError: <reason> <stacktrace> (Native method)
The first error is thrown if there are too many threads in the JVM and there is not enough memory left to create a new thread. I’ve seen this because the memory limits of a process have been reached (especially in 32bit operating systems, e.g. on Windows 32bit it is 2GB) or the maximum number of file handles for the user that executes the java process has been reached. The second error message indicates that a memory allocation error on a native stack (JNI method call) has occured.
It is also interesting that a memory allocation error on the JVM stack (too many frames on the stack) does not throw an Java OutOfMemory error but as the JVM specification mandates: java.lang.StackOverflowError.
The last variant of the OutOfMemoryError that I know of is
Exception in thread "main": java.lang.OutOfMemoryError: request <size> bytes for <reason>. Out of swap space?
This error is thrown if there is not enough memory left on the operating system level – which is normally true if other processes are using all of the available memory or the swap space is configured too small.
This first blog entry of the Java OutOfMemoryError series covered the basics of the Java Memory Architecture. In my point of view it is essential to know the different memory areas of the JVM and its functions if you want to understand why a java.lang.OutOfMemoryError occured in your application. I hope that I have made clear that there can be many variations of this error with totally different possible causes. There are a lot of open questions about when and why theses errors occur and how we can monitor and analyze memory problems in our applications. This is exactly what the next episodes ot this Java OutOfMemoryError series will cover.
发表评论
-
JVM 深入笔记
2012-04-12 20:36 1000JVM 深入笔记(1)内存区域是如何划分的? 一个超短的前言 ... -
JVM启动参数
2011-06-10 16:27 964java [jvmargs] class [arguments ... -
JVM指令集 和 一个异常例子
2011-05-19 16:14 1018指令码 助记符 说明 0x00 nop ... -
The Top Java Memory Problems – Part 1
2011-05-18 11:01 904转载:http://blog.dynatrace.com/20 ... -
BTrace使用简介
2011-04-19 13:51 777该文转载自:http://rdc.taobao.com/tea ... -
大方法的执行性能与调优过程小记
2011-04-18 16:20 804该文章转载自:http://rdc.taobao.com/te ... -
十个最好的Java性能故障排除工具
2011-04-18 16:02 924推荐十个最好的Java性能 ... -
两个OOM Cases排查过程的分享
2011-04-18 15:39 929分享一下两个OOM Cases的查找过程,一个应用是Nativ ... -
Monitoring Java garbage collection with jstat
2011-04-18 15:28 858The original article is at : ht ... -
理解Heap Profling名词-Shallow和Retained Sizes
2011-04-18 14:58 974所有包含Heap Profling功能的工具(MAT, You ... -
Java深度历险(四)——Java垃圾回收机制与引用类型
2011-04-01 08:18 980Java语言的一个重要特性 ... -
Thread Dump 和Java应用诊断
2011-03-30 08:08 1130Thread Dump 和Java应用诊断 ... -
Memory Analysis Part 1 – Obtaining a Java Heapdump
2011-03-14 16:06 1119For troubleshooting Java memory ... -
Java Memory Leaks et al. (2. Act)
2011-03-14 15:37 925The first act of this blog-seri ... -
Erlang memory architecture vs Java memory architecture
2011-03-14 15:36 909I read a really, really interes ... -
Permanent generation
2011-03-14 14:53 1270版权声明:转载时请以 ... -
JVM内存模型以及垃圾回收
2011-03-13 09:38 983内存由 Perm 和 Heap 组成. 其中 Heap ... -
Java虚拟机垃圾回收机制
2011-03-13 09:31 825一、相关概念 基本 ... -
JVM结构
2011-03-10 14:43 889类文件格式 JVM使用一 ... -
OOM与JVM(转)
2009-03-24 15:49 966OOM与JVM(转) 2008年08月08日 星期五 15: ...
相关推荐
j-jtp02244-Java.theory.and.practice-Fixing.the.Java.Memory.Model.Part1/2.pdf Java Memory Model Pragmatics (transcript).pdf 读JSR133笔记 - 十年磨一剑 - ITeye博客.pdf The JSR-133 Cookbook.pdf jsr-133-...
《深入理解.NET内存管理与SciTech.NET.Memory.Profiler.v4.0.114工具的运用》 在软件开发过程中,尤其是使用.NET框架时,内存管理是至关重要的一个环节。内存泄漏不仅会消耗宝贵的系统资源,还可能导致应用程序性能...
《Java Memory Model》是一篇关于Java内存模型的重要论文,由Jeremy Manson、William Pugh以及Sarita V. Adve共同撰写。该论文主要介绍了Java 5.0版本中更新后的Java内存模型(JMM),并详细阐述了它在多线程程序中...
advances in memory architecture design, discusses the benefits of using at various levels of memory hierarchy, and also reviews the mitigation techniques to overcome the limitations of applying such ...
1. 启动Profiler:运行SciTech.NET.Memory.Profiler,选择要分析的.NET进程。 2. 捕获快照:在关键操作前后捕获内存快照,通过比较两个快照之间的差异,可以发现对象数量和大小的变化,从而找出可能导致内存泄漏的...
Appendix A: Architecture Specifics ..... . . . . 1117 Appendix B:Working with the Source Code .... . . . . 1141 Appendix C: Notes on C ...... . . . . . . . 1175 Appendix D: System Startup ...... . . ...
C语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY.HC语言头文件 MEMORY....
.NET Memory.Profiler.v4.0完美破解版,检测内存,定位未释放资源。非常好用。
elasticsearch启动后自动关闭:max virtual memory areas vm.max_map_count [65530] is too low, increase to at… elasticsearch 我遇到的问题是用docker 启动elasticsearch后会自动关闭,具体关闭时间点没注意,...
本文档提供了Java HotSpot虚拟机(JVM)中内存管理的广泛概述,特别是在Sun公司的Java 2平台标准版(J2SE)5.0版本的发布中。文档描述了可供使用的垃圾收集器(Garbage Collectors),给出了关于如何选择和配置收集...
Section C.1. Using ifconfig to Manage Network Devices 572 Section C.2. Using ping to Test the Reachability 575 Section C.3. Using netstat to View the Network State 576 Section C.4. Using ...
1. 使用 Java 提供的垃圾回收机制:Java 提供了多种垃圾回收机制,例如 generational garbage collection、parallel garbage collection 等。 2. 使用外部工具:例如使用 Eclipse 的 Memory Analyzer Tool (MAT) ...
lucene-memory-3.0.2.jar,lucene高亮显示中不可少的jar包lucene-memory-*.jar
1. **Java内存模型**: Java内存模型分为几个区域:堆(Heap)、方法区(Method Area)、栈(Stack)、程序计数器(PC Register)和本地方法栈(Native Method Stack)。了解这些区域的作用是调优的基础,例如,堆...
lucene-memory-3.0.1.jar
1. **调整JVM启动参数**:你可以通过设置JVM的启动参数 `-Xms` 和 `-Xmx` 来控制Java进程的初始堆大小和最大堆大小。例如,可以将它们分别设置为256MB和1GB,命令如下: ``` java -Xms256m -Xmx1g -jar your-...
lucene-memory-3.3.0.jar 包下载
配置jvm参数-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/mat/ java_pid1089.hprof 手工直接导,PID为进程号 jmap -dump:live,format=b,file= java_pid1089.hprof PID 然后,在linux解压后执行以下命令...
在深度学习领域,CUDA(Compute Unified Device Architecture)是NVIDIA公司推出的一种编程接口,它允许开发者利用GPU的强大计算能力进行并行处理。然而,在训练深度学习模型时,我们经常遇到“CUDA error: out of ...
假如你机器的内存不大,改大该参数的值,会导致MemoryAnalyzer启动时,报错:Failed to create the Java Virtual Machine。 2.当你导出的dump文件的大小大于你配置的1024m(说明1中,提到的配置:-vmargs– Xmx1024m...