java中有没有像malloc/free这样的东西?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4404872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there something like malloc/free in java?
提问by java
I've never seen such statements though,does it exist in java world at all?
不过,我从未见过这样的陈述,它是否存在于 Java 世界中?
采纳答案by ClosureCowboy
Java's version of malloc
is new
-- it creates a new object of a specified type.
Java 的版本malloc
是new
——它创建一个指定类型的新对象。
In Java, memory is managed for you, so you cannot explicitly delete
or free
an object.
在 Java 中,内存是为您管理的,因此您不能显式delete
或free
对象。
回答by kriss
new
instead of malloc
, garbage collector instead of free.
new
而不是malloc
,垃圾收集器而不是免费的。
回答by Alfred
Java has a garbage collector. That's why you never see such statements in your code(which is nice if you ask me)
Java 有一个垃圾收集器。这就是为什么你永远不会在你的代码中看到这样的语句(如果你问我这很好)
In computer science, garbage collection (GC) is a form of automatic memory management. It is a special case of resource management, in which the limited resource being managed is memory. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp.
在计算机科学中,垃圾收集 (GC) 是一种自动内存管理形式。这是资源管理的一种特殊情况,其中被管理的有限资源是内存。垃圾收集器,或只是收集器,试图回收垃圾,或者被程序不再使用的对象占用的内存。垃圾收集是由 John McCarthy 在 1959 年左右发明的,用于解决 Lisp 中的问题。
回答by Stephen C
No direct equivalents exist in Java:
Java 中不存在直接的等价物:
C malloc
creates an untyped heap node and returns you a pointer to it that allows you to access the memory however you want.
Cmalloc
创建一个无类型的堆节点并返回一个指向它的指针,允许你随意访问内存。
Java does not have the concept of an untyped object, and does not allow you to access memory directly. The closest that you can get in Java to malloc
would be new byte[size]
, but that returns you a strongly typed object that you can ONLY use as a byte array.
Java 没有无类型对象的概念,并且不允许您直接访问内存。您可以在 Java 中获得的最接近的malloc
是new byte[size]
,但这会返回一个强类型对象,您只能将其用作字节数组。
C free
releases a heap node.
Cfree
释放一个堆节点。
Java does not allow you to explicitly release objects. Object deallocation in Java is totally in the hands of the garbage collector. In some cases you can influence the behaviour of the GC; e.g. by assigning null
to a reference variable and calling System.gc()
. However, this does not force the object to be deallocated ... and is a very expensive way to proceed.
Java 不允许显式释放对象。Java 中的对象释放完全掌握在垃圾收集器的手中。在某些情况下,您可以影响 GC 的行为;例如,通过分配null
给引用变量并调用System.gc()
. 然而,这并不强制对象被释放......并且是一种非常昂贵的处理方式。
回答by AlanObject
If you are up to no good (tm) I suppose you can get access to raw memory though the JNI interface. This is where you can call C programs from Java Programs. Of course you have to be running in an environment where your program has the privileges to do so (a browser won't normally allow this unless it is suicidal) but you can access objects via C pointers that way.
如果您做得不好(tm),我想您可以通过 JNI 接口访问原始内存。您可以在此处从 Java 程序调用 C 程序。当然,您必须在您的程序有权这样做的环境中运行(浏览器通常不会允许这样做,除非它是自杀式的),但您可以通过 C 指针访问对象。
I sort of wonder where the original question is coming from. At one point long ago I was totally skeptical of the notion that C-style memory management and C-style pointers were not needed, but at this point I am true believer.
我有点想知道最初的问题来自哪里。很久以前,我完全怀疑不需要 C 风格的内存管理和 C 风格的指针的概念,但在这一点上我是真正的信徒。