• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Data i/o streams and memory

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your code...
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic