It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
已关闭10年。
说明Java编写程序的内存分配。
当
已关闭10年。
说明Java编写程序的内存分配。
最佳答案
通常,在Java "heap"上分配内存是作为创建对象的自动结果。它是自动管理的;一旦不再引用对象,最终垃圾收集器会将它曾经占用的内存返回到可用池。有关更多信息,请访问java.sun.com网站,例如此overview of memory management。特别是因为它与Sun的JVM实现HotSpot有关,所以有一个PDF on it。
这与程序员直接控制分配和释放的语言(例如C)相反。在Java中,您只需将其交给环境即可:
void doSomething() {
NiftyObject joe;
joe = new NiftyObject(); // Allocation
// ...use joe for something...
// Done; no "free" call (or similar) required
}
当
joe
超出范围(函数返回;我在这里假设函数不返回joe
或将其存储在某处)时,joe
可以由垃圾收集器收集。实际上,这完全取决于实现和环境。关于java - Java中的内存分配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2220037/