1

I'm very new to Java. I found in this link an easy way (without any programming) to increase assigned JVM memory in win 7 http://www.wikihow.com/Increase-Java-Memory-in-Windows-7

My question: Is there a similar way to perform (force) garbage collection in Win 7? I mean something like System.gc() but in windows

I need it to be in Windows without going into details of Heap analysis and stuff like that

  • No, even calling `System.gc()` isn't gonna starts a collection for sure. – Jonathan Drapeau Aug 20 '15 at 14:26
  • 2
    Java VisualVM allows you to connect to a java process, and force it to perform a GC, but I'm not really sure that it's what you want (your question is somewhat unclear). – Florent Bayle Aug 20 '15 at 14:27
  • Calling `System.gc()` just tells that the GC should run, but you aren't guaranteed it will run (at that particular point in time). Also, calling the GC yourself is usually not recommended, ideally you should leave the GC alone. What exactly are you after? – npinti Aug 20 '15 at 14:28
  • What I'm after is that I have a Java app that throws this common error `outOfMemoryErro` and I'm new to Java. I was able to increase the assigned memo as above in the link but still have some memo problems. So I want to perform GC also through windows to solve my problem for the Java app. And because I'm from VB and know little about Java, I need it to be simple through windows –  Aug 20 '15 at 14:34
  • 1
    Performing GC won't help you with that. Increasing the heap will do the trick. Also, you should edit your question with the `OutOfMemoryError` information. – Fred Porciúncula Aug 20 '15 at 14:34

5 Answers5

2

Forget about GC. If you're having OutOfMemoryError (and there is nothing in your code screwing up your memory usage, because if that's the case you should fix it first) you need to increase the heap size.

There are two parameters that will help you with that: Xmx and Xms. The first is the most important, since it defines the maximum memory a Java application can use. The second defines the initial heap size, but the JVM will increase it if it's needed, until it reaches the maximum value.

You can check this question to read about these parameters: What are the Xms and Xmx parameters when starting JVMs?

Community
  • 1
  • 1
Fred Porciúncula
  • 8,533
  • 3
  • 40
  • 57
  • 1
    `If you're having OutOfMemoryError you need to increase the heap size.` Or fix your code to use less memory :D (first thing to try actually) – Dici Aug 20 '15 at 14:40
  • True story. But even if his code is bad, GC should be able to handle his useless allocated objects. It's not like there might be a memory leak or something. So, in general, the solution is to increase the heap. – Fred Porciúncula Aug 20 '15 at 14:45
  • There could definitely be a memory leak. There are hundred of possible reasons : trying to sort in memory a large stream of data instead of using a write on disk + merge algorithm, letting the default Json parser interning all the fields of some Json records whereas one of them is a unique id and there are millions of them... etc – Dici Aug 20 '15 at 15:45
  • Trying to sort in memory a large stream of data isn't a memory leak, it's just bad programming. Same with your JSON example. – Fred Porciúncula Aug 20 '15 at 15:48
  • Yeah, whatever, you get me. The first thing to do is to check you're not doing anything stupid, not blindly increasing the heap size, it is the best way to build unscalable systems – Dici Aug 20 '15 at 15:51
1

You cannot force jvm to perform garbage collection. You can only request it by using System.gc() method or Runtime.gc() method.
To increase the chances for garbage collection use this code:
for(int i=0;i<2000;++i)
System.gc();

This will higher chances for garbage collection to be performed.

Gaur93
  • 685
  • 7
  • 19
  • Where does the 95% chance come from ? – Dici Aug 20 '15 at 14:30
  • well it is not written anywhere but you cannot ensure 100% that garbage collection will be performed. – Gaur93 Aug 20 '15 at 14:33
  • `x != 100` does not mean `x == 95`. You should remove it, you have no idea of when the GC will occur – Dici Aug 20 '15 at 14:35
  • Objection: there is one hard moment where you know with 100% certainty that a GC is going to happen: right before the JVM throws an OutOfMemory error. – Gimby Aug 20 '15 at 15:20
1

The first thing you must know about Java is that it is 99% platform-independent so asking a question specifically for Windows is usually unrelevant. The garbage collection occurres within the JVM, it has nothing to do with the operating system running the JVM. By the way, there is no way to force an immediate GC, as the doc of System.gc explains it :

Calling the gc 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.

Dici
  • 25,226
  • 7
  • 41
  • 82
1

If you mean from the command line you can do

on windows

jmap -histo:live {pid} > nul

on linux

jmap -histo:live {pid} > /dev/null

This will trigger a full GC from the command line.

You should never need to do this except perhaps for exotic test cases.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Should I do the above command `jmap -histo:live {pid} > nul` when I get the error or when I think that this error may happen? And thanks btw, because you the only one who included Windows in their answers. –  Aug 24 '15 at 12:30
  • @AhmedAdel Triggering a GC manually won't help you avoid an OutOfMemoryError as it will do this for you anyway. If you run this command without dumping the output it can help you determine which objects are using all the space. – Peter Lawrey Aug 24 '15 at 15:05
0

How to perform Garbage Collection in win 7?

Empty the recycle bin!

Just kidding. The tutorial you linked describes how to increase the upperbound of memory attributed to Java processes. Giving them more memory could allow them to run faster I they are memory hungry. This has nothing to do with triggering a single garbage collection.

You can't do the same (i.e. attributing more memory) with Window since Windows is the operating system which controls and manages the available memory.

wero
  • 32,544
  • 3
  • 59
  • 84
  • 1
    Memory has nothing to do with garbage collection ? The more you have memory on the heap, the less you are likely to need major GCs – Dici Aug 20 '15 at 14:32
  • 3
    _This has nothing to do with garbage collection_ It definitely does. If you increase the heap size, GC will occur less often and that is why you end up with better performance. On the other hand, if you increase it too much, GC will still occurr less often but it will take too long to process the entire heap and you may actually degrade performance. But of course it depens on the GC algorithm being used and on the application itself, this is a vast and complex topic. – Fred Porciúncula Aug 20 '15 at 14:32
  • @ThiagoPorciúncula of course. But in my eyes the OP seems to confound increasing max memory with performing a gc, and that is expressed in my answer. – wero Aug 20 '15 at 14:36
  • I agree that OP is confused about that. But your answer seems to be saying that heap size and performance gain has nothing to do with GC. – Fred Porciúncula Aug 20 '15 at 14:41