Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

An issue about thread number and total memory.  RSS feed

 
Dmitry Zhuravlev
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings!

Gentlemen, as you know there is a method Runtime.freeMemory() which returns the amount of free memory available to JVM in bytes. In my app I have created a special thread that logs this number for me to know whats going on with memory.A problem is that sometimes this method returns a value as small as dozens or hundreds of bytes. One time I have observed Runtime.freeMemory() = 8. It looks like I have a memory problem. My Tomcat stops answering the requests.

In my app I create several threads via ThreadPoolExecutor and ScheduledThreadPoolExecutor. As far as I understand if a thread runs some class's method it creates all the method-local variables not in HeapMemory but in StackMemory. And this memory is a part of total memory available to JVM. The formula should be something like this:
Runtime.freeMemory = Runtime.maxMemory - Thread1Stack1 - Thread2Stack-...-HeapSpace.
Its just a guess. Am I right?

I am trying to figure it because I need some guidelines on how many threads can operate simultaneously in my application. Each threads consumes memory because it creates some method-local variables while running some methods. How can I know which number of threads is ok? Usually my application creates 3-4 threads, but sometimes the number is raised to 10-12. I suppose its ok for processor because that threads spend a big amount of time sleeping. But I was not taking memory issues into account. Now I have to do it.

Can you advice me some ways to recover from this memory problem?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this is a threading issue , 10-12 threads is nothing on a modern OS.

freememory ...

returns the amount of free memory in the Java Virtual Machine. Calling the gc method may result in increasing the value returned by freeMemory.


i.e. your used memory may be garbage and freed by a gc , so you ever see out of memory error in which case read up on the java options -Xmx etc

If you still think you have a link use VisualVM (free in your Jdk bin directory) to attach and look at whats eating your memory.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!