• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Usage of resources increases when im closing socket, input or output streams  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I study the work of sockets on the example of simple chat
When I try to disconnect a user in the server window when calling the disconnect () method, my usage of CPU jumps to 95%, and RAM to 2GB. And this happens when the method reaches one of three lines:



And no matter in what order they are, the result is an increased expenditure of resources
Also, the user window is being filled with the text "null". I can not imagine what could be the cause.
Im giving all the program code, because I do not know where to look for the problem anymore.





 
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stepan Bogatõrjov wrote:When I try to disconnect a user in the server window when calling the disconnect () method, my usage of CPU jumps to 95%, and RAM to 2GB.


The problem is in this block of code:
The BufferedReader#readLine method will return a null if the socket is closed.  You are passing that null to your listener to be displayed, and then immediately going back for more in a tight loop.

A better approach would be to use the value returned by readLine to control if you remain in the loop and only call the onRecieveString callback while the socket has not been closed.  After getting a null, you will drop out of the loop and the onDisctonnect callback will be called, indicating that the chat session has been disconnected.
 
Ron McLeod
Saloon Keeper
Posts: 2406
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you don't need the call thread.interrupt() here:
The blocking call to BufferedReader#readLine will get return immediately after the BufferedReader has been closed.
 
Stepan Bogatõrjov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Stepan Bogatõrjov wrote:When I try to disconnect a user in the server window when calling the disconnect () method, my usage of CPU jumps to 95%, and RAM to 2GB.


The problem is in this block of code:
The BufferedReader#readLine method will return a null if the socket is closed.  You are passing that null to your listener to be displayed, and then immediately going back for more in a tight loop.

A better approach would be to use the value returned by readLine to control if you remain in the loop and only call the onRecieveString callback while the socket has not been closed.  After getting a null, you will drop out of the loop and the onDisctonnect callback will be called, indicating that the chat session has been disconnected.

Thank! Now everything is working fine. I finally got an answer that helped me. Before that, I asked the same question in 3 other forums and no one could help.
 
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!