Java会自动释放内存吗?

huangapple go评论84阅读模式
英文:

java deallocates memory automatically?

问题

我正在寻找一种将字节数组转换为字符串的方法,我找到的唯一方法是这样的:

String s1 = "horse";
String s2;
byte[] b = s1.getBytes();
s2 = new String(b);

所以我的问题是,是否有其他方法在不创建新的String实例的情况下将字节数组转换为字符串?
另外,如果我反复执行以下操作:

String s;
while (true) {
  s = new String();
}

会占用越来越多的内存,还是会自动释放和重新分配内存?如果内存会自动释放,我就不必再寻找将字节数组转换为字符串的替代方法了。

附注(我希望s2的值为“horse”)
附注2(抱歉,我的英语不太好)

英文:

I was looking for a method to turn a byte array into a string, the only way I found is this:

String s1="horse";
String s2;
byte[] b=s1.getBytes();
s2=new String(b);

So my questions are, is there any other way to convert a byte array to a string without creating a new instance of String?
Also if I repeatedly did:

String s;
while(true){
  s=new String();
}

Would take up more and more memory, or it is automatically deallocated and reallocated? If the memory were deallocated automatically, I would no longer have to look for an alternative method to convert an array of bytes to a string.

P.S.(I want s2 to be "horse")
P.S.2(Sorry my bad english)

答案1

得分: 2

Johannes 的评论是一个很好的起点,因为垃圾回收是 Java 中的一个关键概念。

然而,要回答你的问题,当使用字节数组初始化时,是的,你需要创建一个新的字符串实例。

在你的第二个代码片段中:

String s;
while(true){
  s=new String();
}

我们有一个字符串 s,它是一个指向空的字符串的指针。然后在循环中,你将它指向堆上的一个字符串对象。当你在循环中重新分配 s 时,它会为新的字符串分配更多的内存,然后垃圾收集器会从堆中回收旧的字符串对象。这是因为该对象再没有任何引用指向它了。

这里有一篇关于字符串不可变性的好文章。

英文:

The comment by Johannes is a good starting place as Garbage collection is a key concept in Java.

To answer your questions though, no you will need to create a new instance of string when initializing with a byte array.

In your second code snippet:

String s;
while(true){
  s=new String();
}

What we have is a String 's' which is a String pointer to nothing. Then in your loop you point this to a String object on the heap. When you reassign 's' in the loop it will allocate more memory for your new String and then the garbage collector will pick up the old String object from the heap. This is because the object has nothing referencing it anymore.

Here's a good article on string immutability.

答案2

得分: 2

所以我的问题是,是否有任何其他方法可以将字节数组转换为字符串,而不创建新的 String 实例?

没有其他方法。Java 字符串是不可变的。这意味着你不能更新/替换字符串中的字符。将字节数组转换为现有字符串会违反不可变性。

String s;
while (true) {
  s = new String();
}

会占用越来越多的内存,还是会自动释放和重新分配内存?

内存会被垃圾回收器(GC)自动回收。GC 会偶尔运行,识别不再可达的对象;也就是程序无法再找到的对象。然后这些对象会被删除。

这只是简化版本。实际上,并不是所有的“丢失”对象都会在同一时间被回收,而且有一些特殊类型的可达性会被不同地处理。

无论如何,在你的例子中,每次程序循环执行时,都会创建一个新的 String 对象,并且对先前 String 的引用会丢失。然后稍后(根据需要)GC 会找到并删除这些丢失的对象。

如果内存会自动释放,我将不再需要寻找将字节数组转换为字符串的替代方法。

是的,而且你不需要。

在 Java 中,你只需让运行时系统处理对象内存的分配和释放。(只需确保不会意外地使对象保持可达状态。)

英文:

> So my questions are, is there any other way to convert a byte array to a string without creating a new instance of String?

No there isn't. Java strings are immutable. That means that you cannot update / replace the characters in a string. Converting a byte array to an existing string would violate immutability.

> String s;
> while (true) {
> s = new String();
> }
>
> Would take up more and more memory, or it is automatically deallocated and reallocated?

Memory is automatically reclaimed by the Garbage collector (GC). The GC runs occasionally, identifies objects that are no longer reachable; i.e. that the program cannot find anymore. Those objects are then deleted.

<sup>That is the simple version. In reality not all "lost" objects are reclaimed at the same time, and there are some special kinds of reachability that are handled differently.</sup>

Anyway, in your example, each time the program goes around the loop, a new String object is created, and the reference to the previous String is lost. Then later (as required) the GC finds and deletes the lost objects.

> If the memory were deallocated automatically, I would no longer have to look for an alternative method to convert an array of bytes to a string.

It is, and you don't.

In Java, you just let the runtime system deal with allocation and deallocation of object memory. (Just make sure that you don't cause objects to remain reachable by accident.)

huangapple
  • 本文由 发表于 2020年4月3日 23:30:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61015270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定