High Memory Usage

Answered

I have a new MacBook pro that actually has 64 GB of RAM. However, I'm still running into memory issues. My project is huge (>50K lines of code).

First I'd like to ask a clarifying question. My project is built with Gradle through IntelliJ. It is a kotlin project with multiple subprojects. How is memory shared/distributed between all of these processes? I know that IntelliJ, the kotlin daemon, the gradle daemon, and each gradle fork are all seperate processes. Do these all have their own independent memory usage? And if so, how much do they all need?

I have chosen some settings somewhat arbitrarily. They are:

IntelliJ "Custom VM Options" 

-Xmx25000m


gradle.properties

org.gradle.jvmargs=-Xmx20g
kotlin.daemon.jvm.options=-Xmx20g

 

So it would seem I've split my memory into 3 roughly equal portions for IntelliJ, Gradle, and Kotlin.

However, I have no idea if this is optimal. I'm still running into out-of-memory issues in IntelliJ even though it has 25GB.

0
14 comments
Histogram. Top 50 by instance count:
    1: [44.2M/ 2.3GB] java.util.LinkedHashMap$Entry
    2: [40.6M/1.46GB] java.util.concurrent.ConcurrentHashMap$Node
    3: [27.2M/1.21GB] byte[]
    4: [26.8M/ 644MB] java.util.ArrayList
    5: [24.7M/ 594MB] org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope$FindClassRequest
    6: [20.5M/ 451MB] java.lang.String
    7: [18.8M/ 678MB] java.util.HashMap$Node
    8: [17.3M/2.12GB] java.lang.Object[]
    9: [17.2M/ 551MB] kotlin.SynchronizedLazyImpl!1
   10: [14.5M/ 247MB] org.jetbrains.kotlin.name.Name
   11: [12.5M/ 401MB] org.jetbrains.kotlin.storage.LockBasedStorageManager$LockBasedNotNullLazyValue
   12: [10.2M/ 164MB] org.jetbrains.kotlin.types.TypeSubstitutor
   13: [10.1M/ 254MB] org.jetbrains.kotlin.types.IndexedParametersSubstitution
   14: [10.1M/ 242MB] org.jetbrains.kotlin.types.TypeProjection[]
   15: [10.1M/ 740MB] java.util.LinkedHashMap
   16: [9.89M/ 910MB] java.util.concurrent.ConcurrentHashMap
   17: [8.63M/ 138MB] java.util.LinkedHashSet
   18: [7.97M/ 191MB] org.jetbrains.kotlin.types.TypeProjectionImpl
   19: [6.52M/1.51GB] java.util.HashMap$Node[]
   20: [6.25M/ 281MB] org.jetbrains.kotlin.types.SimpleTypeImpl
   21: [5.84M/ 140MB] org.jetbrains.kotlin.descriptors.impl.FieldDescriptorImpl
   22: [5.63M/90.2MB] org.jetbrains.kotlin.container.InstanceComponentDescriptor
   23: [5.48M/ 203MB] org.jetbrains.kotlin.types.KotlinTypeFactory$simpleType$1
   24: [5.34M/ 170MB] org.jetbrains.kotlin.storage.LockBasedStorageManager$MapBasedMemoizedFunctionToNotNull
   25: [5.12M/ 102MB] org.jetbrains.kotlin.protobuf.LiteralByteString
   26: [5.09M/ 121MB] org.jetbrains.kotlin.descriptors.TypeParameterDescriptor[]
   27: [5.09M/ 244MB] org.jetbrains.kotlin.resolve.scopes.SubstitutingScope
   28: [5.09M/ 101MB] org.jetbrains.kotlin.resolve.scopes.SubstitutingScope$substitutor$2
   29: [5.08M/ 101MB] org.jetbrains.kotlin.resolve.scopes.SubstitutingScope$_allDescriptors$2
   30: [4.92M/88.5MB] org.jetbrains.kotlin.protobuf.FieldSet
   31: [4.92M/ 260MB] org.jetbrains.kotlin.protobuf.SmallSortedMap$1
   32: [4.63M/ 185MB] org.jetbrains.kotlin.name.FqNameUnsafe
   33: [4.27M/68.4MB] java.util.LinkedHashMap$LinkedKeySet
   34: [3.95M/ 126MB] org.jetbrains.kotlin.storage.LockBasedStorageManager$MapBasedMemoizedFunction
   35: [3.93M/94.3MB] org.jetbrains.kotlin.name.FqName
   36: [3.61M/ 256MB] org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
   37: [3.41M/68.3MB] java.util.Collections$SingletonList
   38: [3.33M/80.0MB] com.intellij.util.keyFMap.OneElementFMap
   39: [2.94M/ 291MB] org.jetbrains.kotlin.descriptors.impl.PropertyGetterDescriptorImpl
   40: [2.93M/58.7MB] org.jetbrains.kotlin.utils.SmartSet
   41: [2.91M/69.8MB] java.util.Collections$UnmodifiableRandomAccessList
   42: [2.85M/ 501MB] org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
   43: [2.83M/ 300MB] org.jetbrains.kotlin.metadata.ProtoBuf$Type
   44: [2.57M/ 123MB] org.jetbrains.kotlin.container.ImplicitSingletonTypeComponentDescriptor
   45: [ 2.3M/ 103MB] org.jetbrains.kotlin.metadata.ProtoBuf$QualifiedNameTable$QualifiedName
   46: [1.71M/82.1MB] org.jetbrains.kotlin.storage.LockBasedStorageManager$4
   47: [1.64M/26.3MB] org.jetbrains.kotlin.psi.KtImportInfo$ImportContent$FqNameBased
   48: [1.64M/39.5MB] org.jetbrains.kotlin.resolve.lazy.FileScopeFactory$DefaultImportImpl
   49: [1.63M/40.8MB] org.jetbrains.kotlin.resolve.ImportPath
   50: [1.62M/ 266MB] org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
