aspose file tools*
The moose likes Beginning Java and the fly likes Trying to understand better how JVM works. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trying to understand better how JVM works." Watch "Trying to understand better how JVM works." New topic
Author

Trying to understand better how JVM works.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Susan,
sleep means the JVM gives up the CPU and waits. Why would you expect the memory usage to change significantly?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Note, also, that sleep() pauses just one thread; other threads in the background (the garbage collector, for example) continue to run.


[Jess in Action][AskingGoodQuestions]
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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)?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
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.


Ping & DNS - my free Android networking tools app
Daniel Chemko
Ranch Hand

Joined: Feb 27, 2008
Posts: 32
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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.

Amaru Shakur
Ranch Hand

Joined: Jul 17, 2008
Posts: 50
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


Even though im marked for death I will spark till i loose my breath
Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
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

Joined: Oct 13, 2007
Posts: 224
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to understand better how JVM works.