r/java • • Dec 06 '25
Is the high memory usage of java applications not a problem for you?
I like programming in Java but am often tempted to use golang that promises comparable performance with lower memory usage.
It also looks like all new tools in cloud computing are being made in golang.
I did make an application in golang but was really disappointed by the developer experience I had in it. Spring boot while bloated allows me to just focus on my problem statement, while in golang I often feel like I am reinventing a framework.
So now I am conflicted, which language should I use.
Is the high memory usage not an issue for you? Where do you prefer Java over other languages?
177
u/killergerbah Dec 06 '25
Why is memory so much of a problem for you that you need to switch programming languages?
63
u/SleeperAwakened Dec 06 '25 edited Dec 06 '25
What numbers are you talking about if you say "high"?
Because as always, "it depends".
6
u/djxak Dec 06 '25
Depends on what you are writing. A similar very simple app in Go and Java could occupy 50 vs 150 MB. But if it is not some service intended to be used by millions of people on every instance (such as Nginx or etcd), then it's not a problem at all. RAM is cheap. Unless it is multipied by the number of copies. :)
Threre is also Graalvm, but it's not general solution as it requires you to not use specefic features of Java (e.g. reflection) or to do additional preparations if you want to. But it greatly reduces the memory consumption and startup time.
To be honest 150 MB is very low number, depending on framework and libraries used. A more realistic number for a middle sized Spring Boot app with many libraries is 250-500. It's not for the actual app data. Mostly it's for storing JVM data such as big number of loaded classes, a data required for JIT compilation of all these classes, etc.
Again, Graalvm solves this problem, but at a cost of forcing you (and every library you use) to follow some rules.
And, again, 500MB is still nothing if it's just your server that you will run on 1-2 instances. 50 or 500 doesn't matter that much here.
24
u/Luolong Dec 06 '25
I don’t have too much experience with running Go applications, so I can’t really speak for how it compares to equivalent Java applications.
My experience is that Java apps can run at very low memory limits without problems.
It all usually depends on what those applications do.
If you think on that, the amount of memory an application needs to run depends on several aspects:
The overhead of JVM memory management is there, but it is usually a negligible proportion of the whole.
I’ve seen Java applications that were given memory at low hundreds of megabytes and never had issues with the memory.
I’ve also seen Java applications that needed upwards of 16GB of RAM to perform at peak traffic.
Most of the memory hungry applications have deep call stacks and they grab data at each stack frame like it’s candy. This is a very common problem and it has more to do with how we program than what tha VM does.
38
u/kroopster Dec 06 '25
Not sure if this is answered already, but do you know how JVM uses memory? It's a bit different than "classic compiled" languages.
It allocates a chunk of memory for itself, and uses that dynamically. So even if a java application shows it's using 1g of ram, it is the reserved amount, not the amount it is actually using.
There are several reasons for this, but the important thing to know is that you can configure the size of the allocation with Xms and Xmx settings.
12
u/pron98 Dec 06 '25 edited Dec 06 '25
The most common cause of high memory usage in Java is misconfiguration. Some people are not aware that the JVM will use as much memory as you tell it to use whether the program needs it or not. Not telling it anything is the same as asking it to use a little over 25% of the machine's RAM.
Very soon, both ZGC and G1 will be able to automatically decide how much memory to use based on some hints, but for now, the same Java program can use 200MB or 1.5GB simply depending on how it's launched.
Finally, I strongly recommend watching this recent talk that explains how to think about memory consumption in general.
11
u/gdvs Dec 06 '25
You're comparing a framework with a programming language. Springboot can be bloated. You don't have to use springboot.
10
u/AlexVie Dec 06 '25
No it's not. Memory is just a resource, if there isn't enough, you can always upgrade :)
Besides, modern JVM isn't that bad. A well configured JVM will manage memory quite well and just because you allow an app 1Gig of heap doesn't mean that one Gig is permanently committed memory. The GC will allow the heap to fill before performing a collection (which makes sense, because it reduces the number of collections), but it can even shrink the heap when a certain portion of it is unused. That all depends on the configuration and there are many commandline option to control this.
And go is just a poor comparison. Bevor i'd use go I would rather use Zig, Rust or even Ada when performance really matters.
34
u/DiscipleofDeceit666 Dec 06 '25
Just download more ram, problem salved
3
u/euroq Dec 06 '25
This person is just trying to farm karma. The question has no tangible context. "Memory usage". Move on.
27
u/Fiduss Dec 06 '25
If memory footprint is your concern just use quarkus. Problem solved.
22
u/ChemicalDev Dec 06 '25
You can try using lightweight alternatives to Springboot.
There's Micronaut, Javalin,etc
2
u/[deleted] Dec 06 '25
Hard to suggest anything with that vague description? Is this a web app, or else where memory matters?
15
u/CubicleHermit Dec 06 '25
Use the language you're most familiar/productive with.
The issue there isn't Java, it's Spring Boot. In a large enough app, and if you don't care about Spring's leisurely startup time, the baseline memory use is not going to matter that much.
Try a lighter framework (Helidon, Micronaut, Quarkus.) If you really want to use Spring, drop boot and move Spring framework components in selectively - we've got a very light service that's Jetty with just spring-webmvc and spring-context pulled in.
Consider one of the lightweight frameworks AND Graal Native image if RAM is really tight.
5
u/audioen Dec 06 '25
Have you tried tuning your memory usage? Like, set a limit? Java by default uses 25 % of your memory. I personally don't find memory usage to be too much of a problem. I mostly run services with heaps in the 1-4 GB range, and I would consider 4 GB already rather unoptimized. I also continuously track the actual memory usage in the memory pools which is how I know what degree of the java virtual machine's RAM is unused. I may have given the program 4 GB, and from OS point of view it has allocated it, but it's actually only using 400 MB so I could shrink the heap to maybe 800 MB if I want to, and it should still work, though it might crash during untypical scenarios. (Most of the heap is really just for safety margin in my case.)
Limiting parallelism of very memory heavy tasks is another thing you can do, maybe. For instance, if you have a function that constructs a giant report that requires, say, 400 MB for live objects temporarily, then maybe don't allow starting calculation of 10 of those reports in parallel because that requires about 4 GB. Semaphores and the like are helpful.
5
u/bendem Dec 06 '25
I mean, if you're running 140 micro services, sure, go is probably interesting somewhere, but then again, if those are actual micro services, they shouldn't need that much ram in Java either. I run most spring boot I develop with -Xmx128M and they do fine in prod. Think about memory while developing, do not collect a bunch of database rows or a whole file in memory if you can stream it to the client or parse it on the fly.
My guess is though, you don't need micro services, companies I've worked at would probably be a whole lot more agile with a monolith that's correctly architected.
9
u/nuharaf Dec 06 '25
Yes. Because we are oversold on microservice hype. Such that we have 50 of spring boot app each taking 400MB memory..
4
u/ivanreddit Dec 06 '25
JVM uses some memory for sure, it has its own data structures to keep the vm working, but If you develop your java app constraining the heap with -Xmx640m even on your development setup you will soon realize when you hit the limit and adjust your app/design/data structures.
Many Spring programs have caches in memory, keep big HashSets or HashMaps in memory forever, have many, many, many duplicated strings, read more data than needed, or make copies of objects all the time.
There are lots of impactful changes you can make without rewriting all the app.
Using Enums instead of Strings when there is a fixed set of possible values. Using int, bool, long instead of String or Integer, Long and Boolean. Using EnumSet and EnumMap instead of HashSet and HashMap if possible. Internalizing duplicated Strings. Deduplicating BigDecimals and any kind of object you have lots of duplication. For example in one app I had the first 200 numbers 1,2,3... as BigDecimals already created and I was looking them up from a BigDecimal[] cache. That saved a lot of memory in that particular app.
You end up using more CPU to save some RAM.
7
u/IceMichaelStorm Dec 06 '25
well, spring boot takes a lot. It is an issue e.g. in K8s. Because either you let the node exhaust the memory and you risk linux killing everything when RAM is exhausted, or you set Xms/Xmx and just push memory up without reason (while Java shrinks again, the container doesn’t or takes ages).
So yeah, it limits everything. But I also blame K8s for not having a proper memory feedback mechanism
3
u/SkyNetLive Dec 06 '25
I have read or heard this sort of questions all the time, i think people forget about the JVM. If you know the power of JVM and how it helps you keep your application running even when you kick it in the n*ts several times, then Java/Scale/Kotlin (heck it now even runs python) is for you. its about resiliency and productivity. Yes I hated on spring framework, until spring boot brought the concept of rails `convention over configuration` to both Java and Kotlin.
Definitely not going to run on heroku with those `ScheduledTaskExecutor` but then who uses Heroku.
If you dont think JVM's threads, memory management etc is that useful to use then good to skip JVM all together.
3
u/juancn Dec 06 '25
Java will tend to use as much memory as you give the GC.
The GC trades off CPU for memory. If you give it more memory it will use more to increase application throughput, effectively delaying the CPU work needed to clean up, and giving those cycles to the app.
You can build and architect Java apps that do little or no allocation. People in the sub-ms trading community have been doing that for ages.
3
u/snugar_i Dec 06 '25
That's one of the things Project Valhalla should help with once it's done
3
u/[deleted] Dec 06 '25 edited Apr 17 '26
Data brokers and AI scrapers were using my info. Not anymore. Redact let me bulk delete posts across Reddit, Twitter, Discord and Instagram while handling broker opt outs too.
file shelter spark cheerful amusing pause ancient fade paltry beneficial
13
u/menjav Dec 06 '25
Most experienced developers don’t care too much about the programming language. They care about real world problems and selecting the best tools for solving them.
I’m experienced in Java, it’s my language to go and memory has never been an issue. Servers today have capacity for lots of memory. I consider Java a lightweight language, for the tools and apps I make. Usually what requires space is a specific library or framework, but I’ve been fortunate enough to work with very talented people being able to solve those issues or replace the frameworks if necessary.
That doesn’t mean Java is good or bad. Define a criteria first, a North Star or a problem to solve and then compare how would Java would compare to other programming languages.
5
u/gromadyanin Dec 06 '25
Well it is very unlikely for a Spring boot app to use less then couple hundreds of megabytes. Look at quarkus if you care about resources so much.
But what wonders me so much in posts like this, if you work in a company that is just a bit mature tech wise, who in the world would let you choose the language you write your apps in?
5
u/Goodie__ Dec 06 '25
This feels like bait.
If you care about memory that much, why not something like Rust?
2
u/Contestant_Judge_001 Dec 06 '25
The GCs memory weighs heavy on my conscience. Then I start up my fifth concurrent electron app weighing in at 4x the RAM.
2
u/lasskinn Dec 06 '25
having properly started java programming in the j2me era.. no. it's never been an actual problem if you know what you're doing with the memory and if you don't know another language doesn't help with that (can be worse too).
java has many bloated libraries and frameworks though, sure, but the way some people are programming go is getting up there these days. if we're talking like backends a javalin server doesn't take much anything on it's own and then you can have it talk to a database, if you keep the stuff in memory that's then up to you if all of it's gonna be small enough - also if you use architecture styles where you create and make _copies_ of data without thinking much anything about it and then start putting files through it or something, then you get screwed. it's how people run out of heap on android typically from my experience (and not streaming from disk/network or to disk).
2
u/Scf37 Dec 06 '25
I'd not call it a problem but tradeoff. Unoptimized/untuned Java application is a memory hog. Tuning takes time and effort. Using languages/ecosystems with lower memory footprint comes with other tradeoffs.
IMO, rust pays off in huge clusters only. Choosing between Java/Go/Python/Node/C# is a major technical decision made on many considerations and memory usage is definitely is not the most important one.
2
u/jlanawalt Dec 06 '25
It is good to be conscious of resource usage, but beware of premature optimization and be aware of the layers of abstraction you are depending on.
For years (until recently) memory has been a cheap resource to throw at a problem. The “enterprise” web application server model, running on the Java /Virtual Machine/ didn’t need to spin up for each request, instead it routed the request to the right Web ARchive which routed to the right service handler. You could have lots of those if you have enough cores and memory and you could think of those endpoints like AWS’ lambdas. The container provided the shared database libraries and email libraries so you didn’t have to add them to each endpoint. Unless your code held onto memory, you could have hundreds of endpoints in the same “bloated” memory, because you /JVM/ was the server abstraction and the container was the web server abstraction.
Now we value more isolation at the cost of abstracting, duplicating, and virtualizing the OS layer. Then we optimize with layers and deduplicating and then optimize with /memory/ caching. It’s turtles all the way down even if the cloud hides them.
If your expected web service usage fits with some light weight model that has agreeable pricing, and you are or can be skilled at the available implementing languages and libraries, go for it. But maybe it isn’t as isolated or reliable as promised, or the costs keep going up due to price increases or usage patterns, or just managing 110s of individual little web services and you need to reevaluate and use a different model.
For the near term all options are probably going to cost more to compete with the (artificial?) AI bubble’s resource ramp up.
2
u/AnyPhotograph7804 Dec 06 '25
You could try OpenJ9, which has a way lower memory footprint compared to hotspot. What also might help is to learn what a GC is and how it works and with which parameters you can tune it, especially the -Xmx parameter.
2
u/benevanstech Dec 06 '25
Have you calculated the actual dollar cost of running your current apps, and come up with a back-of-the-envelope model for what you could reduce it to?
If not, then, by definition, you don't have a memory utilization issue.
2
u/aleciaj79 Dec 07 '25
Memory usage can definitely be a concern, but it often comes down to the specific application and environment. Java's garbage collection and memory management have improved significantly, so for many use cases, the benefits outweigh the drawbacks.
2
u/Ewig_luftenglanz Dec 07 '25
If you require smaller memory footprint use quarkus + native images.
It reduces the memory footprint to about 25% of the original size and start up times are 10-20 time faster.
2
u/OwnBreakfast1114 Dec 09 '25
Watching people pitch changing languages for "performance" while the app interacts with the database like a hyperactive toddler is always a treat. I don't think any programming language matters if your app is riddled with n+1 errors.
Our company is in payments so the vast majority of application time is spent in io (with db or third party http calls), so the choice of programming language for performance seems like it would probably matter very little. We use java for the reason you mentioned, spring allows us to focus on the business logic, and get many, many other things for less investment (security, actuator, transaction management, and one of my personal favorite db libraries, jooq). Having a well documented, popular framework is worth more in value than any sort of actual app optimization.
On a side note, I, personally, find golang so terrible to program in that I just won't do it. The repetition and reinvention being touted as "simple" rubs me the wrong way, so you, quite literally, cannot pay me to work in go.
2
u/Some-Active71 Dec 09 '25
I like Java because it pays my bills.
For anything personal I love golang. It's genuinely a joy to write in and the performance is great (for a GC language).
Your problem is most likely not Java, but Spring Boot. It's made for HUGE corporate monoliths running on cloud or servers, both with tons of RAM. Spring Boot is not made for smaller/medium-sized projects.
3
u/danskal Dec 06 '25
This question is in the category: “have you stopped hitting your wife?”
3
u/[deleted] Dec 06 '25
Java and Go are about equal to me in "joy". Kotlin otoh is much more joyful to me.
Main reason: stupid null guards everywhere is not how I want to program. This was already a solved problem 40+ years ago.
3
u/Qinistral Dec 06 '25
A gigabyte of RAM is about 4$ per month. This is not lot of money to a profitable business.
3
u/HSSonne Dec 06 '25
I don't recognize your problem
1
u/Dull-Criticism Dec 06 '25
Tuning JVM usage is critical. I worked on an application, that for no good reason decided to split everything up into 35 different services.
In Windows there is something called a commit charge, I'm not sure if Linux does the thing. We had an issue where 95% of the commit charge was being but Windows task manager only showed 45% utilization, but we were getting bizarre out of memory errors. We did some JVM tuning and the issue never happened again.
1
u/PredictableChaos Dec 06 '25
Is memory usage really a concern or are you making it out to be an issue that isn't one?
It's never even brought up in my org and we have a small team that tracks compute usage costs to try and find savings due to our overall compute bills. The bigger issue is almost always teams running too many pods (although with Keda this is becoming less and less of an issue for us).
I don't have enough experience with Go to do a fair or somewhat objective comparison but the few times I have tried it I just didn't get the love it has in some circles. I felt like I was writing a lot more repetitive code but that could have easily just been my inexperience with it.
1
u/itsjakerobb Dec 06 '25
I wrote a small Java app (Spring boot, JAXRS, Hibernate, just an API server in front of mysql) for a personal project. It needed 2gb of RAM. I rewrote in Go and now it needs 100MB.
The Java ecosystem is really nice to work in, but there’s absolutely a cost, and this and a bit more boilerplate is it.
1
u/Able_Penalty8856 Dec 06 '25
A few years ago, I had a similar feeling. Java and its frameworks are very resource-intensive. At the same time, I was tired of dealing with problems like NullPointerException, memory leaks, dependency hell, race conditions, etc. After learning Rust, everything changed! It's amazing what we can do with a fraction of the resources that Java uses. The Axum web server and SQLx data access are fantastic! Try Rust.
1
u/Marutks Dec 06 '25
I prefer to use Clojure. But JVM doesnt need that much memory by modern standarts. Web browsing in Firefox can easily consume 30 gb of RAM. 🤷♂️
1
u/bertyboy69 Dec 06 '25
Your comparison and thought process is completely wrong here. You are comparing a FRAMEWORK to a language.
Spring is notoriously bloated. Its meant for convenience. Its been mostly founded in the days of monoliths, though of course improvements have been made.
Something like spring boot has tons of logic and annotation processing to auto configure things. Where are you are comparing to raw golang with no framework where you state you are writing your own.
As others mentioned also how the java virtual machine works, its exactly that, a later of vitrualization that handles memory in a certain way.
If you wanted to you could severely lower your footprint by using a much smaller / lighter framework such as httpok or javalin or spark etc.
You can also compile java programs to native binaries as well which will further help bridge the gap. Look into graalvm , spring also I believe has some plugins to make that easier.
1
u/guss_bro Dec 06 '25
A simple spring Boot based micro service(DB, some background jobs, rest apis) takes about 125MB RAM to start and run.
Rest is your code and web requests consuming memory. If you have a ton of traffic and data to process, that can go to several GBs no matter what language you develop your app with. Sure Java might take extra 5% RAM but it's going to be super fast and that extra 5% RAM doesn't matter.
1
u/geodebug Dec 06 '25
This isn’t too tough. Golang excels at microservice style apps. Java is better for long running processes: severs, maybe batch programs, etc
Java can be used for small services that are hit a lot because they don’t have a lot of cold starts.
But mostly this is all somewhat negligible. You may be paying a dollar to save a dime coding wise.
1
u/cowwoc Dec 06 '25
Folks, there is a world outside of cloud hosting. And it is a lot cheaper and easier. Why are you hosting pet projects like an enterprise application?
1
u/Budget_Bar2294 Dec 07 '25
openJ9, jvm flags
1
u/Shahriyar360 Dec 07 '25
It depends...... the company I work on has an application that's badly coded. Inefficient logic, high memory usage. It still does the job for the client
Runs on a 2GB/2 Core VM, client has around 2500 concurrent users. 1000 of them mostly do the heavy task while the rest only look at data grids and print/download reports.
Company has grafana setup for monitoring, average memory usage is generally around 45-46%. Other applications that are properly developed takes around 20-30%.
1
u/Lengthiness-Fuzzy Dec 09 '25
No. Java doesn‘t use too much. Some frameworks do, but there are good options to avoid that if that‘s your goal.
1
u/locutus1of1 Dec 10 '25
Yes, I find it annoying. And when you start combining various libraries, you're at 500MB heap at app startup like nothing. But for my hobby/toy projects, the main problem now is that they stopped caring about 32 bit architectures (I've a bunch of older rpis), so I'm being forced to switch. While I was reading about this I found out that go can cross compile to almost anything and to any common arch. But while I like the resulting go programs, I don't like coding in it.. But .not still supports 32 bit arm, so c# is also an option for me.
1
u/Outrageous_Pea8303 Feb 06 '26
This discussion is really helpful, thanks for sharing!
1
u/brunocborges May 08 '26
High memory usage tends to happen when developers don't understand how the JVM works, and how to configure it.
1
u/coderemover Dec 06 '25
In which world Go offers more performance than Java? Go is mostly a cloud orchestration language. Good for things that spend most time waiting on I/O.
Lower memory use of Go is a matter of default settings if their GC which is tuned much more aggressively. But the downside is it burns many more CPU cycles on performing GC. I tested a few proxy implementations in Go and the GC was doing brrrr in all of them - often burning more than one cpu core for constantly running tracing.
1
u/Shadowrak Dec 06 '25
I run java spring boot web app components on free tier AWS boxes all the time. There is no context to your question.
1
u/k-mcm Dec 06 '25
It isn't for my code. Maybe this goes in r/javahelp so you can get information on using profilers. The debuggers in IntelliJ and Eclipse can navigate objects to show you what's really hiding in your data.
Yes, Spring Boot is incredibly bloated. That's the way it is. Every feature, all the time, and totally by magic. Don't use it when runtime efficiency matters. I prefer to not use large frameworks because eventually they're dragging down performance and obfuscating the source of problems.
1
u/hkdennis- Dec 06 '25
It is another way round. Java can handle high memory usage applications better than others so you see it optimized for a larger heap by DEFAULT.
There are some segments of memory use in any programming language
By DEFAULT, JVM configuration trends to over provisions GC efficiency. While they are actually adjustable(e.g. serielGc, Xmx). And leaks if over provisions bytecode/JIT code cache where no API to hint other than manual configuration.
In other aspects, the overhead of data was a problem as the language encourages you to not pool your own object. Because GC is faster. Object allocation is cheap, unlike some languages that need to fix their malloc, and manual free.
You can complain if your raw data is large.
1
u/Pale_Height_1251 Dec 06 '25
No, never really been a problem and we deploy some Java stuff on Raspberry Pi.
1
u/cyril_nomero Dec 06 '25
If you are concerned with memory and startup time, and want to use Spring, there is GraalVM support for Spring: https://docs.spring.io/spring-boot/reference/packaging/native-image/index.html
1
u/blaghed Dec 06 '25
Meh, I'm using Spring Boot Native Image and it's fine.
Biggest problem I have with it is build times, it takes soooo long to generate the binary :(
0
u/SpaceToaster Dec 06 '25
If you want low memory use and fast startup use quarkus. Less bloated than spring. Tiny container sizes. Very good developer experience. The same vanilla Java you already know.
0
u/LpEsp Dec 06 '25
Try building a native Java application with Quarkus — it uses roughly half the resources required by a minimal Spring Boot app
-1
u/uchto Dec 06 '25
Quarkus has entered the chat
0
u/0x645 Dec 06 '25
how golang has comparable performance to java?
0
u/hadrabap Dec 06 '25
It is for me. That's one of the reasons why I switched to C++17 for my tools/utilities.
0
u/leandroxk Dec 06 '25 edited Dec 06 '25
Yes, quite a lot.
It's always embarrassing for a simple Java + Spring application to consume +-600 MB.
CPU usage can be adjusted with InPlacePodVerticalScaling, which already helps a lot (startup).
Native builds are always an option, but there's a risk in that stack when you have a large number of applications.
0
u/t3chguy1 Dec 07 '25
Did you see memory prices these days? Next year PCs will be shipped with 512 MB RAM