[...removed 47919 lines...]
Total -        All:  593M 25.5GB 47969 classes (Total instances: 593834854)

Top 10 by bytes count:
    1: [44.2M/ 2.3GB] java.util.LinkedHashMap$Entry
    2: [17.3M/2.12GB] java.lang.Object[]
    3: [6.52M/1.51GB] java.util.HashMap$Node[]
    4: [40.6M/1.46GB] java.util.concurrent.ConcurrentHashMap$Node
    5: [27.2M/1.21GB] byte[]
    6: [9.89M/ 910MB] java.util.concurrent.ConcurrentHashMap
    7: [10.1M/ 740MB] java.util.LinkedHashMap
    8: [18.8M/ 678MB] java.util.HashMap$Node
    9: [1.44M/ 665MB] java.util.concurrent.ConcurrentHashMap$Node[]
   10: [26.8M/ 644MB] java.util.ArrayList

================
Projects open: 1
Project 1:
  Module count: 441
  Editors opened: 2. Counts by type:
   * 2 com.intellij.openapi.fileEditor.impl.text.PsiAwareTextEditorImpl[org.jetbrains.kotlin.idea.KotlinFileType]
0
IntelliJ IDEA 2022.3 EAP (Ultimate Edition)
Build #IU-223.4884.69, built on September 22, 2022
Runtime version: 17.0.4.1+1-b597.1 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.6
GC: G1 Young Generation, G1 Old Generation
Memory: 25008M
Cores: 10
Metal Rendering is ON
Registry:
    editor.focus.mode.color.light=323232
    ide.editor.tab.selection.animation=true
    editor.paint.empty.text=false
    ide.plugins.snapshot.on.unload.fail=true
    search.everywhere.settings=true
    ide.new.editor.tabs.vertical.borders=true
    ide.balloon.shadow.size=0
    editor.focus.mode.color.dark=323232

