• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Need help in understanding the memory usage of Threads in Tomcat

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I created a sample JSP which will list all the threads in Tomcat server and get the thread Id, thread name and memory used by thread and display it on the browser.

I used the method getThreadAllocatedBytes() to get the memory utilized by a thread. I observed that the memory of threads is increasing consistently.

When the GC is executed in Tomcat by itself, the memory of these threads was freed and displayed a lower value.

However when I continuously executed the sample JSP, I see that the memory of the threads increased consistently again.

My question here is Does the method getThreadAllocatedBytes() will get the overall memory usage of the thread since it is created ?

Please help
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is something you should not even worry about, let the container take care of the Threads.

Tomcat maintains a pool of Threads to handle requests, but after a pool Thread returns from handling a request all of the memory consumed by the request object, the response object and any other objects created during the request are available for GC - if you coded the application correctly. So your question:

My question here is Does the method getThreadAllocatedBytes() will get the overall memory usage of the thread since it is created ?



is meaningless.

Bill

 
Bartender
Posts: 20845
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To explain William's explanation in a slightly different way:

Tomcat maintains several thread pools, but the one I think is of most interest to you is the worker pool which contains threads that run JSP and servlet requests.

It is critical for proper operation of Tomcat that each thread in that pool should be 100% interchangeable with any other thread in that pool. That is because threads do not (and MUST not) retain resources when they are in the pool. All resources obtained by an active thread must be released before the thread is returned to the pool. Since threads are assigned on a purely statistical basis, if they aren't interchangeable, they would cause Tomcat to behave in unpredictable ways.

Thus, measuring memory usage of threads as such is a meaningless exercise. What you want to do is measure memory usage of the functions that run under the threads. That is, the application code. Because that's the part that can vary, that's the part that is most likely to cause out-of-memory, and that's the part that if it isn't cleaned up (leaks) will leak to the problems I just mentioned.

And obviously, no two requests are likely to consume exactly the same amount of memory. Especially when they are different types of requests, but depending on that the request process does, even the same type of request can potentially have vastly different memory demands, based on the data involved.

In addition to application code memory usage, Tomcat also has non-pool threads running that will consume and release memory resources, even while no actual web requests are in process. So expect a certain amount of "sawtooth" on the memory usage graphs as these functions run and their resources are garbage-collected.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!