The Joel on Software Discussion Group (CLOSED)

A place to discuss Joel on Software. Now closed.

This community works best when people use their real names. Please register for a free account.

Other Groups:
Joel on Software
Business of Software
Design of Software (CLOSED)
.NET Questions (CLOSED)
TechInterview.org
CityDesk
FogBugz
Fog Creek Copilot


The Old Forum


Your hosts:
Albert D. Kallal
Li-Fan Chen
Stephen Jones

Why does Java eat so much RAM?

JVM is a feeble excuse; there are number of programming languages with virtual machines out there. They are fast and less memory hungry (Python comes to mind).

I'm scared of applications developed with Java. Run them for two hours and they will be eating all your memory. NetBeans has set a high score yesterday, consuming (I don't blame NetBeans, because my taskbar showed the process as java) 250Mb of the 512Mb of memory my laptop has.

What kind of memory operations could the virtual machine be doing at the time? I didn't even write something long, just explored examples and modified them.

I sometimes wonder if the "heap is cheap" philosophy of Java isn't a bad one.
JavaShouldBeLessRamHungry
Wednesday, May 07, 2008
 
 
>I sometimes wonder if the "heap is cheap" philosophy of Java isn't a bad one.

Obviously not, based on Java's popularity.
You have only 512 MB of RAM on your laptop?

Wednesday, May 07, 2008
 
 
Why don't you just use Python then? Is it because overall Python is much slower???
anon
Wednesday, May 07, 2008
 
 
hummm.. first Python is significantly slower and eats much more RAM than Java, except in very trivial cases.

second, the problem isn't so much Java that the programs written in it. for example, how much RAM does the latest Visual Studio IDE eats on your machine ? to me, it has always been more than Eclipse after a long running session.

