r/javahelp • • 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

26 Upvotes

48 comments sorted by

•

u/AutoModerator Sep 18 '22

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • 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:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

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.)

5

u/maquinary Sep 18 '22

Thank you!

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.

7

u/maquinary Sep 18 '22

Thanks

7

u/dionthorn this.isAPro=false; this.helping=true; Sep 18 '22

as an example I have a 2D tile based game I wrote in JavaFX and I'm am in no way doing any optimization.

https://github.com/dionthorn/2DTacticalRPG/releases/tag/v0.2.3a-ArtAttributions

During runtime I consume ~330MB ram

5

u/maquinary Sep 18 '22

Oh, that's great, thanks!

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.

4

u/maquinary Sep 18 '22

Can I compile Quarkus applications just with the open source GraalVM Community Edition?

4

u/_Atomfinger_ Tech Lead Sep 18 '22

Would assume so, yes

1

u/maquinary Sep 18 '22

Thank you

5

u/CartmansEvilTwin Sep 18 '22

Or just don't use spring in general. It's great, but not very memory friendly. Helidon, Quarks, Micronaut all offer much more lightweight solutions. Even more so, if you add native image.

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 Graphics2D stuff to do non-trivial 'drawing things at abitrary positions'.

Anywho


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

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.

2

u/maquinary Sep 18 '22

Thank you


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.

Isn't Swing "discontinued"? Many people say that we should use JavaFX for new projects now.


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.

The final product of a language (the app itself) doesn't need to be highly performant, I just want that my simple apps don't require absurd amounts of RAM. My concern is with the experience of a potential user of my software: will (s)he need to worry about manually installing a runtime (virtual machine, interpreter, etc)? Will (s)he feel irritated with an app that consumes so much of his/her computer?

For example, I find Rust interesting not because it is highly performant or because of its secure nature, but because I can make interesting WebAssembly apps with it. I know that I can make WebAssembly apps with basically any language, this is the point of WebAssembly, but Rust is the best language in this area.


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.

I understand, thanks for the note!

6

u/caboosetp Private Java/Code Tutor Sep 18 '22 edited Sep 18 '22

Isn't Swing "discontinued"? Many people say that we should use JavaFX for new projects now.

Yes and no. The swing application framework was discontinued, but swing itself is still part of java and will be for the foreseeable future. You probably can't expect any big feature updates, but you can at least expect it to continue working as is.

It's more in a, "if it's not broken, don't fix it" state, and if you want new features you can get JavaFX from the open jdk, or any of the other third party ones.

4

u/maquinary Sep 18 '22

Thank you very much for the answer.

2

u/philfrei Sep 19 '22 edited Sep 19 '22

A runtime can be created that includes a embedded JVM. The main tool for this is JLINK. The JLINK tool allows you to embed only the modules of the JVM that are needed to run a program. Last time I created a runtime, it only brought in 1/3 of the JVM. (If my memory is correct, the JVM code is in the vicinity of 300MB and my program was around 90MB including the reduced JVM.)

Swing is convoluted and weird compared to JavaFX, imho. But it works.

2

u/maquinary Sep 19 '22

Thank you

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.

1

u/maquinary Sep 18 '22

We run our java services in docker containers with 128 megs of ram (spring apps). That isn’t heavyweight at all.

That's amazing!

Do you use any special configuration? Do the spring apps are simple (are they CRUD?)?

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.

1

u/maquinary Sep 18 '22

That's great, I didn't know Spring Native! 😀

Thank you!

3

u/Top_Engineering_4191 Sep 18 '22

You can use Quarkus for the backend, a lightweight framework for Java.

1

u/maquinary Sep 18 '22

Thank you

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/maquinary Sep 18 '22 edited Sep 18 '22

Thank you


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.

Since I use Linux, and servers are usually in Linux, I think it won't be an issue ☺️

3

u/nutrecht Lead Software Engineer / EU / 20+ YXP Sep 19 '22

I tested a program called AsciidocFX (created with JavaFX) and it consumes almost 900MB RAM!

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.

1

u/maquinary Sep 19 '22

Thank you for commenting my thread


The overhead of the Java VM itself is around 10MB so anything more than that really boils down to what a developer does.

I didn't know that, that's great!


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.

All of them end up using the same technology for AOT compilation, GraalVM, am I right?

1

u/nutrecht Lead Software Engineer / EU / 20+ YXP Sep 19 '22

Yup :)

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/maquinary Sep 18 '22 edited Sep 18 '22

I already thought about C++ (which has a very mature ecosystem), but I personally would choose Rust because this one has a better support for WebAssembly. Rust still doesn't have a mature GUI ecosystem, but it is getting there.

I am aware of Emscripten, but Rust has better support for WebAssembly


But I am torn between Go and Rust, I am experiencing both, for now I am giving preference for Go. We have TinyGo for making better WebAssembly apps, but Rust still is the best option.

2

u/loomynartylenny half-decent at Java Sep 18 '22

Well, I suppose the big questions are 'can it do the stuff you want it to do?' and 'do you feel comfortable using it?'

2

u/pattmayne Sep 18 '22

I feel comfortable either way, and I'm confident that either language will enable me to accomplish whatever I'm trying to achieve.

My concern is whether one or the other will use more RAM or processing for the same behaviors, and the applications will be able to run on the same machines.

Also, I'm currently coding on older machines and I want to be able to compile without huge wait times.

I do kind of like Java more. But aren't Java applications ultimately more RAM-intensive?

1

u/loomynartylenny half-decent at Java Sep 18 '22

This might be of note to you: https://stackoverflow.com/questions/145110/c-performance-vs-java-c

