Java堆栈和堆内存管理 [英] Java stack and heap memory management

查看:171
本文介绍了Java堆栈和堆内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在以下程序中分配内存:

I want to know how the memory is being allocated in the following program:

public class MemoryClass {

    public static void main(final String[] args) {
        int i = 0;
        MemoryClass memoryClass = new MemoryClass();
        memoryClass.myMethod(memoryClass);
    }

    private void myMethod(final Object obj) {
        int i = 1;
        String s = "HelloWorld!";

    }

}

现在,如同就我的理解而言,下图描述了内存分配的发生方式:

Now, as far as my understanding goes, the following diagram describes how the memory allocation takes place:



在上图中,堆栈内存中的内存 obj s 实际上是引用它们放在堆内存中的实际对象


以下是我想到的一系列问题:


In the above diagram, memory,obj and s, which are in the stack memory, are actually the references to their "actual objects" which are placed inside the heap memory.
Here is the set of questions that come to my mind:


  1. s 存储?

  2. 我是否在 myMethod <中创建了 MemoryClass 的另一个对象/ code>,JVM会在堆栈内存中再次为相同的方法分配内存吗?

  3. JVM是否会释放分配给 myMethod的内存一旦执行完成,如果是这样,它将如何管理问题2中提到的情况(仅当JVM多次为同一方法分配内存时才适用)。

  4. 如果我只声明 s 并且没有初始化它,那么JVM仍会为 java的所有方法分配内存.lang.String 类,如果是,为什么?

  1. Where are the methods of s stored?
  2. Had I created another object of MemoryClass inside myMethod, would JVM allocate memory for the same methods again inside the stack memory?
  3. Would JVM free the memory allocated to myMethod as soon as it's execution is completed, if so, how would it manage the situation mentioned in question 2(only applicable if JVM allocates memory multiple times to the same method).
  4. What would have been the case, if I had only declared s and did not initialize it, would JVM still allocate memory to all the methods of java.lang.String class,if so,why?


推荐答案


存储方法在哪里?

Where are the methods of s stored?

它们存储在String类中宾语;它是在程序中首次引用String时由ClassLoader对象加载的对象。我读到这篇文章时存在的所有JVM实现都从未在类对象加载后释放内存。它在堆上。

They are stored in the String class object; it is an object loaded by a ClassLoader object when String is first referenced in the program. All the implementations of the JVM that existed when I read about this last never deallocated the memory for a class object once it was loaded. It's on the heap.


如果我在myMethod中创建了另一个MemoryClass对象,JVM会在堆栈内存中再次为相同的方法分配内存?

Had I created another object of MemoryClass inside myMethod, would JVM allocate memory for the same methods again inside the stack memory?

不,对象的方法和数据是分开保存的,特别是因为JVM永远不需要多个方法副本。 / p>

No, methods and data for objects is kept separately, specifically because the JVM never needs more than one copy of the methods.


JVM在执行完成后是否释放分配给myMethod的内存,如果是这样,它将如何管理问题2中提到的情况(仅当JVM多次为同一方法分配内存时才适用。)

Would JVM free the memory allocated to myMethod as soon as it's execution is completed, if so, how would it manage the situation mentioned in question 2(only applicable if JVM allocates memory multiple times to the same method).

否。 Java通常不会立即释放存储在堆上的东西。这会使事情运行得太慢。它只在垃圾收集器运行时释放内存,并且只有当它运行垃圾收集器的算法决定它是时间时它才会这样做。

No. Java doesn't generally "immediately free memory" of things stored on the heap. It would make things run too slowly. It only frees memory when the garbage collector runs, and it does that only when its algorithm for running the garbage collector decides it is time.


如果我只声明了s并且没有初始化它,JVM仍然会为java.lang.String类的所有方法分配内存,如果是这样,为什么呢?

What would have been the case, if I had only declared s and did not initialize it, would JVM still allocate memory to all the methods of java.lang.String class,if so,why?

这取决于我认为的JVM实现,也许还有编译器。如果声明一个变量并且从不使用它,那么编译器很可能(并且很常见)注意到它没有用处并且没有将它放入类文件中。如果它不在类文件中,它永远不会被引用,因此它及其方法不会被加载等。如果编译器无论如何都将它放入但是它从未被引用,那么ClassLoader将没有任何理由加载它,但我是否会加载或不加载我有点模糊。可能依赖于JVM实现;它是否加载了东西,因为有类的变量或只有它们被引用?有多少ClassLoader算法可以在一个4位数的PIN头上跳舞?

This depends on the JVM implementation, I think, and maybe the compiler. If you declare a variable and never use it, it is quite possible (and common) for the compiler to notice that there is no use for it and to not put it into the class file. If it isn't in the class file, it is never referenced, and therefore it and its methods are not loaded, etc. If the compiler puts it in anyway but it is never referenced, then the ClassLoader wouldn't have any reason to load it, but I'm a little vague on whether it would get loaded or not. Might depend on the JVM implementation; does it load things because there are variables of the class or only when they are referenced? How many ClassLoader algorithms can dance on the head of a 4-digit PIN?

我鼓励你阅读有关JVM和ClassLoaders等的内容。你会通过阅读它的工作原理来获得更多,而不是用你能想到的例子来解释它。

I encourage you to read about the JVM and ClassLoaders and such; you will gain so much more by reading an explanation of how it works rather than poking at it with examples you can think up.

这篇关于Java堆栈和堆内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