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
相关推荐
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 ...
Section two is an in depth core Java review. Java interviews are really hard. For some reason interviewers love to ask complicated GC tuning questions, or challenge your knowledge on the Big O of ...
Java® Performance Companion shows how to systematically and proactively improve Java performance with today’s advanced multicore hardware and complex operating system environments. The authors, who...
Java® Performance Companion shows how to systematically and proactively improve Java performance with today’s advanced multicore hardware and complex operating system environments. The authors, who...
String str = "GC: Unchecked type in generic call (GC_F_BA_ACD_FDALFD_I_FALDJF)"; Pattern pattern = Pattern.compile("\\([A-Z]+(\\_[A-Z]+)*\\)"); Matcher matcher = pattern.matcher(str); while...
java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或内置...
在 java 语言中,Java 程序的基本单位是类,也就是说:一个 Java 程序是由多个类组成 的。定义一个类与定义一个数据类型是有区别的。在程序设计语言中,把定义数据类型的能 力作为一种很重要的能力来对待。在面向...
花了一整天在跟踪一个问题,每次感觉已经快找到原因的时候发现现象又变了,我觉得从中吸取的教训可以... Map map = System.getProperties(); Random r = new Random(); while (true) { map.put(r.nextInt(), "valu
JAVAC的帮助,输入[sd0807@localhost ~]$ javac 编译:javac ***.java ... load Java programming language agent, see java.lang.instrument -splash:<imagepath> show splash screen with specified image
v2: fix gc error. INSTRUCTIONS 1. Copy "mandroid.exe" to "C:\Program Files (x86)\MSBuild\Xamarin\Android" (or "C:\Program Files\MSBuild\Xamarin\Android" if you are on a 32-bit operating system) 2. ...
What’s New in Python What’s New In Python 3.6 Summary – Release highlights New Features PEP 498: Formatted string literals PEP 526: Syntax for variable annotations PEP 515: Underscores in ...
foreach (System.Windows.Forms.Control control in this.Controls) { if (control is System.Windows.Forms.TextBox) { System.Windows.Forms.TextBox tb = (System.Windows.Forms.TextBox)control ; tb.Text ...
77、简述synchronized和java.util.concurrent.locks.Lock的异同 ? 18 78、abstract class Name { private String name; public abstract boolean isStupidName(String name) {}}这有何错误? 18 79、public class ...
•在java类里只能包含Field,方法,构造器,初始化块,内部类(接口、枚举)等5种成员。 用static修饰的类成员属 于类成员,类Field既可通过类来访问,也可以通过类的对象来访问。当通过对象来访问类属性时,系统会在底...
java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 6、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或...
gc0307 hm1375 ;------------------------------------------------------------------------------- camera_id = 0 ;------------------------------------------------------------------------------- ; 1 ...
# frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:system/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \ # frameworks/native/data/etc/android....
# frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:system/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \ # frameworks/native/data/etc/android....
Plugins: Plugins can now have side dll's that are statically linked in their own folder (Windows 7 with updates and later) Debugging: Improved the FPU window editing when single stepping, allowing you...