Non-Bundled Plugins:
    Shifter (1.9.6)
    EclipseCodeFormatter (22.0.203.000.0-Eclipse_2021-09)
    jclasslib (6.0.4.1)
    krasa.CpuUsageIndicator (1.14)
    com.github.camork.fileExpander (2.0)
    com.pinkfloyded.idea-file-path-autocomplete.idea-file-path-autocomplete (0.4.1)
    com.mallowigi (80.0.0)
    JProfiler (19.8)
    izhangzhihao.rainbow.brackets (6.26)
    com.chrisrm.idea.MaterialThemeUI (8.0.1)
    org.mallowigi.idea.MaterialThemeUI.LangAdditions (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.HighContrast (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.Extras (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.ProjectFrame (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.CustomTheme (2022.1.0)
    matt.idea.ide-open-1 (1.0)
    com.mnw.tabmover (1.4.1)

Kotlin: 223-1.7.10-release-334-IJ4884.69
0

Hello,

How is memory shared/distributed between all of these processes?

There is not an exact number. It is sum of consumption for each process. And Xmx is not the limit for every of them. Some memory is consumed by JVM staff, sub-processes etc. 

Do these all have their own independent memory usage?

Yes. 

how much do they all need?

There is no exact value. It depends on the project type, size and frameworks used.

 
At first, let's make IDE's Xmx less. 4G would be enough. If you allocate more space for the Java process, it does not run GC in time and more space is consumed. 4G is enough even for 2M lines of code with Gradle, Kotlin, and IntelliJ IDEA . 

Then, let's disable all non-bundled plugins in IntelliJ IDEA. They could have memory leaks or other bugs, especially when they are installed on the first EAP version of IntelliJ IDEA (not all plugins adopted their code base for new version). 

If the out-of-memory error still occurs, please capture a memory snapshot according to https://intellij-support.jetbrains.com/hc/en-us/articles/207241235-Reporting-performance-problems#memory_snapshot and upload it to https://uploads.jetbrains.com/ for analysis. 

 

0

Hi @Konstantin this is all really helpful, thank you! I had no idea that the IDE can handle 2M lines of code with 4GB. I must have a memory leak in a plugin as you are suggesting.

Please let me take some time to try different things based on your advice.

0

I'm thinking more about your advice about Xmx. Does this advice extend to the kotlin daemon and gradle daemon? Are you suggesting 20g might be too much for those and that there may be GC issues there as well?

0

Does this advice extend to the kotlin daemon and gradle daemon?

Yes, I'd configure lower Xmx for them too. GC is working simultaneously here. 

The basic troubleshooting steps could be like 

1. Build the project manually from the command line and make sure build is working (no memory leaks in the project itself). 

2. Build the project in a freshly installed IntelliJ IDEA with all defaults. 

3. Enable plugins one by one and check how they affect the build. 

0

Thank you again. One more question, because in addition to practical advice I'm also trying gain a deeper understanding.

You said:

If you allocate more space for the Java process, it does not run GC in time and more space is consumed

This made me curious so I started reading a bit about how GC works. I understand now that GC is triggered when different spaces (like young generation, survivor space, etc.) are filled up. So I understand that setting xmx too high will decrease the frequency of GC. But, to be clear, this couldn't cause a memory leak on its own right? Even if xmx is set way too high, wouldn't GC still eventually be triggered and clear up the memory to prevent out-of-memory issues?

If my understanding above is correct, it would seem there must be a memory leak in some plugin or something?

0

this couldn't cause a memory leak on its own right 
...
there must be a memory leak in some plugin or something

You are absolutely right. 

 

1

Thank you again. One more question, because in addition to practical advice I'm also trying gain a deeper understanding.

You said:

If you allocate more space for the Java process, it does not run GC in time and more space is consumed

This made me curious so I started reading a bit about how GC works. I understand now that GC is triggered when different spaces (like young generation, survivor space, etc.) are filled up. So I understand that setting xmx too high will decrease the frequency of GC. But, to be clear, this couldn't cause a memory leak on its own right? Even if xmx is set way too high, wouldn't GC still eventually be triggered and clear up the memory to prevent out-of-memory issues?

If my understanding above is correct, it would seem there must be a memory leak in some plugin or something?

yes man you are right 

0

I cannot simply disable all of my non-bundled plugins and add them back 1 by 1.

The issue is that the out-of-memory issue happens sparsely and is not easy to reproduce. I need my plugins to work.

 

I have disabled as many as I can. And I set `xmx` to 4g. However, I still get OOM errors occasionally. 

My plugins are now: 

 

 

Non-Bundled Plugins:
    com.github.camork.fileExpander (2.0)
    com.pinkfloyded.idea-file-path-autocomplete.idea-file-path-autocomplete (0.4.1)
    Shifter (1.9.6)
    com.mallowigi (80.0.0)
    izhangzhihao.rainbow.brackets (6.26)
    com.chrisrm.idea.MaterialThemeUI (8.0.1)
    org.mallowigi.idea.MaterialThemeUI.ProjectFrame (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.CustomTheme (2022.1.0)
    org.mallowigi.idea.MaterialThemeUI.LangAdditions (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.HighContrast (2022.1.1)
    org.mallowigi.idea.MaterialThemeUI.Extras (2022.1.1)
    matt.idea.ide-open-1 (1.0)
    com.mnw.tabmover (1.4.1)
0

I will update this thread as I come closer to a solution. Please allow me some time to experiment with disabling different plugins.

0

I can only guess here, any of them could have a memory leak. Moreover, IntelliJ IDEA or your project could have it as well. 

So, a bit more troubleshooting is needed (at least 3 steps I mentioned earlier): https://intellij-support.jetbrains.com/hc/en-us/community/posts/7913165395474/comments/7924499992978

 

0

Please sign in to leave a comment.