Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Java window takes so much memory... Why is this?  RSS feed

 
Ken Shinbashi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I am running some programs from Sun's Java tutorial. I am looking through the Swing sample programs, like the simple Celsius to Fahrenheit converter.

When I run it, my system monitor shows that it is taking 12 MB!!!

Why should a little window with 4 components take up 12 MB? I thought it should be 100K.

If someone could explain this to me, I would appreciate it.


Thanks.

Ken S.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Virtual Machine itself takes up a certain fixed amount of space, regardless of the program you run on it. It's a complicated program itself, remember. Then there's the memory it sets aside for creating Java objects.

But this fixed amount is the same whether your program is 10 lines or 10,000,000 lines. For larger programs, you won't even notice the small overhead.
 
Ken Shinbashi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ernest.

Is it possible then to run two different programs on the same JVM, so that the overhead is shared?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an "application server" -- i.e., a Web server that hosts server-side Java code -- yes, absolutely. Lots of applications can all share one JVM.

For desktop applications, not really. But it's true that Sun's most recent JVM does "class data sharing" on some platforms. Some of the overhead is put into shared memory and all JVMs on one desktop share it.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible, but is it convenient?

For a trivial case, A.class and B.class in anonymous package in the same dir.
Make a small frame with two buttons 'A' and 'B', and start a new Thread if a Button is hit.
Call A.main (null) or B.main (null) in this thread.

To run two programs is easy, but System.exit () in one of them will stop the other.
To be flexible you would need to know every jar to use on startup.
You could need the ability to pass parameters.
Perhaps some program needs settings passed on the commandline like '-Dxy=foo' and these might conflict.
Two applications could use a common Singleton which wasn't meant to work in such environments.

It's possible, but is it convenient?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!