Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Data i/o streams and memory  RSS feed

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does open streams affect the CPU usage and memory? In my project, I have Data IO Streams which are socket connected streams as



If I close these streams after using the socket connections too get closed. I am doubting that open streams cause CPU usage and memory. Am I correct? If now what causes high CPU usage and memory?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:Hi,

Does open streams affect the CPU usage and memory? In my project, I have Data IO Streams which are socket connected streams as



If I close these streams after using the socket connections too get closed. I am doubting that open streams cause CPU usage and memory. Am I correct? If now what causes high CPU usage and memory?


Everything you do in your Java program will have some affect on the CPU usage and memory usage on your system. The various different stream objects take a non-zero amount of memory to construct, and whenever your program does something it will almost certainly involve the CPU at some point.

However if you're talking about idle streams (where you open them and then never send or receive any data) then I would wager that they have a negligible effect on CPU and memory usage. Then couple that with the fact that the operating system probably farms out a lot of the network IO processing to the network card then I doubt even in the general case that the sockets will have much impact on CPU and memory usage.

The real contribution to those resources will come from your code that uses the sockets. When you read data from a stream it will be placed into memory. When you process data for sending or after receiving it then it will use CPU usage. So if you use a stream to read a 1GB file into memory then you'll have used up a lot of memory, and if you process that file you will use some CPU etc.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

Thanks for the reply. I was using a while(true) loop without delay that caused me CPU usage. I inserted a 100 ms delay which reduced the CPU usage to 00. How does that happen? And If I transfer data over sockets, the CPU usage goes to nearly 61 but it doesn't come back to 00 after completion of task. It stays in the range of 55 - 65. Any ideas?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post your code...
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partheban Udayakumar wrote:I was using a while(true) loop without delay that caused me CPU usage. I inserted a 100 ms delay which reduced the CPU usage to 00. How does that happen?

That sounds like so-called "busy waiting" - essentially a loop where the CPU never gets to sleep (or wait). That's a big no-no. Having the thread sleep for N ms in each loop is a standard way to avoid that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!