9

I had three projects open. One of them - Spark - was very large. Upon closing spark there was NO difference in memory usage - as reported by os/x activity monitor. Note: all projects are opened within the same Intellij instance.

enter image description here

It is in fact using just over 4GB. And I only now have two projects open. Those two projects only take up 1.5GB if I shut down Intellij and start it up again.

So .. what to do to "encourage" Intellij to release the memory it is using? It is running very very slowly (can not keep up with my typing for example)

Update I just closed the larger of the two remaining projects. STILL no reduction in memory usage. The remaining project is a single python file. So Intellij should be using under 512Meg at this point!

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

3 Answers3

5

Following up on @PeterGromov's answer it seems that is were difficult to obtain the memory back. In addition @KevinKrumwiede mentioned the XX:MaxHeapFreeRatio which appears to be an avenue.

Here are a couple of those ideas taken bit farther from Does GC release back memory to OS?

The HotSpot JVM does release memory back to the OS, but does so reluctantly.

You can make it more aggressive by setting -XX:GCTimeRatio=19 -XX:MinHeapFreeRatio=20 -XX:MaxHeapFreeRatio=30 which will allow it to spend more CPU time on collecting and constrain the amount of allocated-but-unused heap memory after a GC cycle.

Additionally with Java 9 -XX:-ShrinkHeapInSteps option can be be used to apply the shrinking caused by the previous two options more aggressively. Relevant OpenJDK bug.

Do note that shrinking ability and behavior depends on the chosen garbage collector. For example G1 only gained the ability to yield back unused chunks in the middle of the heap with jdk8u20.

So if heap shrinking is needed it should be tested for a particular JVM version and GC configuration.

and from How to free memory in Java?

To extend upon the answer and comment by Yiannis Xanthopoulos and Hot Licks (sorry, I cannot comment yet!), you can set VM options like this example:

-XX:+UseG1GC -XX:MinHeapFreeRatio=15 -XX:MaxHeapFreeRatio=30 In my jdk 7 this will then release unused VM memory if more than 30% of the heap becomes free after GC when the VM is idle. You will probably need to tune these parameters.

While I didn't see it emphasized in the link below, note that some garbage collectors may not obey these parameters and by default java may pick one of these for you, should you happen to have more than one core (hence the UseG1GC argument above).

I am going to add the -XX:MaxHeapFreeRatio to IJ and report back if it were to help.

Our application presently only runs on Java7 so the first approach above is not yet viable - but there is hope since our app is moving to jdk8 soon.

Community
  • 1
  • 1
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
4

https://www.jetbrains.com/help/idea/status-bar.html

I used this:

Shows the current heap level and memory usage. Visibility of this section in the Status bar is defined by the Show memory indicator check box in the Appearance page of the Settings/Preferences dialog. It is not shown by default. Click the memory indicator to run the garbage collector.

  • 1
    The link may stop working, and other users could not find useful your answer. Please try integrate some explanation in your answer – GabrieleMartini Feb 07 '19 at 12:57
  • I don't know man. In the memory indicator it shows ONLY 624 MB used our of 1923 MB, while windows tells me PyCharm is actually using 1.5 GB of memory. I then clicked on the memory indicator and it released some 400MB memory but WIndows still show the same memory usage. I suspect it actually just released the memory to an "allocated pool", not to the OS. – Nicholas Humphrey Jun 08 '21 at 16:08
0

The underlying Java virtual machine supports only growing of its heap. So even if after closing all projects the IDE doesn't need all of it, it's still allocated and counted as used in the OS.

Peter Gromov
  • 17,615
  • 7
  • 49
  • 35