aspose file tools*
The moose likes Threads and Synchronization and the fly likes thread, increase in memory used by the object and less memory usage for CPU. is it correct? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread, increase in memory used by the object and less memory usage for CPU. is it correct?" Watch "thread, increase in memory used by the object and less memory usage for CPU. is it correct?" New topic
Author

thread, increase in memory used by the object and less memory usage for CPU. is it correct?

Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
I have been doing some experiment on multithreaded programming.

Is my findings correct with the increase number of threads?
1- The time performance speed up.
2- The memory used by the object, i think (Runtime.getRuntime().totalMemory - Runtime.getRuntime().freeMemory implemented in the coding) become higher.
3- CPU usage also gets higher.
4- Memory usage of the CPU gets lower.

As my readings said multithreading programs should speed up the performance also should able to use less of memory. Thus this memory refer to memory usage of the CPU? Next, why the memory used by the object getting higher along with the increase number threads?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

Hello Sara Brown,

Here is what I think (though I'm not a threading expert):
1- The time performance speed up.

Not necessarily. If you have only one processor, then after one particular value, performance decreases drastically. e.g. 4 threads might do something faster than 1 thread, but if you do same operation with, say 100 threads, then it might take more time than sequential operation.
2- The memory used by the object, i think (Runtime.getRuntime().totalMemory - Runtime.getRuntime().freeMemory implemented in the coding) become higher.

That is memory used by JVM, not object. Yes, it becomes higher, because there are extra Thread objects now, and each thread has its own stack.
3- CPU usage also gets higher.

Generally, yes. Unless you have 1 CPU per thread - there's no context switching (I mean context of the thread).
4- Memory usage of the CPU gets lower.

Again, yes only if you have multiple CPUs. Basically, this CPU usage and memory usage of CPU gets divided among the CPUs. So, for one CPU, you have x percent usage and y MB of memory usage, then for 2 CPUs, generally, it is x/2 percent usage and y/2 MB of memory usage, and other halves of both CPUs can serve other processes/threads.
As my readings said multithreading programs should speed up the performance also should able to use less of memory. Thus this memory refer to memory usage of the CPU?

Yes. It is memory of CPU. But multithreading does not always give guarantee of increase in speed. e.g. if you are writing files on a SATA hard-disk, then 2 or 4 threads would do it faster than 50 threads.
Next, why the memory used by the object getting higher along with the increase number threads?

Well, because, as already mentioned, there are extra Thread objects, a thread scheduler in place(without threading, there's only one thread - main thread - and hence no scheduling is required), and each thread has its own call stack (i.e. why thread cannot return a value).

I hope this helps.

Correct me if I'm wrong.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
Hi Anayonkar Shivalkar.

This is so helpful! thank you very very much. I really appreciate it.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1512
    
    5

You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread, increase in memory used by the object and less memory usage for CPU. is it correct?