为什么Java使用堆进行内存分配? [英] Why does Java uses heap for memory allocation?

查看:335
本文介绍了为什么Java使用堆进行内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在一本java书中读到这句话,说java中的Objects驻留在堆上。
是否使用了堆,因为它是快速存储数据和检索数据的最佳方式?

I just read this statement in a java book saying Objects in java reside on a heap. Is a heap used because it is the best way to store data and retrieve data fast ?

我只对数据结构是初学者有所了解。我的意思是为什么不堆叠或其他东西?

I only have an idea about data structures being a beginner. I mean why not stack or something else ?

推荐答案

堆栈的问题是你只能删除最新的东西你添加。这适用于局部变量,因为它们在您进入和退出函数时来来往往,但对于生命周期不遵循单个函数的任意数据则不太好。内存堆允许您随意添加和删除数据。

The problem with a stack is that you can only remove the most recent thing you added. This works fine for local variables, since they come and go as you enter and exit functions, but not so well for arbitrary data who's lifecycle doesn't follow that of individual functions. The memory heap allows you to add and remove data at will.

这篇关于为什么Java使用堆进行内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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