How to force garbage collection in Java?

后端 未结 22 1409
离开以前
离开以前 2020-11-22 00:31

Is it possible to force garbage collection in Java, even if it is tricky to do? I know about System.gc(); and Runtime.gc(); but they only suggest t

相关标签:
22条回答
  • 2020-11-22 00:53

    Using the Java™ Virtual Machine Tool Interface (JVM TI), the function

    jvmtiError ForceGarbageCollection(jvmtiEnv* env)
    

    will "Force the VM to perform a garbage collection." The JVM TI is part of the JavaTM Platform Debugger Architecture (JPDA).

    0 讨论(0)
  • 2020-11-22 00:53

    If you are running out of memory and getting an OutOfMemoryException you can try increasing the amount of heap space available to java by starting you program with java -Xms128m -Xmx512m instead of just java. This will give you an initial heap size of 128Mb and a maximum of 512Mb, which is far more than the standard 32Mb/128Mb.

    0 讨论(0)
  • 2020-11-22 00:53

    I would like to add some thing here. Please not that Java runs on Virtual Machine and not actual Machine. The virtual machine has its own way of communication with the machine. It may varry from system to system. Now When we call the GC we ask the Virtual Machine of Java to call the Garbage Collector.

    Since the Garbage Collector is with Virtual Machine , we can not force it to do a cleanup there and then. Rather that we queue our request with the Garbage Collector. It depends on the Virtual Machine, after particular time (this may change from system to system, generally when the threshold memory allocated to the JVM is full) the actual machine will free up the space. :D

    0 讨论(0)
  • 2020-11-22 00:54

    Really, I don't get you. But to be clear about "Infinite Object Creation" I meant that there is some piece of code at my big system do creation of objects whom handles and alive in memory, I could not get this piece of code actually, just gesture!!

    This is correct, only gesture. You have pretty much the standard answers already given by several posters. Let's take this one by one:

    1. I could not get this piece of code actually

    Correct, there is no actual jvm - such is only a specification, a bunch of computer science describing a desired behaviour ... I recently dug into initializing Java objects from native code. To get what you want, the only way is to do what is called aggressive nulling. The mistakes if done wrong are so bad doing that we have to limit ourselves to the original scope of the question:

    1. some piece of code at my big system do creation of objects

    Most of the posters here will assume you are saying you are working to an interface, if such we would have to see if you are being handed the entire object or one item at a time.

    If you no longer need an object, you can assign null to the object but if you get it wrong there is a null pointer exception generated. I bet you can achieve better work if you use NIO

    Any time you or I or anyone else gets: "Please I need that horribly." it is almost universal precursor to near total destruction of what you are trying to work on .... write us a small sample code, sanitizing from it any actual code used and show us your question.

    Do not get frustrated. Often what this resolves to is your dba is using a package bought somewhere and the original design is not tweaked for massive data structures.

    That is very common.

    0 讨论(0)
  • 2020-11-22 00:56

    You can trigger a GC from the command line. This is useful for batch/crontab:

    jdk1.7.0/bin/jcmd <pid> GC.run
    

    See :

    • https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html
    0 讨论(0)
  • 2020-11-22 00:57

    Your best option is to call System.gc() which simply is a hint to the garbage collector that you want it to do a collection. There is no way to force and immediate collection though as the garbage collector is non-deterministic.

    0 讨论(0)
提交回复
热议问题