Java encourages a coding style where you allocate a bazillion little objects to do anything. it is built on the idea that RAM is cheap and garbage easily collected. there are also a few features in the language that increase its memory usage for very little practical utility (e.g. you can synchronized on any object => you to reserve space on *any* object for some lock bits even though you'll only use these on less than 1% of all objects allocated).

it is possible to write memory-conscious Java programs, but this requires a lot more effort than using the standard Java libraries, so most people don't do it...
Nia Nia
Wednesday, May 07, 2008
 
 
OMG PYTHON IS TEH RULEZ...If only everyone would just use Python, which is far superior!

Wednesday, May 07, 2008
 
 
I don't get it. So actually the heap is cheap should use less memory? Why half my RAM is being consumed then?

I'm afraid of launching NetBeans. And it was the same thing for SQL Developer (Oracle). Visual studio express 2005 doesn't consume much. I can run it for hours and it will remain fast and smooth.

What's wrong with having 512Mb of RAM? Didn't see any reason of upgrading (until now I guess...)
JavaShouldBeLessRamHungry
Wednesday, May 07, 2008
 
 
"(I don't blame NetBeans, because my taskbar showed the process as java) 250Mb of the 512Mb of memory my laptop has."

Why don't you blame NetBeans? All Java applications ultimately show up in task manager as java.exe. So what you are seeing is the memory usage based on how NetBeans is programmed. Any other reference to NetBeans in the task manager are just executable stub programs designed to get NetBeans up an running under Java.

For example, I have a one line Java program that just waits for user input and closes. It consumes about 5MB. NetBeans is currently consuming 200MB and I'm not executing any code and I don't have any source code windows open. It is just sitting there showing me the project pane.
uggh
Wednesday, May 07, 2008
 
 
And for reference, I just fired up Visual C# 2005 Express and opened a simple hello world project. The memory usage is about 23MB.
uggh
Wednesday, May 07, 2008
 
 
@ughh:

What version of netbeans are you running and what plugins are enabled? My netebans 6.1 (The "All Included" bundle with Web & Java EE, Mobility, Java SE, Ruby and C/C++ modules) has been running for 6 hours and is using 97MB. This versions has a lot more features than VS 2005 express...
Anon Anon Anon
Wednesday, May 07, 2008
 
 
Btw... I'm using Java 6 to run netbeans.. it may make a little difference too.
Anon Anon Anon
Wednesday, May 07, 2008
 
 
For every Java program (using the Sun JVM), there are JVM settings that control how much memory is used. -Xms specifies initial heap size, -Xmx specifies maximum heap size. If -Xms is set to 200MB, it doesn't matter how much space the app actually needs, the Java process will grab 200MB on loading.

One way to monitor application memory usage is through JConsole, which ships with Java5. That'll show you how much of the heap is actually being used.
Nick
Wednesday, May 07, 2008
 
 
buy more ram. it's cheap. 4GB will cost less than a java programer daily salary.
omer
Wednesday, May 07, 2008
 
 
"For every Java program (using the Sun JVM), there are JVM settings that control how much memory is used. -Xms specifies initial heap size, -Xmx specifies maximum heap size."

I must remember to mention that to all the potential users of my Java apps.


"buy more ram. it's cheap."

That too, and the constant, huge updates which will annoy them and eat their hard disk.

Or... I could just use a proper language.
Jimmy Jones
Wednesday, May 07, 2008
 
 
@Jimmy Jones:

> "For every Java program (using the Sun JVM), there are JVM
> settings that control how much memory is used. -Xms specifies
> initial heap size, -Xmx specifies maximum heap size."
>
> I must remember to mention that to all the potential users of
> my Java apps.

No you don't... just create an exe wrapper that sets the proper startup parameters.
Anon Anon Anon
Wednesday, May 07, 2008
 
 
>Visual C# 2005 Express

Visual Studio C++ 2008 Express, one relatively small project open, running for about three hours, using 29MB.

Yes, Java is a memory pig in my own experience.
.
Wednesday, May 07, 2008
 
 
>> What's wrong with having 512Mb of RAM?

LOLOLOLOLOLO... I can't even comment I'm too busy laughing
TravisO Send private email
Wednesday, May 07, 2008
 
 
@Travis

I don't think programmers should cover inefficient code with more RAM. I have been using a 512Mb laptop for a year or so and I can tell you that beside Java, everything else is fine.

Even MS Office 2007 is fast. And Dell wanted to ship it with Vista (I refused the Vista offer though).
JavaShouldBeLessRamHungry
Wednesday, May 07, 2008
 
 
">I sometimes wonder if the "heap is cheap" philosophy of  Java isn't a bad one.

 Obviously not, based on Java's popularity."

Obvious absence of logic. Popularity does not equal good design.
McCarthy
Wednesday, May 07, 2008
 
 
Questions of memory usage aside, I picked up a gig of laptop RAM for $38 the other day and slotted it in my laptop easy as anything. Among other marvels, it now boots much faster. Go on. It's worth it.
Jivlain Send private email
Wednesday, May 07, 2008
 
 
"For every Java program (using the Sun JVM), there are JVM settings that control how much memory is used. -Xms specifies initial heap size, -Xmx specifies maximum heap size. If -Xms is set to 200MB, it doesn't matter how much space the app actually needs, the Java process will grab 200MB on loading."

Generally Java memory usage is very tunable.  See:
 http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

In my experience a standard JVM follows a relatively simple algorithm when allocating space for a new object instance:

- Check if there is space in the heap.  If there is, allocate space.
- If there is not enough space, check current heap space against maximum heap space.  If current < maximum, request more memory from the OS.
- If current = maximum, then perform garbage collection
- If there is still not enough heap space available, throw an OutOfMemoryError

Therefore if your java process is eating a lot of RAM but not getting OutOfMemoryErrors, then dropping the -Xmx value may simply solve your problem.  The downside to this approach is more garbage collection cycles.

On the other hand if you do end up getting OutOfMemoryErrors, then you either have a memory leak (i.e. references unnecessarily being kept around) or your program legitimately needs that amount of RAM (in-memory DB or something along those lines).

Anyway the key lesson learned here is that Java's RAM usage is absolutely tunable to your needs and situation; it is not really a fair statement to simply "blame Java" or "blame the JVM" for RAM usage.
Greg Case Send private email
Wednesday, May 07, 2008
 
 
uggh > And for reference, I just fired up Visual C# 2005 Express and opened a simple hello world project. The memory usage is about 23MB.

"Hello, world!" EXE in PowerBasic = 2.5MB. We can probably do better than this.
ZeFred
Wednesday, May 07, 2008
 
 
I think Linux might be a better OS with 512mb of RAM. Isn't that the saleing point of Linux where you get to keep using your old outdated hardware for as long as you like.

why do fatcat M$ alway want you to spend more money on hardware to run their ass-hog OS?
OSS Rock!
Wednesday, May 07, 2008
 
 
OSS Rock - to a certain degree, but good luck running modern Linux distributions (X + KDE/Gnome/etc + Firefox) on a 486.
none
Wednesday, May 07, 2008
 
 
""Hello, world!" EXE in PowerBasic = 2.5MB. We can probably do better than this. "

So you are saying that the whole PowerBasic IDE runs on 2.5MB? I think you misunderstood what I was saying. I was saying that Visual Studio was consuming 23MB with a simple project open while NetBeans was consuming 200MB with a similar project open.
uggh
Wednesday, May 07, 2008
 
 
"Python is significantly slower and eats much more RAM than Java."

But Python is faster than Ruby. :P
Victor Noagbodji
Wednesday, May 07, 2008
 
 
>"On the other hand if you do end up getting
> OutOfMemoryErrors, then you either have a memory leak"


Wait a minute.... Java has memory leaks?
Jimmy Jones
Thursday, May 08, 2008
 
 
Sure. In Java, a memory leak is caused by accidentally retaining a reference to an object you don't need anymore, thereby preventing the GC from ever collecting it.
BenjiSmith Send private email
Thursday, May 08, 2008
 
 
My _default_ install of Netbeans is EXPLICITLY set to allocate up to 256 megs of RAM at startup.  This is a configurable option.  If you want to set it to 24 megs (or any value of your choosing) feel free to.

....Nothing to see here folks, move on....

Thursday, May 08, 2008
 
 
It makes a tradeoff between performance, reliability, and memory.

But people have *always* complained about Java being memory hungry.  I remember how mad I was that it took 20 MB!!

But, check out the enhancements made in JDK 1.6u10.  Lots of stuff is allocated on the stack now.
Lally Singh Send private email
Sunday, May 11, 2008
 
 
It depends on the kind of application you are developing.Simple java applications don't take that much
resources.But consider this scenario.Most of the time java
is used for web development.Eg. A typical setup with Eclipse as the IDE,with JBoss server plugin,and struts,spring,hibernate,jsp,servelet...will take up a lot of memory and you will have to allocate extra heap space even to run with modest performance.Most of the frameworks
create several objects on startup and keep them in memory till you exit the IDE.And the indeterminate nature of GC adds to this mess.
biju thomas Send private email
Tuesday, May 13, 2008
 
 
>> ... Visual C# 2005 Express ... simple hello world .. memory usage is about 23MB

Well if you're running it from debugging mode that's a diff story.  If you compile the project and run the EXE, you'd see like 12-14mb of ram.
TravisO Send private email
Wednesday, May 14, 2008
 
 
I agree that Java's memory management is completly terrible.  Or at least Sun's JVM is.  It's possible another JVM could do this differently.

In the Sun JVM, you have that maximum heap option.  The problem is that if your program *ever* needs 512MB, then it has to have that set to 512MB, and then even when it is only really using 10MB, all the garbage objects sit there in that 512MB forever.
Jonathan Briggs
Wednesday, May 14, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz