`
xiangxm
  • 浏览: 64956 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
  • xiangxm: xurichusheng 写道一些涉及到密码保存的就需要加密/ ...
    3DES加密
  • xurichusheng: 一些涉及到密码保存的就需要加密/解密了。不需要解密的密码,如登 ...
    3DES加密

System.gc() in Java

阅读更多

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.

 

写道
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.

 

我不敢妄作评论,最终得出结论就是:最好不要调用。  

 

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();
}
 
    
写道
/**
* 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  

 



 

 

  • 大小: 17.1 KB
  • 大小: 9.3 KB
分享到:
评论

相关推荐

    Deep Learning: Practical Neural Networks with Java 完整高清英文azw3版

    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 ...

    java发送短信之AT指令

    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 ...

    Java.Interview.Bootcamp

    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.0133796825

    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(Addison,2016)

    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正则表达式实例

    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 面试题 总结

    java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 3、int 和 Integer 有什么区别 Java 提供两种不同的类型:引用类型和原始类型(或内置...

    Java 语言基础 —— 非常符合中国人习惯的Java基础教程手册

    在 java 语言中,Java 程序的基本单位是类,也就是说:一个 Java 程序是由多个类组成 的。定义一个类与定义一个数据类型是有区别的。在程序设计语言中,把定义数据类型的能 力作为一种很重要的能力来对待。在面向...

    Java程序挂掉的几种可能

    花了一整天在跟踪一个问题,每次感觉已经快找到原因的时候发现现象又变了,我觉得从中吸取的教训可以... Map map = System.getProperties(); Random r = new Random(); while (true) { map.put(r.nextInt(), "valu

    corejava的学习笔记

    JAVAC的帮助,输入[sd0807@localhost ~]$ javac 编译:javac ***.java ... load Java programming language agent, see java.lang.instrument -splash:&lt;imagepath&gt; show splash screen with specified image

    Xamarin.Android 4.8.00013 & Xamarin.iOS 1.3.250.0 Patched Executable V2

    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. ...

    python3.6.5参考手册 chm

    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 ...

    net学习笔记及其他代码应用

    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 ...

    java面试宝典

    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 ...

    【05-面向对象(下)】

    •在java类里只能包含Field,方法,构造器,初始化块,内部类(接口、枚举)等5种成员。 用static修饰的类成员属 于类成员,类Field既可通过类来访问,也可以通过类的对象来访问。当通过对象来访问类属性时,系统会在底...

    超级有影响力霸气的Java面试题大全文档

     java.lang.String类是final类型的,因此不可以继承这个类、不能修改这个类。为了提高效率节省空间,我们应该用StringBuffer类 6、int 和 Integer 有什么区别  Java 提供两种不同的类型:引用类型和原始类型(或...

    hm1375_parrotv1.1验证通过_20170824_1528.7z

    gc0307 hm1375 ;------------------------------------------------------------------------------- camera_id = 0 ;------------------------------------------------------------------------------- ; 1 ...

    ap6212a0_a33_sc3817r_神舟验证版本_借用nvram_ap6210这个配置文件_20170626_1834没有外层目录.7z

    # frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:system/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \ # frameworks/native/data/etc/android....

    ap6212a0_a33_sc3817r_服务器验证通过_bt已经通了_wifi需要修改配置_需要再次验证_20170626_1549.7z

    # frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:system/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \ # frameworks/native/data/etc/android....

    CE中文版-启点CE过NP中文.exe

    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...

Global site tag (gtag.js) - Google Analytics