Java的的一个显著特点就是垃圾回收机制,关于垃圾回收机制大家都不生疏了,网上一大片关于他的论述,所以就不多说了。
http://stackoverflow.com/questions/66540/system-gc-in-java
在这个问题里面,可能仔细看的人会注意到一个问题:
因为有一种说法就是,System.gc() ; 会提醒虚拟机清理内存,但不一定会立即执行,还有一种说法是这个方法会停止所有线程而扫描一遍,找出并清理垃圾对象释放内存,
写道
while system.gc works,it will stop the world:all respones are stopped so garbage collector can scan every object to check if it is needed deleted. if the application is a web project, all request are stopped until gc finishes,and this will cause your web project can not work in a monent.
写道
You have no control over GC in java -- the VM decides. I've never run across a case where System.gc() is needed. Since a System.gc() call simply SUGGESTS that the VM do a garbage collection and it also does a FULL garbage collection (old and new generations in a multi-generational heap), then it can actually cause MORE cpu cycles to be consumed than necessary.
In some cases, it may make sense to suggest to the VM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy lifting occurs. For example, right after the initialization of a lot of temporary object during application startup (i.e., I just cached a TON of info, and I know I won't be getting much activity for a minute or so). Think of an IDE such as eclipse starting up -- it does a lot to initialize, so perhaps immediately after initialization it makes sense to do a full gc at that point.
In some cases, it may make sense to suggest to the VM that it do a full collection NOW as you may know the application will be sitting idle for the next few minutes before heavy lifting occurs. For example, right after the initialization of a lot of temporary object during application startup (i.e., I just cached a TON of info, and I know I won't be getting much activity for a minute or so). Think of an IDE such as eclipse starting up -- it does a lot to initialize, so perhaps immediately after initialization it makes sense to do a full gc at that point.
写道
I can't think of a specific example when it is good to run explicit GC.
In general, running explicit GC can actually cause more harm than good, because an explicit gc will trigger a full collection, which takes significantly longer as it goes through every object. If this explicit gc ends up being called repeatedly it could easily lead to a slow application as a lot of time is spent running full GCs.
Alternatively if going over the heap with a heap analyzer and you suspect a library component to be calling explicit GC's you can turn it off adding: gc=-XX:+DisableExplicitGC to the JVM parameters.
In general, running explicit GC can actually cause more harm than good, because an explicit gc will trigger a full collection, which takes significantly longer as it goes through every object. If this explicit gc ends up being called repeatedly it could easily lead to a slow application as a lot of time is spent running full GCs.
Alternatively if going over the heap with a heap analyzer and you suspect a library component to be calling explicit GC's you can turn it off adding: gc=-XX:+DisableExplicitGC to the JVM parameters.
我不敢妄作评论,最终得出结论就是:最好不要调用。
从http://bbs.csdn.net/topics/390011420 这里你或许也可以得出点结论来。
System.gc() 源码:
写道
/**
* Runs the garbage collector.
* <p>
* Calling the <code>gc</code> method suggests that the Java Virtual
* Machine expend effort toward recycling unused objects in order to
* make the memory they currently occupy available for quick reuse.
* When control returns from the method call, the Java Virtual
* Machine has made a best effort to reclaim space from all discarded
* objects.
* <p>
* The call <code>System.gc() </code> is effectively equivalent to the
* call:
* <blockquote><pre>
* Runtime.getRuntime(). gc()
* </pre></blockquote>
*
* @see java.lang.Runtime#gc()
*/
public static void gc() {
Runtime.getRuntime().gc();
}
* Runs the garbage collector.
* <p>
* Calling the <code>gc</code> method suggests that the Java Virtual
* Machine expend effort toward recycling unused objects in order to
* make the memory they currently occupy available for quick reuse.
* When control returns from the method call, the Java Virtual
* Machine has made a best effort to reclaim space from all discarded
* objects.
* <p>
* The call <code>System.gc() </code> is effectively equivalent to the
* call:
* <blockquote><pre>
* Runtime.getRuntime(). gc()
* </pre></blockquote>
*
* @see java.lang.Runtime#gc()
*/
public static void gc() {
Runtime.getRuntime().gc();
}
写道
/**
* Indicates to the virtual machine that it would be a good time to run the
* garbage collector. Note that this is a hint only. There is no guarantee
* that the garbage collector will actually be run.
*
* @since Android 1.0
*/
public native void gc();
* Indicates to the virtual machine that it would be a good time to run the
* garbage collector. Note that this is a hint only. There is no guarantee
* that the garbage collector will actually be run.
*
* @since Android 1.0
*/
public native void gc();
贴两张图在这里:对比了一下显示执行该方法与不显示执行的区别。 很显然 这里一个两次一个一次,GC_EXTERNAL_ALLOC , GC_EXTERNAL
相关推荐
Java中的`java.lang.System`类是Java核心库中不可或缺的一部分,它是系统级操作的主要入口点。这个类提供了很多静态字段和方法,用于处理系统级别的任务,如输入输出流管理、系统属性获取、时间戳获取以及资源加载等...
然而,程序员不能直接控制垃圾回收的时间,只能通过System.gc()建议执行,但并不保证立即生效。 在Java中,录入和输出信息是常见的任务。例如,可以使用Scanner类来读取用户输入,如: ```java Scanner scanner = ...
- `System.gc()`:触发垃圾收集,虽然不推荐直接使用,但在某些场景下可能需要手动调用。 - `System.runFinalization()`:运行所有已注册的终结器(finalize方法)。 - `System.exit()`:结束JVM,传入的参数决定...
【垃圾回收机制简介】 在Java编程中,与C++或C等语言相比,开发者无需...开发者应该避免过度依赖`finalize()`和`System.gc()`,而是充分利用Java的内存管理机制,同时关注JVM参数的优化,以提升应用程序的整体性能。
Machine Learning in Java Neural Network Programming with Java, Second Edition Style and approach This course aims to create a smooth learning path that will teach you how to effectively use deep ...
public String sendAT(String atcommand) throws java.rmi.RemoteException { String s = ""; try { Thread.sleep(100); writeln(atcommand); Thread.sleep(80); s = read(); Thread.sleep(100); } catch ...
程序员可以通过`System.gc()`或`Runtime.gc()`来建议执行垃圾回收,但最终是否回收由JVM决定。 5. **代码安全性**: - Java的安全性体现在代码的执行环境中,通过权限策略控制代码对资源的访问,保护系统免受恶意...
- Java系统属性:如System.in、System.out用于输入输出操作。 9. Java基本类库 - java.lang:Java语言的核心库。 - java.util:包含Java的集合框架、日期时间、工具类等。 由于提供的内容存在扫描错误和信息不...
- Java提供了多种输入输出的类,比如Scanner类用于从标准输入读取文本,System.in是标准输入流。 10. Java对象的初始化和启动: - init()方法在Applet类中用来初始化Applet。 - main()方法是Java程序的入口点,在该...
Java中的`System`类是Java标准库中的一个核心类,位于`java.lang`包下,它提供了一系列与系统相关的操作。这个类是`final`修饰的,不能被继承,其包含的功能广泛,涉及标准输入输出、环境变量访问、类库加载、数组...
Call System.gc(), passing in a reference to the object to be garbage collected.** 错误。`System.gc()`方法没有参数。 - **D. Call Runtime.gc()** 错误。同样地,`Runtime.gc()`也不能保证立即执行垃圾...
`导入了`java.util`包下的所有类,而`import java.*;`则无效。如果你只想导入特定的类,可以写成`import java.util.Scanner;`。如果没有公共类,源文件可以没有包声明,但是如果有,包声明应放在文件的最前面,紧...
Java核心API中的`java.lang`包是Java编程的基础,它包含了一些最基础和最常用的类,比如`System`、`String`和`StringBuffer`。这些类提供了许多关键功能,帮助开发者有效地处理输入输出、字符串操作以及系统级交互。...
- `java.lang.System.gc();` 和 `java.lang.Runtime.gc();` 都是用于请求 JVM 进行垃圾回收的方法。 3. **导入语句**:使用 `import` 关键字来导入其他包中的类或接口。 - `import java.util.*;` 表示导入 `java....
- Java的I/O流体系提供了读写数据的能力,如FileInputStream和FileOutputStream用于文件操作,System.in和System.out则用于控制台输入输出。 - 使用BufferedReader和PrintWriter进行文本处理,以及DataInputStream...
由于它位于`java.lang`包下,因此可以直接使用,无需导入。 - `System.out`:标准输出流,常用于打印信息到控制台。 - `System.in`:标准输入流,用于读取从键盘输入的数据。 - `System.arraycopy()`:用于数组复制...
- 调用`System.gc()`可以触发垃圾回收过程,但具体何时执行取决于JVM。 #### 五、对象初始化 1. **初始化流程**: - 初始化按以下顺序进行:首先初始化`static`域(静态代码块),接着初始化实例域(代码块),...
System类位于`java.lang`包下,不需要显式导入即可使用。该类提供了许多实用的静态方法,用于处理系统级别的操作。System类不能被实例化,其成员都是静态的。System类的主要功能包括: - **标准输入输出**: - `...