请问内存分配和垃圾回收有什么区别?
据我所知,“垃圾收集”是内存管理的一种形式,它是一种自动回收未使用内存的方法。
但什么是“内存分配”以及与“垃圾收集”的概念区别?
I understand that 'Garbage Collection' is a form of memory management and that it's a way to automatically reclaim unused memory.
But what is 'memory allocation' and the conceptual difference from 'Garbage Collection'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
他们是截然相反的。所以是的,差别很大。
分配内存是申请内存空间来存储事物的过程。
垃圾收集(或释放内存)是将内存释放回可用内存池的过程。
当声明/初始化变量并超出范围时,许多较新的语言会在后台为您执行这两个步骤。
They are Polar opposites. So yeah, pretty big difference.
Allocating memory is the process of claiming a memory space to store things.
Garbage Collection (or freeing of memory) is the process of releasing that memory back to the pool of available memory.
Many newer languages perform both of these steps in the background for you when variables are declared/initialized, and fall out of scope.
内存分配是向系统请求一些内存以用于某些目的的行为。
垃圾收集是一个检查先前分配的某些内存是否不再真正使用(即不再可以从程序访问)以自动释放它的过程。
一个微妙的一点是,垃圾收集的目标实际上并不是“释放不再使用的对象”,而是模拟具有无限内存的机器,允许您继续分配内存而不关心释放它;因此,它不能替代其他类型资源的管理(例如文件句柄、数据库连接等)。
Memory allocation is the act of asking for some memory to the system to use it for something.
Garbage collection is a process to check if some memory that was previously allocated is no longer really in use (i.e. is no longer accessible from the program) to free it automatically.
A subtle point is that the objective of garbage collection is not actually "freeing objects that are no longer used", but to emulate a machine with infinite memory, allowing you to continue to allocate memory and not caring about deallocating it; for this reason, it's not a substitute for the management of other kind resources (e.g. file handles, database connections, ...).
一个简单的伪代码示例:
这将在堆上请求足够的新空间来存储 LinkedList 对象。
然而,当函数体结束时,
myList
就会消失,并且你不再知道这个LinkedList的存储位置(内存地址)。因此,绝对没有办法告诉系统释放该内存,并在以后再次提供给您使用。Java 垃圾收集器会自动为您执行此操作,但会牺牲一些性能,并且还会引入一点不确定性(您无法真正判断 GC 何时被调用)。
在
C++
中,没有本机垃圾收集器(还没有?)。但管理内存的正确方法是使用 smart_pointers(例如std::auto_ptr
(在 C++11 中已弃用)、std::shared_ptr
)等。A simple pseudo-code example:
This will request enough new space on the heap to store the LinkedList object.
However, when the function body is over,
myList
dissapears and you do not have anymore anyway of knowing where this LinkedList is stored (the memory address). Hence, there is absolutely no way to tell to the system to free that memory, and make it available to you again later.The Java Garbage Collector will do that for you automatically, in the cost of some performance, and with also introducing a little non-determinism (you cannot really tell when the GC will be called).
In
C++
there is no native garbage collector (yet?). But the correct way of managing memory is by the use of smart_pointers (eg.std::auto_ptr
(deprecated in C++11),std::shared_ptr
) etc etc.你想要一本书。您去图书馆索取您想要的书。图书馆会检查他们是否有这本书(在这种情况下他们有),你很乐意拿走它,并且知道你必须稍后归还它。
你回家,坐下来,读完这本书。第二天你把书还回图书馆,因为你已经看完了。
这是内存分配和垃圾收集的简单类比。计算机的内存是有限的,就像图书馆的书籍数量有限一样。当您想要分配内存时,您需要发出请求,如果计算机有足够的内存(库有足够的副本供您使用),那么您收到的就是一块内存。计算机需要内存来存储数据。
由于电脑的内存有限,你需要归还内存,否则你就会耗尽内存(就像如果没有人把书还给图书馆那么图书馆就什么都没有了,如果电脑运行的话,电脑会在你眼前爆炸并燃烧)内存不足...不是真的)。垃圾收集是检查先前分配的内存是否不再使用的术语,以便可以将其返回并重新用于其他目的。
You want a book. You go to the library and request the book you want. The library checks to see if they have the book (in which case they do) and you gladly take it and know you must return it later.
You go home, sit down, read the book and finish it. You return the book back to the library the next day because you are finished with it.
That is a simple analogy for memory allocation and garbage collection. Computers have limited memory, just like libraries have limited copies of books. When you want to allocate memory you need to make a request and if the computer has sufficient memory (the library has enough copies for you) then what you receive is a chunk of memory. Computers need memory for storing data.
Since computers have limited memory, you need to return the memory otherwise you will run out (just like if no one returned the books to the library then the library would have nothing, the computer will explode and burn furiously before your very eyes if it runs out of memory... not really). Garbage collection is the term for checking whether memory that has been previously allocated is no longer in use so it can be returned and reused for other purposes.
内存分配要求计算机提供一些内存,以便存储数据。例如,在 C++ 中:
告诉计算机分配足够的内存来存储一定数量的整数。
做同样事情的另一种方法是:
这里的区别在于,在第二个示例中,我们知道代码何时编写和编译,我们需要多少空间 - 它是 6 * 一个 int 的大小。这让我们可以进行静态内存分配(使用所谓的“堆栈”上的内存)。
在第一个示例中,我们不知道代码编译时需要多少空间,只有在程序运行时才知道,并且我们有 howManyIntsIWant 的值。这是动态内存分配,它在“堆”上获取内存。
现在,通过静态分配,我们不需要告诉计算机何时完成内存分配。这与堆栈的工作方式有关;简而言之,一旦我们离开创建静态数组的函数,内存就会立即被吞噬。
对于动态分配,这种情况不会发生,因此必须通过其他方式清理内存。在某些语言中,您必须编写代码来释放该内存,而在其他语言中,它是自动完成的。这就是垃圾收集——语言中内置的一些自动进程,它将扫描堆上所有动态分配的内存,找出哪些位没有被使用并释放它们(即将它们释放给其他进程和程序)。
所以:内存分配=为你的程序请求内存。垃圾收集=编程语言本身计算出哪些内存不再被使用并为您释放它。
Memory allocation asks the computer for some memory, in order to store data. For example, in C++:
tells the computer to allocate me enough memory to store some number of integers.
Another way of doing the same thing would be:
The difference here is that in the second example, we know when the code is written and compiled exactly how much space we need - it's 6 * the size of one int. This lets us do static memory allocation (which uses memory on what's called the "stack").
In the first example we don't know how much space is needed when the code is compiled, we only know it when the program is running and we have the value of howManyIntsIWant. This is dynamic memory allocation, which gets memory on the "heap".
Now, with static allocation we don't need to tell the computer when we're finished with the memory. This relates to how the stack works; the short version is that once we've left the function where we created that static array, the memory is swallowed up straight away.
With dynamic allocation, this doesn't happen so the memory has to be cleaned up some other way. In some languages, you have to write the code to deallocate this memory, in other it's done automatically. This is garbage collection - some automatic process built into the language that will sweep through all of the dynamically allocated memory on the heap, work out which bits aren't being used and deallocate them (i.e. free them up for other processes and programs).
So: memory allocation = asking for memory for your program. Garbage collection = where the programming language itself works out what memory isn't being used any more and deallocates it for you.