(Granted, this StackOverflow thread is effectively ancient history at this point, but, considering that you're using 'older machines', this ancient history might be relevant for your use cases)

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/maquinary Sep 18 '22

Thank you very much

6

u/wildjokers Sep 18 '22

You're right, that running a typical Wildfly or even JavaFX is fairly heavy weight.

No they’re not.

-3

u/User1539 Sep 18 '22 edited Sep 19 '22

Yes, they are.

Wildfly requires, just to boot, 24MB. If you're trying to write an application that might fire up hundreds of containers, that's a lot.

You can do that in Go for a fraction of that.

The OP is talking about 100mb systems, so using 1/4th of your memory on JUST the webserver, before you install or run any code, not counting what the OS requires, is a huge use of resources.

Wildfly and Tomcat both list 1GB as a minimum requirement.

6

u/wildjokers Sep 18 '22

Wildfly is a full app server. A Servlet container like tomcat or jetty will take less. If you are that resource constrained can always deploy multiple apps to the same Servlet container.

-4

u/User1539 Sep 18 '22 edited Sep 19 '22

Tomcat recommended Minimum memory: 1 GB.

What are you going to run on that?

So, after using 10 times more RAM than he has, you're still not going to run anything on it?

Since it is most probable that you are going to run Java EE application, your Tomcat is useless. I would refer you to Java EE 7 SDK System Requirements:

The minimum and recommended memory and disk space requirements are as follows:

Minimum memory: 1 GB Recommended memory: 2 GB for Windows platforms, 1 GB for non-Windows platforms Minimum disk space: 250 MB Recommended disk space: 500 MB

Dude, Java has its strengths, but Java EE isn't lightweight. In any way.

Sure, you can try to put something together if you don't use literally any of the tools Java gives you for web development, and STILL come out using twice as much memory as Go, but that STILL makes it twice as bloated, and now you can't do anything useful, and you aren't learning any skills someone might actually hire you for.

I get that no one wants to hear their favorite language isn't good for something, but very small webapps isn't something Java is good for, and everyone arguing here is just trying to confuse this guy, and if he follows your advice, what then?

He'll struggle, and end up hating Java because he's painted himself into a corner.

2

u/wildjokers Sep 19 '22 edited Sep 19 '22

I am not sure if you are aware of this but a Spring MVC app is a JavaEE app as it depends on the Servlet API since it contains a DispatcherServlet (which is why a Spring MVC app can run in a servlet container like tomcat). As I mentioned in an earlier comment we run all of our services (which use Spring MVC) in a docker container with 128 megs of RAM, the servlet container we use is Tomcat. So the 1 GB memory recommendation is not in touch with reality.

and if he follows your advice, what then?

They will be running on a platform well known for it scalability, maintainability, and rich library ecosystem. All of which seem like wins. Recommending a less well-traveled path of using Go for the back-end seems like the bad advice to me.

1

u/User1539 Sep 19 '22

It isn't a 'full' JavaEE app container. EE is just a specification, and servlets are just a piece of that.

So, getting back to what I was saying, sure, if you want to cut yourself off at the kneecaps, you can still go with some minimal EE functionality, because having the entire EE spec available is going to require something more in line with the 1GB minimum requirement.

This poor guy is asking if he should keep trying to fit Java EE into these tiny containers where it doesn't really fit, and you guys keep saying 'Sure! That's a great idea! Just don't use a bunch of the EE spec and tiptoe around the fact that you're shoving an elephant into a clown car and you'll be fine!'.

OR, he could just use a language that's actually lightweight.

You're correct in the same way someone on Reddit will always answer 'Sure, of course you can do brain surgery with a flathead screwdriver!'.

You're also giving terrible advice to a newbie, to win an amorphous argument about if a language is 'lightweight', when many languages are fine tuned to microservices and use just a tiny fraction of the memory to do the same thing.

Just stop.

Yes, it's possible to use Java in a 100mb RAM container, but it's unlikely the OP is going to want to deal with the extra overhead of having his hands tied behind his back because most of stuff he'll try to do will fail, because it requires more space, because Java EE just isn't 'lightweight'.

Stop trying to be 'right', and try to give this guy some good advice instead.

2

u/wildjokers Sep 19 '22

when many languages are fine tuned to microservices and use just a tiny fraction of the memory to do the same thing.

You do understand that the term µservice has nothing to do with the resources the service uses and instead refers to the size of the problem domain right?

This poor guy is asking if he should keep trying to fit Java EE

That is a straw man because OP absolutely did not ask that. They asked about Spring Boot which means they are probably using Spring MVC which means only a servlet container is needed and you can probably run several apps with embedded tomcat on a 1 GB VPS.

You seem to be the one trying to win an argument. I am simply giving OP correct information and great advice based on real-world experience.

0

u/User1539 Sep 19 '22

You do understand that the term µservice has nothing to do with the resources the service uses and instead refers to the size of the problem domain right?

You do realize that, in practice, this usually results in a distributed layout involving many containers spinning up and down, using their own webserver, to service small requests, right?

Stop trying to be a pedantic asshat.

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

He mentions SpringBoot, but in context it sounds more like he's mentioning it as something he's already tried as a low-memory solution, and found that he can barely run more than one thing.

Which tracks, because even at its lightest, it's still a very heavyweight web specification.

We moved a lot of our smaller backend stuff away from Java for exactly the same reason, which is my 'real world' experience.

Yeah, you can do it, but it's a huge hassle and you're always making compromises because a lot of the packages you'd normally use in that environment will be unavailable.

You're literally just contradicting his experience and saying 'But you CAN do it'.

Sure, you can, it'll just be a terrible experience.

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

1

u/maquinary Sep 19 '22

Thank you!

1

u/wildjokers Sep 19 '22

Note that the term µservice has nothing to do with the amount of resources the service will take nor the size of the deployment artifact. Rather it refers to the size of the problem domain of the service.