java内存管理总结

编译好的java程序需要运行在jvm中。

程序,无论是代码还是数据,都需要存储在内存中。JVM为java提供并管理所需要的内存空间。

JVM内存分为堆、栈、方法区。

对象存储在堆中。

This lives in the general random-access memory(RAM)area,but has direct support from the processor via its stack pointer.The stack pointer is moved down to create new memory and moved up to release that memory.This is an extremely fast and efficient way to allocate storage,second only to registers.The Java system must know,while it is creating the program,the exact lifetime of all the items that are sorted on the stack.This constraint places limits on the flexibility of your programs,so while some Java storage exits on the stack-in particular,object references-Java objects themselves are not placed on the stack.

                             ---<Thinking in java>

栈用于存放方法中的局部变量

This is a general-purpose pool of memory(also in the RAM area) where all Java objects live.The nice thing about the heap is that,unlike the stack,the compiler doesn't need to know how long that storage must stay on the heap.Thus,there's a great deal of flexibility in using storage on the heap.Whenever you need an object,you simply write the code to create it by using new ,and the storage is allocated on the heap when that code is executed.Of course there's a price you pay for this flexibility:It may take more time to allocate and clean up heap storage than stack storage (if you even could create objects on the stack in Java,as you can in C++)

                             ---<Thinking in java>

方法区用于存放类的信息。Java程序运行时首先通过类加载器载入类文件的字节码信息,经过解析后将其装入方法区。类的各种信息(包括方法)都在方法区存储。

原文地址:https://www.cnblogs.com/rixiang/p/5479413.html