• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trying to understand better how JVM works.

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a very simple program which I created just for testing purpose to understand JVM better.
My question is during Thread.sleep(...), I saw using JConsole that the program is still using a little bit more than 5 MB of memory. Why is that??



Thanks in advance for all the help.
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Susan,
sleep means the JVM gives up the CPU and waits. Why would you expect the memory usage to change significantly?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note, also, that sleep() pauses just one thread; other threads in the background (the garbage collector, for example) continue to run.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I'm wondering is what's that 5 MB memory used for?
Isn't it a little bit too much since the program doesn't have too many things (variables, etc, etc)?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Run your program using

to get an idea of all the things the JVM needs to load into memory in order to get even the simplest class up and running.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On linux you can always look at all open file handles by calling:

/usr/sbin/lsof | grep java
or
/usr/sbin/lsof | grep my_java_app's_pid

And you can see all the files loaded into virtual memory. Of course there's a chance that the memory isn't allocated into physical ram, just that it has been internally mapped to the process space.

Just one example of an entry is:
java 17120 dchemko mem REG 8,19 50827455 7286804 /usr/java/jdk1.6.0_10/jre/lib/rt.jar

The virtual size of my rt.jar is 50MB, so if the JRE loaded the entire core java classes into memory, you'd be sitting idle at 50MB. Anyways, it is also possible that the system's default minimum stack is set higher than you wanted it to.. or maybe the memory allocated is also reflecting memory mapped files, but I couldn't think of any MMIO files that would open by your setup.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Susan Smith:
What I'm wondering is what's that 5 MB memory used for?
Isn't it a little bit too much since the program doesn't have too many things (variables, etc, etc)?



5 MB does not sound like a lot to me. Heck, the byte arrays needed to hold the bytecodes, for the classes loaded by the classloader, could account for most of that alone.

Henry
[ August 06, 2008: Message edited by: Henry Wong ]
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason I want to understand better about JVM is because I have a looping program like below.
I run it for a while and I saw the heap size keeps increasing and increasing (I use Java VisualVM from Java 6 to analyze the heap).
The stack size is constant but not the heap size. I'm trying to figure out why this happen.

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the reason you care about this is because your getting a java.lang.OutOfMemoryError exception? a temporary fix would be to increase the heap size
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to understand why the heap size keeps increasing. I don't understand this.
I have around another 50+ programs doing similar looping process. If I have to increase the heap size again and again, the amount of memory needed for my server would be huge.
 
Susan Smith
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably this is already a separate question from my original question. I will post a separate question.

Thank you all for your help.
I really appreciate it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic