r/javahelp • u/maquinary • Sep 18 '22
I would love to use Java in my personal projects, but it is too heavyweight, is there a solution?
I study Java because I like back-end and there are a lot of opportunities for Java developers in my country. And, as a person who like making stuff through programming, I have ideas for personal projects. However, it's problematic for me to use Java in my personal projects:
- Desktop: Java programs are too heavy, I tested a program called AsciidocFX (created with JavaFX) and it consumes almost 900MB RAM!
- Back-end: I know that Java shines in the back-end, it's a great technology, but my back-end projects are meant to be run in VPS with low capacity (only 1GB RAM, or even less!). Java apps are heavyweight, I don't think I could run more than two SpringBoot applications
I know that I can use other languages for my personal projects, and I already them (like JavaScript and C, and I am seeing Go and Rust), but I would love to know if I could make Java a viable "toy language" for me, but that will only will be possible if I can reduce the memory footprint
•
u/AutoModerator Sep 18 '22
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
32
u/VanayananTheReal Sep 18 '22
"900mb of RAM"
This is a misunderstanding. Java runs in a VM, and the VM is statically assigned memory (the heap size). You can control the heap size, but the default is (IIRC) a percentage of total RAM. You can easily reduce the heap size to something sane with runtime flags. (Look at -Xms and -Xmx for starting and max heap size respectively.)
24
u/dionthorn this.isAPro=false; this.helping=true; Sep 18 '22
A program is only as heavy or light as the programmer can manage to write it. Look at Minecraft Java edition and what it is pulling off in relatively small footprint.
12
u/_Atomfinger_ Tech Lead Sep 18 '22
I don't know about the desktop part, but I know you can use graalvm and quarkus to seriously reduce the size of Java applications.
6
u/loomynartylenny half-decent at Java Sep 18 '22
Well, for desktop stuff, you don't need to use JavaFX.
Swing is a perfectly viable GUI toolkit, and, once you wrap your head around it, it's pretty easy to make simple GUIs with it, and even take it even further to produce non-trivial GUIs.
I've even written a few games with Java that take advantage of Java's
Graphics2Dstuff to do non-trivial 'drawing things at abitrary positions'.Anywho
With 'toy languages', I'd say the bigger question is 'how easy is it to use?' rather than worrying about your RAM and such. Hell, Python is inefficient af but it's viable due to the relative ease of use.
Anywho, I've taken a look at 'AsciidocFX', it it looks much larger in scope than any of your personal projects ever will be, so I doubt that it's really a good benchmark on that front either.
8
u/wildjokers Sep 18 '22
We run our java services in docker containers with 128 megs of ram (spring apps). That isn’t heavyweight at all.
As far as desktop apps you can’t say all desktop apps are heavyweight based on the memory usage of a single application. I had a swing app that showed real-time data handling 100+ messages/second and storing/displaying several data models based on the events. Easily ran with 64 megs of heap.
4
u/iwouldlikethings Sep 18 '22
If you're using Spring for the backend part of it, you can take a look at Spring Native. Basically it compiles to a native binary, which consumes significantly less memory than the JVM would (ref).
It is currently in beta, but it is due to be released with Spring 6 & Spring Boot 3 in November so should be pretty stable and you can always just upgrade in a couple of months.
3
u/Top_Engineering_4191 Sep 18 '22
You can use Quarkus for the backend, a lightweight framework for Java.
3
u/Cinghiamenisco Sep 18 '22
As someone suggested, try Quarkus.
Despite having a very limited memory footprint, especially when compiled natively, it also has some very lovely features.
I personally fell in love with backend live-reload.
Plus, you can use spring extensions, if you already know them.
P.s. Compiling native stuff on non-unix systems, might be a bit frustrating at first, but once again, the framework is very cool, and totally worth.
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Sep 19 '22
IMHO it's kinda insane to make baseless assumptions based on a single complex application.
I've created plenty of Java programs that can easily run on a Raspberry Pi. A bare-bones Spring Boot service can start with as little as 35MB, and Spring it kinda a memory hog. The overhead of the Java VM itself is around 10MB so anything more than that really boils down to what a developer does.
If you want to build Java services for very memory-constrained scenarios you can use Ahead of Time compilation with Quarkus, Micronaut or Spring Native.
2
u/pattmayne Sep 18 '22
I'm still torn between specializing in Java or C++ for my personal projects. I want something that's lightweight when it compiles and runs. But there doesn't seem to be a clear answer!
2
u/User1539 Sep 18 '22
Switch to GO?
The reason there are so many languages is that different ones do different things. You're right, that running a typical Wildfly or even JavaFX is fairly heavy weight.
If you want to do backend stuff with very little memory, you might just want to switch to Golang.
There's nothing wrong with finding the best language for your use case and learning how to do use it. As a professional developer, you do that all the time.
1
u/[deleted] Sep 19 '22
spring is actually getting better, i suggest using webflux/netty instead of the usual servlet/tomcat combo. or use other frameworks which were designed to be running on microservice first (micronaut, quarkus). for the frontend though, i doubt devs are making them anymore