除了内存之外,是否有编程语言支持自动管理资源的示例?
自动内存管理的思想得到了新编程语言的大力支持。 我感兴趣是否存在自动管理其他资源(如文件、网络套接字等)的概念?
The idea of automatic memory management has gained big support with new programming languages. I'm interested if concepts exists for automatic management of other resources like files, network-sockets etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于单线程应用程序,多种语言中都存在这样的模式:资源在代码块范围内可用,并在最后进行清理。 例如在 C++ 中使用 RAII 或 with-open-file (以及较新的受 Lisp 影响的语言中的等效项 - Dylan 中的相同,< a href="http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx" rel="nofollow noreferrer">C#, Python ,在 Ruby 中你可以将块传递给文件对象)。
我不知道有什么更适合现代垃圾收集大放异彩的多线程环境,除了将 RAII 和引用计数或 C++ 中的 auto_ptr 结合起来之外,这并不总是一个简单的组合。
自动资源管理和自动内存管理之间的一个重要区别是,内存管理通常可以承受不确定性,并且仅在进程需要时才回收,而资源通常在操作系统级别受到限制,因此应该尽快回收因为它不再被使用。 因此选择智能指针而不是垃圾收集作为管理实现。 有一个中间级别的资源 - GDI 对象、临时文件句柄、线程 - 应用程序希望限制其使用的总量,但不太关心将它们释放给其他进程 - 这些通常被池化,这可以让你获得一些资源走向自动化管理。
For single threaded applications, the pattern of a resource being available for the extent of a block of code, with clean-up at the end, exists in several languages. Examples are the use of RAII in C++, or with-open-file in Common Lisp (and equivalent in newer Lisp-influenced languages - the same in Dylan, C#, Python and in Ruby you can pass a block to a file object).
I'm not aware of anything better suited for the multithreaded environments where modern garbage collection shines, short of combining RAII and reference counting or auto_ptr in C++, which isn't always a trivial combination.
One important distinction between automatic management of resources and automatic memory management is that memory management can often afford to be non-deterministic and only reclaimed when the process requires it, whereas often a resource is limited at an OS level, so should be reclaimed as soon as it is no longer used. Hence the choice of smart pointers rather than garbage collection as the management implementation. There's an intermediate level of resource - GDI objects, temporary file handles, threads - where an application wants to limit the total it uses, but doesn't care so much about releasing them to other processes - these are often pooled, which gets you some way towards automatic management.
我们现在可以自动管理内存分配的原因之一是我们拥有如此多的内存。
在内存紧张的日子里,你必须最大限度地利用系统的每一口。
其他资源(例如文件句柄和套接字)要少得多,并且仍然需要手动处理(双关语)。
还要考虑 .net 紧凑框架,Windows 移动设备具有 32MB 或 64MB 的易失性内存可供使用,这并不罕见,当您仔细考虑时,这仍然是“很多”。
我想知道 .net 紧凑框架的占用空间是多少,以及它在具有 4mb 易失性内存的诺基亚手机上的表现如何。
有人有什么想法吗?
(这是一个维基答案,请随意更正或添加更多细节)
所以,恕我直言,我们可以缓慢地回收内存,因为我们不会匆忙耗尽它,但情况并非如此其他资源。
One of the reasons we can automatically manage memory allocation now is we have so much of it.
Back in the days when memory was tight you had to squeeze the most out of every bite the system had.
Other resouces such as file handles and sockets are far fewer, and still need to be handled by hand (pun intended).
Consider also the .net compact framework, it’s not uncommon for windows mobile devices to have 32mb or 64mb of volatile memory to play with which - when you think about it - is still “lots”.
I wondering what the footprint of the .net compact framework is, and how would it perform on a Nokia phone with 4mb of volatile memory.
Anyone any ideas?
(This is a wiki answer, feel free to correct or add more detail)
So, IMHIO we can afford to be slow reclaiming memory, because we're not going to run out of it in a hurry, which isn't the case with other resources.
对象持久化和缓存子系统可以被认为是文件和资源的自动分配。 如果将缓存子系统应用于网络连接,则不必关心文件打开、文件删除等。
一种自动管理网络连接的方法可以在并行计算环境(即MPI)中完成,您可以以编程方式设置处理器互连的形状。 而不是您只是从一个进程向另一个进程发送消息,几乎忽略了它的实现方式。 有时这些消息是在套接字中翻译的。
如果您有一个功能可以让您从其 Url 获取页面,您会认为它是一种自动套接字管理吗?
Object persistence and caching subsystems can be considered an automatic allocation of files and resources. If you apply a caching subsystem to a network connection you don't have to care about file opening, file deleting, and so on.
A way to manage automatically network connection could be done in parallel computing environment (i.e MPI), you can set programmatically the shape of the processors interconnections. Than you just send a message from a process to another, almost ignoring the way it's implemented. Sometimes those messages are translated in sockets.
If you have a function that let you get a page from its Url, would you consider it a sort of Automatic socket management?