类对象的内存分配 [英] Memory allocation of class objects

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

问题描述

我一直在问这个问题采访。请帮我找到它的答案。

假设你有一个Employee类。有2个变量的原因 - 1.字符串名称2.智力年龄

现在,员工EMP =新员工();

现在问的问题是:


  1. 其中,存储在内存中即堆栈或堆和对象如何EMP?

  2. 其中,存储在内存中,以及如何姓名和年龄变量?

  3. 是什么在这个声明中的每个单词做即哪些呢员工do..then emp..then = ..然后..新员工,然后..然后()..然后;

  4. 是什么上面的语句和员工EMP之间的区别; ?告诉在内存分配方面?

请与您的宝贵意见回复。


解决方案

  

      
  1. 其中,存储在内存中即堆栈或堆和对象如何EMP?

  2.   

现在的问题是不好措辞。 EMP 不是的对象的; EMP 变量的其中的包含引用的一个对象。

让我们改写了一个问题:


  

1(a)如果是由对象 EMP 提到存储在内存中?


的目的是通过可变称为 EMP 被存储在长期储存,也被称为堆


  

1(B) EMP 是一个变量,因此重新presents的存储位置。哪里是在内存中的存储位置?


问题没有给予足够的信息说。变量 EMP 可能是一个静态字段,实例字段或局部变量。 (它不能成为一个正式的参数,因为分号)。如果本地,它也可能是一个lambda的封闭过外变量或局部迭代器块,或者异步方法。所有的这些将改变变量的存储是否在短期或长期存储。如果它是在短期存储它可能是在栈上,或者它可以是一个寄存器。


  

2在哪里名称并存储在内存年龄变量又如何?


由于它们是一类的领域,具有这些变量相关联的存储位置是总是在长期堆

由于名称的类型为字符串,它指的是东西 - 一个字符串 - 也是在堆上。 (或者,变量可为空,在这种情况下,它不是指任何东西。)


  

3是什么在这个声明中的每个单词做即哪些呢员工do..then emp..then = ..然后..新员工,然后..然后()..然后;


问题是非常严重的措辞。首先,这些都不是字,这些都是令牌。 (和()终止的两个的标记。)其次,它是完全不清楚的问题是指做什么。因此,让我们问一个不同的问题:


  

3,详细的操作进行描述时,这一宣言执行在运行时。


我们不能与任何precision说,因为没有在问题的足够信息。这个问题说,这是一个语句,所以它不是一个字段声明。让我们假设为简单起见,这不是在迭代器块或异步方法,并且该本地没有任何匿名函数的外变量


  • 首先,短期存储被分配给变量;它很可能是enregistered;如果不是,这将是在堆栈上。它被分配一个空引用。


  • 二,内存分配器被要求出示空的内存员工的堆一个实例。它这样做,生产到内存的引用。


  • 第三,如果这是我们见过的员工和员工都有一个静态构造函数,静态构造函数运行的第一次。


  • 四,静态构造函数完成后,员工的字段初始运行。


  • 五,员工的基类的构造函数运行。这可能会导致其他的静态构造函数来执行。


  • 六,员工的构造函数体中运行。


  • 第七,构造完成并引用到现在已经初始化的对象被复制到它的存储。


这一切当然都是假设,没有什么前进的道路上抛出异常。


  

4是什么上面的语句和员工EMP之间的区别; ?告诉在内存分配方面。


问题不包含足够的信息给出一个准确的答案。如果局部变量从未则使用的编译器是免费优化它拿走。如果不优化它拿走,然后存储在 EMP 被分配了短期池,初始化为null,并且从未使用过。

I have been asked this question in interview. Please help me find its answer.

Suppose you have a class Employee. There are 2 variables in it - 1. String Name 2. Int Age

Now, Employee emp = new Employee();

Now the Questions asked are :

  1. Where the object emp stored in memory i.e in stack or heap and how ?
  2. Where the name and age variables stored in memory and how?
  3. What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;
  4. What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.?

Please reply with your valuable comments.

解决方案

  1. Where the object emp stored in memory i.e in stack or heap and how ?

The question is poorly worded. emp is not an object; emp is a variable which contains a reference to an object.

So let's reword the question:

1 (a) Where is the object referred to by emp stored in memory?

The object referred to by variable emp is stored in long-term storage, also known as "the heap".

1 (b) emp is a variable and therefore represents a storage location. Where is that storage location in memory?

The question does not give enough information to say. Variable emp could be a static field, instance field, or local variable. (It cannot be a formal parameter because of the semicolon.) If a local, it could also be a closed-over outer variable of a lambda, or a local of an iterator block, or of an async method. All of these would change whether the variable's storage is in short-term or long-term storage. If it is in short-term storage it could be on the stack or it could be a register.

2 Where are the name and age variables stored in memory and how?

Since they are fields of a class, the storage locations associated with these variables are always on the long-term heap.

Since name is of type string, the thing that it refers to -- a string -- is also on the heap. (Or, the variable could be null, in which case it doesn't refer to anything.)

3 What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ;

The question is extremely badly worded. First off, those are not "words", those are "tokens". (And () is two tokens.) Second, it is completely unclear what the question means by "do". So let's ask a different question:

3 Describe in detail the operations performed when this declaration executes at runtime.

We cannot say with any precision because there is not enough information in the question. The question says that it is a statement, so it is not a field declaration. Let's assume for simplicity's sake that it is not in an iterator block or async method, and that the local is not an outer variable of any anonymous function.

  • First, short-term storage is allocated for the variable; it is likely to be enregistered; if not, it will be on the stack. It is assigned a null reference.

  • Second, the memory allocator is asked to produce empty memory for an instance of Employee on the heap. It does so and produces a reference to that memory.

  • Third, if this is the first time we've seen Employee and Employee has a static constructor, the static constructor runs.

  • Fourth, after the static ctor completes, Employee's field initializers run.

  • Fifth, Employee's base class constructor runs. This might cause other static constructors to execute.

  • Sixth, Employee's constructor body runs.

  • Seventh, the constructor completes and the reference to the now-initialized object is copied into its storage.

All this of course assumes that nothing along the way threw an exception.

4 What is the difference between the above statement and Employee emp; ? Tell in terms of memory allocation.

The question does not contain enough information to give an accurate answer. If the local variable is never used then the compiler is free to optimize it away. If it does not optimize it away then the storage for emp is allocated off the short-term pool, initialized to null, and never used.

这篇关于类对象的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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