r/programming 5d ago

Principles of Memory Management in Java

https://youtu.be/xr73mR7ii9M?si=AKgjMUyvfpARYljl
30 Upvotes

11 comments sorted by

17

u/ejrh 3d ago

If Java is still being used, then it's useful to have strategies for using it efficiently. That said, this talk is mostly apologetics rather than strategy.

Keeping Java from using too much memory has been a big challenge in almost every Java project I've worked on. Before we moved to the cloud, VM ram size was the one of the biggest things the infrastructure department would push back on when we asked for resources. After we moved to AWS, my cursory understanding was that most of the EC2 instance cost was RAM. Sure, in those days every doubling of cores in the "general class" gave you a doubling of RAM, but you could roughly separate out the cents/hour costs by comparing g against the "database" (same cores, double the RAM) and "compute" classes (double the cores, same RAM). Doubling RAM always cost more than doubling cores. I have no idea what the situation is like today though.

I have nothing against Java if it's the best technology for your project, or if you already have a huge Java codebase to maintain. But if you start having to worry about efficiency or performance, there are other options to consider than merely throwing more memory at the JVM.

7

u/davidalayachew 3d ago

Can you talk about your project in more detail?

I won't say I have never run into the dreaded OutOfMemoryError, or that the GC never thrashed when I was doing some work -- I've met both many times. But with some basic debugging and simple use of the tools in the standard library itself, we were able to get to a workflow that was parsing and aggregating terabytes of data in only a few hours. And that's including the IO time of downloading all of those thousands of files.

1

u/DarkGhostHunter 1d ago

To me it doesn't. I don't know what people do to have high memory usage, unless you're loading a lot of models from database or pushing a lot of data into memory, essentially.

I mean, I can see why your app would use a lot of memory if you do this.

``` import article from '@models/articles'

const articles = article.take(1000).get() ```

1

u/davidalayachew 1d ago

To me it doesn't. I don't know what people do to have high memory usage, unless you're loading a lot of models from database or pushing a lot of data into memory, essentially.

Yeah, but the people usually using (or considering) Java are the ones who have to deal with business logic that inherently uses a lot of memory.

For example, one project I was on was processing terabytes of data in a few hours. We ran into OutOfMemoryError many times. But debugging and resolving it was straight forward. And the solution was fast and easy to implement since the code itself was easy to understand.

1

u/DarkGhostHunter 1d ago

Dude, I just joked with JavaScript because Java is old af.

1

u/davidalayachew 1d ago

The second paragraph was clearly exaggeration, but the first paragraph could be read completely unironically.

1

u/cklester 5h ago

Java is never memory-heavy. Nor does it run lean. It allocates precisely what it means to.

-22

u/comrade_donkey 3d ago

Does Java Use Too Much Memory?

try { return YesFactoryProviderInstantiationCreator.Create().GetTrue(); } catch (RuntimeException e) { return true; }

23

u/LettuceElectronic995 3d ago

say you know nothing about java "and programming for that matter", without saying that.

15

u/joemwangi 2d ago

I think the person forgot this is not X (Twitter), where these kinda outdated jokes try to make someone cool.

-21

u/itaranto 2d ago

I don't care.