- Tausief Shaikh (Senior Server developer)
Introduction Covers sense of responsibility towards Project development in IT Focusing on memory and CPU utilizations rather than just coding. Thread and Thread Pool Eclipse Debugging Use of thread dumps and analysis using JVisualVM
Developing a software is easy but developing best software is indeed very tough If you know how to code, have some basic logic thinking, and with proper assistance of NET you can develop applications easily. Eg: If you know Java core, like datatypes, OOPs concepts, defining classes, interfaces, variables, method, then you can code and go on to develop big applications. Knowing the write ways of coding (designing in specific) however needs some experience, but would definitely come after spending some time in IT.
Developing a software is easy but developing best software is indeed very tough… Even tough we develop an application, functionally all correct, that doesn't mean our work is done. As a developer its our responsibility to make sure that the application works superb under heavy load. When many users access the application concurrently, then this can add more load on the resources we use. That means, more load on the memory, more CPU time utilization, more disk space utilization, etc…. Our application would always be deployed and run in a limited environment, where we have some max memory size, max disk space, max capability of CPU core. So there is always a challenge of how many requests the application can process in parallel, because beyond this the application would eventually crash.
Developing a software is easy but developing best software is indeed very tough… If many objects are created on heap for processing a single, then when huge load hit the app in parallel, lot of heap size would be occupied, and app would crash, so objects has to be wisely allocated and cleaned-up from memory. Like wise, when we create a thread, a new stack location would be allocated. So if too many threads are created, then eventually a time would come when we might run out of memory crashing the application again. Like wise, we can risk the harddisk spaces as well. Thus we should be very particular while accessing these resources, and should be a very valuable concern of every Java developer.
Thread and Thread Pool We all know that for every thread new stack memory is allocated. Due to this, code runs in parallel. If you think like a server application developer(suppose tomcat developer), then for every incoming request, a thread should be allocated, in order to process the request. We can simply create a new thread for every request. Even tough this would functionally be fine, however would just be a nightmare when practically deployed on server. One of the disadvantages of the thread-per-request approach is that the overhead of creating a new thread for each request is significant; a server that created a new thread for each request would spend more time and consume more system resources creating and destroying threads than it would processing actual user requests.
Thread and Thread Pool In addition to the overhead of creating and destroying threads, active threads consume system resources. Creating too many threads in one JVM can cause the system to run out of memory or thrash due to excessive memory consumption. To prevent resource thrashing, server applications need some means of limiting how many requests are being processed at any given time. To avoid this, the best solution is to introduce and maintain a ThreadPool.
Advantages of Thread Pool By reusing threads for multiple tasks, the thread-creation overhead is spread over many tasks. Eg: when a request comes, server would create many threads and keep it ready for execution. As a bonus, because the thread already exists when a request arrives, the delay introduced by thread creation is eliminated. Thus, the request can be serviced immediately, rendering the application more responsive. By properly tuning the number of threads in the thread pool, you can prevent resource thrashing by forcing any requests in excess of a certain threshold to wait until a thread is available to process it.
Thread Pool – your usage If you get a opportunity to work on server applications, then you can speed up your response time and avoid memory crash issues. At times, you can tune the thread pool size of the server, for suiting your application requirement. You can create your own thread pools for performing any asynchronous task. For specific projects you can design a framework over core threads and mange it with pool in order to control sync and async processing of tasks.
Eclipse (with Thread) While a thread executes, it runs in Stack. Stack consists of 3 parts: local variables, operand stack, and frame data. The code instruction executes one by one based on the logic Eg: In this execution flow: Line 1 -> 2 -> 3 -> 6 ->… Eclipse contains a feature by which we can track the execution flow of a thread. And to add more to it, we can also see the runtime values of variables.
Importance of eclipse debugging This can be the ultimate tool which can help you in debugging any major issues during project dev. When you catch a thread, then you can see entire internal details about the thread as to what is actually happening at runtime of project. This can help you in exploring runtime flow of any use-case with 100% assurance.
Eclipse (with Thread) – Debug Standalone app For debugging a standalone Java application in eclipse, follow the below process: Add breakpoint in java code where you want to stop the thread execution. (double client on line numbers mentioned in left) Right click Java class(with main()), Debug As => Java App Executions stops at the line where breakpoint is added Using Eclipse debug perspective, walkthrough thread
Starting Tomcat in debug mode – Approach1 Directly configure tomcat in eclipse. Then “Debug On Server” would directly start tomcat in debug mode and help your breakpoints getting caught. Advantage: This method is very useful when tomcat is running in local machine, as it saves a lot of manually efforts and Ecilipse does lot of things for you. Disadvantage: While working with shared integrated environments, where tomcat is on another linux machine, this process might not be that handy
Starting Tomcat in debug mode – Approach2 This approach would also be handly while working with integrated environment Step 1: Start tomcat in debug mode Step 2: Eclipse Configuration to connect to remote tomcat process Continue…
Starting Tomcat in debug mode – Approach2 Start tomcat in debug mode Goto DOS prompt: and run below 2 commands set JPDA_ADDRESS=8000 set JPDA_TRANSPORT=dt_socket Run: /bin/catalina.bat jpda start Eclipse configuration Goto Debug configuration In left pane, start the remote Java application pointing to 8000 debug port started by tomcat for accepting remote debug conections.
Thread Dump and usage Lot of time during application testing, you might end up in a awkward situation where all your app threads might be stuck or waiting for something. And your application would not have any thing threads available to process your request. In such cases, it really becomes tough to analyze as to what’s going with the threads. And this problems grows with huge KLOC(kilo lines of code). In entire code base, any wrongly implemented use-case, might have consumed all your threads.
Thread Dump and usage Threads might be “getting stuck/consuming more processing time” due to any of the below reasons: Synchronized blocks/methods Taking more time for DB calls Dependencies on external resources, like HTTP calls or remote webservices or remote ejbs Nested iterations can also add delay. Or threads might be in sleeping state. We should write code in such a way so as to optimize or avoid all of the above issues. But still, while doing load testing, you might end up observing the above issues. Which risks thread utilizations.
Thread Dump and usage Thread Dumps are used to monitor the state of any of the application thread at a given point of time. Thread dump is a snapshot of all thread-state at any give point of time. At a given point of time you can say, which threads are doing what in your application. This can help you in identifying the bottleneck in your application. Then you can visit the code and work on the piece to make it perform better.
Thread States - JVisualVM Threads can be in any of the below states RUNNING: currently threads are performing some tasks. You can see currently the thread is running which line of which class. SLEEPING: Indicates currently thread is in sleeping state WAITING: This doesn’t specify problem situation, it simply means that thread is available in pool and can process any new task which is submitted to pool. MONITOR: Currently thread is blocked due to lock acquired by other thread on a resource which this thread is trying to active.
Java Visual VM JVisualVM is a tool which comes along with java installation under /bin folder This can be used to indicate internal memory details of an application. Like CPU utilizations, Heap occupancy, classes and thread details in the memory. You can effectively check the thread state of the system. It provides with a “Threads inspector” window, which helps you in identifying at which line number the threads are currently.
Thank You. Presenter details: Name: Tausief Shaikh Id: Fb: Contact: