简体   繁体   English

Java如何有效地管理内存

[英]How Java manage memory efficiently

Strings are immutable in java ie every time we make changes in string object it creates a new instance and the old object become unreferenced ie waste. 字符串在java中是不可变的,即每次我们在字符串对象中进行更改时,它都会创建一个新实例,而旧对象则变为未引用,即浪费。 so in a big program there will be so many unreferenced objects which can not be access. 所以在一个大程序中会有很多无法访问的未引用对象。 Does java manage this? java管理这个吗? how? 怎么样? for example- 例如-

String s="abc";
s=s.concat("def");

now object "abc" can not be referenced at all but as strings are immutable it will still exist in the memory pool. 现在对象“abc”根本无法引用,但由于字符串是不可变的,它仍然存在于内存池中。

First of all, it sounds like you need a crash source in Java and garbage collection. 首先,听起来你需要Java和垃圾收集中的崩溃源。 With that said, there's a few basic points to clear up: 话虽如此,还有一些基本要点需要澄清:

1) Just because an object is immutable does not mean its memory is leaked. 1)仅仅因为一个对象是不可变的并不意味着它的内存泄漏。 If no references exist to an immutable object, it is just as eligible for garbage collection as any other objects. 如果对不可变对象没有引用,则它与任何其他对象一样有资格进行垃圾回收。

2) String constants are an exception to this because they are always interned by the JVM. 2)字符串常量是一个例外,因为它们总是由JVM 实习 This means that string constants are kept in a special memory pool, and any time a string is created, this pool is first checked to see if that string already exists. 这意味着字符串常量保存在特殊的内存池中,并且每次创建字符串时,首先检查此池以查看该字符串是否已存在。 If it does, a reference to it is returned. 如果是,则返回对它的引用。 (You can force non-constant strings to join the pool using the String.intern() method). (您可以使用String.intern()方法强制非常量字符串加入池)。

3) The amount of memory these strings occupy is so minimal that you should essentially never worry about it. 3)这些字符串占用的内存量非常小,您基本上不必担心它。

Java has automatic garbage collector which keeps running in background.This garbage collector keep checking for unused object and once it detect/find any such object it destroy it ie free the memory/resources used by that particular object.This is taken care by JVM so you need not worry about it.However if you want you can instruct JVM to do garbage collection .After that JVM can schedule garbage collection accordingly Java有自动垃圾收集器,它继续在后台运行。这个垃圾收集器不断检查未使用的对象,一旦它检测到/找到任何这样的对象就会销毁它,即释放该特定对象使用的内存/资源。这是由JVM照顾的。你不用担心它。但是如果你想要你可以指示JVM进行垃圾收集。之后JVM可以相应地安排垃圾收集

To know how garbage collection works check the below link: 要了解垃圾收集的工作原理,请查看以下链接:

http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html http://javarevisited.blogspot.in/2011/04/garbage-collection-in-java.html

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Memory management in java is done by JVM ie Garbage Collector in JVM Java中的内存管理由JVM(即JVM中的垃圾收集器)完成

All objects are stored in heap when they have reference like 所有对象在引用时都存储在堆中

MyCode ref=new MyCode();

Garbage collector validate that object for Garbage collection in two cases 垃圾收集器在两种情况下验证垃圾收集的对象

  1. If reference to object becomes null 如果对象的引用变为null
ref=null;
  1. When Island of Isolation happens 当隔离岛发生时

http://www.geeksforgeeks.org/island-of-isolation-in-java/ http://www.geeksforgeeks.org/island-of-isolation-in-java/

Memory management is some what different regarding String objects as above answers have already explained it 内存管理是关于String对象的一些不同,如上面的答案已经解释过它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM