Good morning, recently I have been having some problems with Minecraft and the use of ram, every time I try to play a minecraft modpack, in any version and any mods, it always uses the maximum allocated ram and a little more, for example, I allocate 4gb, minecraft ALWAYS uses 6.5gb, I can be on the loading screen or in the game, always the same amount. I have 16gb of memory, so the problem is when I play very heavy modpacks and I allocate 8gb ram, it always uses 10gb of ram, and sometimes I have freezes. I've tried to reinstall java, use multiple arguments and multiple clients until the original minecraft, but the error persists. In the game the usage is shown normally, however, in the task manager it is always more than allocated.
The problem is the game shows 3gb of ram use and the task manager shows 10gb and never down!
Now I'm using multimc and it shows the same, i don't know what to do.
This is normal, and is why you should never allocate more than half your free memory to the game (more or less, the overhead is relatively constant, about 2-2.5 GB from what you say); not only can you force your system to page to disk, which will cause lag spikes, but garbage collection will also take longer, again causing lag spikes (or more noticeable/longer spikes, ideally they are short enough to not be noticeable):
2. Why not give it more memory?
Let's give Minecraft 4 GB of RAM to play with. This would need a PC with at least 8 GB RAM (as the real memory usage is almost double the memory visible in Java). If the VM decides to use all the memory, then it will increase the time between the garbage collections (20 sec instead of 4), but it will also increase the garbage collection time by 4, so every 20 seconds there will be one massive lag spike.
This also explains why you shouldn't allocate as much memory as you feel like; if the memory usage (in F3) only goes to say, 3 GB then 4 GB should be enough.
Also, what are your JVM arguments? Do you have -Xms or just -Xmx? -Xms will force the JVM to use all the memory available if it is equal to -Xmx, even if it isn't needed (if you do use it it shouldn't be any higher than the normal memory usage; the main benefit is during startup, as otherwise the JVM will resize the heap multiple times, slowing it down).
That thread may be of use to you. What you allocate to it isn't the total limit of what the entirety of Minecraft can and will use; it will be more. However, such high use wasn't normal, though it happened more than once, as well. I only had it actually get to 10 GB and complain of actually being out of memory that once though, and I then raised my page file, which is what I suspect was the issue, so make sure you're not disabling it (do NOT do this) and if you're setting a smaller static size than what Windows usually defaults to, with today's programs and RAM amounts, make sure it's still enough.
I've saw about that and i don't allocate more than 8gb ram, but the problem are in minecraft ALWAYS use max ram allocated and more, if i allocate 3gb, the game will use 4/5gb all the time, creating a world or playing in other dimension, the ram never changes and stay on more than max.
I'm don't using any argument, in the multimc i say to allocate 512 to minimun memory and 8096 to max.
Well, I can't say what's going on unless it's pre-caching or something. The old statement is "unused RAM is wasted RAM" so maybe it's using up to the limit regardless of the minimum it needs. I'm not sure.
Regardless, the question then becomes "is this a problem to you?" If you need it use less RAM, allocate less. If it's enough, then all is well and allocate less. If it's not enough, you don't have enough RAM then.
Is the freezing lasting multiple seconds while reflecting maximum RAM use, only to drop after the garbage collector saves it from a crash? Or is it more like stutter or micro stutter?
If it's the first one, that is strange, and I've got no answers for it. The obvious answer would be "raise your RAM allocation" but you say Minecraft chases the amount you set, whether it's increased or decreased, and then behaves this same way (freezing from being at max RAM use) regardless? Yeah, that is strange.
Does this also happen with vanilla, or only when running all of those mods? Start there to see if it's an issue with the game, or maybe a memory leak with one of the mods.
Stutter during garbage collection is normal; the severity of it varies though.
You've identified RAM use is higher with mod packs with hundred of mods. That is normal too.
You need to find out if you simply need to allocated even more RAM (if you have 16 GB, I'd caution against allocating more than 8 GB), or if there is a particular bad mod or few causing runaway RAM use. Unfortunately, that is beyond my knowledge. You might just need more RAM and RAM allocation for that mod pack.
As I've mentioned before, the memory allocated to a Java application (using -Xmx) is only a portion of the total memory used by the entire Java process, which can be thought of like running a Windows VM on Linux or vice-versa, and indeed is a virtual machine itself (in other worlds, the JVM (Java Virtual Machine) is sort of like an operating system that manages the actual Java application). This also includes not just the VM but any native libraries that the game uses, such as OpenGL and all the rendering data it handles (which can be quite a lot - at any time there can be millions of blocks being rendered, with each vertex, 4 per face, requiring multiple bytes). Java applications can even directly allocate memory outside of the VM, usually for communication with native code, which is only limited by physical RAM. While the actual overhead varies the creator of Optifine says it is around double the memory allocated to the Java process (presumably this overhead is relatively constant, you say you were seeing 6.5 GB of usage with 4 GB allocated and 10 GB with 8 GB allocated, suggesting 2-2.5 GB, which would double the overall usage with the default of 2 GB).
Modern modpacks are also incredibly demanding due to changes made to the game over the years, in addition to ineffficiences present in modern modloaders and mods themselves (Forge uses a lot of memory and resources by itself since it was designed to allow mods to indirectly hook into the game's code, which includes a lot of memory allocation for dispatching "events", as opposed to "jar mods" which directly modified it and were thus often incompatible with other mods, but could also be far more lightweight. Many modders also do not spend much time on optimizations; I've seen code that is quite bad in terms of object creation):
I don't have any experience myself but I see 4-6+ GB being quoted for reasonably sized modpacks (example1, example2), and that's just the Java heap - in short, you need a very powerful (at least RAM-wise) system to run modern modpacks and this will probably only worsen over time (1.8 was the worst update due to giving every single block and block variant, up to thousands for some modded blocks, its own block model which requires a lot of memory).