• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Limiting the usage of the JVM

 
Ranch Hand
Posts: 31
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to try and limit the usage of both RAM and CPU in Java. I have created a very CPU intensive program and would like to limit its usage of the computers CPU time. The program is designed to be extremely inefficient as I would like to find a way to limit Java cou usage. Is it possible to do this in Java or even through a JNI? I understand that memory is already limited with Xmx.

Thank you to all who reply.
TS
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Storm wrote:I would like to try and limit the usage of both RAM and CPU in Java. I have created a very CPU intensive program and would like to limit its usage of the computers CPU time. The program is designed to be extremely inefficient as I would like to find a way to limit Java cou usage. Is it possible to do this in Java or even through a JNI? I understand that memory is already limited with Xmx.



CPU scheduling is more related to the OS than with any process. And the JVM is just a process.

And unfortunately, there is no one standard way to do it. Different OSes may have different options on how to control scheduling.

Controlling which processors to use is controlled by "affinity". You can literally place each thread on a particular cpu core if you like (with some OSes). With this, you can limit the threads to a particular set of cores to be used -- leaving the other cores free for the other processes. However ...

IMO, controlling priority is probably easier. You can actually lower the priority of any process (in this case the JVM) -- in Linux, this is done via the "nice" command. The JVM will still use *all* of the cores and at 100%, but it is running at a lower priority. The JVM process will give up time slices as soon as the other processes need them. This gives the JVM all the cores, as long as no other process needs them.

Henry
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Windows you can do a similar thing using the Windows Task Manager. Open the Processes tab, find your JVM process, right click, then choose Set Priority -> Low.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Storm wrote:I would like to try and limit the usage of both RAM and CPU in Java.


You need to implement a simple scheduler using Java's synchronization techniques. It can be a control thread, that just toggles a flag which is checked in a worker thread. Or it can be some combination with synchronization involved. A particular variant depends on your exact situation.
 
Tom Storm
Ranch Hand
Posts: 31
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean to create a thread scheduler and then assign different priorities to threads? If so my only objection is that the usage wouldn't change unless another thread of a higher priority needs cpu time. What I thought could be implemented was a method that would effectively slow down a Java program which would result in far less usage.
Possibly a method which analyses a certain number of objects, then sleeps for a certain period of time and then analyse the same number of objects in a continuous loop. I am not sure if that would result in smooth lower usages or extremely varied usages.
Thoughts?

Thanks for the replies.
TS
 
Alexey Bezrodnov
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Storm wrote:Do you mean to create a thread scheduler and then assign different priorities to threads?


No, I mean to create a method call in your worker thread. When thread performs some work in a loop, it calls a method to checks some flag value every iteration. If flag value is negative, then method calls wait(numberOfMilliseconds); If flag value is positive the method just returns. And flag value is managed by another thread, where you can define any logic you want the scheduler to implement.
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

While both of these sound interesting...

Tom Storm wrote:
Possibly a method which analyses a certain number of objects, then sleeps for a certain period of time and then analyse the same number of objects in a continuous loop. I am not sure if that would result in smooth lower usages or extremely varied usages.
Thoughts?



Alexey Bezrodnov wrote:
No, I mean to create a method call in your worker thread. When thread performs some work in a loop, it calls a method to checks some flag value every iteration. If flag value is negative, then method calls wait(numberOfMilliseconds); If flag value is positive the method just returns. And flag value is managed by another thread, where you can define any logic you want the scheduler to implement.



It also sounds like a lot of work. What wrong with just running an OS command just before starting the JVM?

And quite frankly, if you can do such drastic changes to the application, why not do something simpler? Like use less threads? Or use a thread pool instead?

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Storm wrote:If so my only objection is that the usage wouldn't change unless another thread of a higher priority needs cpu time. What I thought could be implemented was a method that would effectively slow down a Java program which would result in far less usage.



And why would you want to do that? Meaning why would you want the CPU to be anything less than 100%, when there is work to do?

The most common purpose for this is so that you can have some free CPU resources. CPU resources that are available so that you don't starve more important processes. By using priorities, you don't starve more important processes, and for cases where the more important processes don't need the CPU, the JVM will use them.

In other words, you solve the starvation problem, and you fully utilize your processors. You bought a computer with lots of CPU cores for a reason. Why would you ever want them to sit around doing nothing?

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Storm wrote:I would like to try and limit the usage of both RAM and CPU in Java. I have created a very CPU intensive program and would like to limit its usage of the computers CPU time. The program is designed to be extremely inefficient as I would like to find a way to limit Java cou usage. Is it possible to do this in Java or even through a JNI? I understand that memory is already limited with Xmx.


Which will probably solve one of your problems.

There's already a fairly lively thread created by Alexey that might answer some of your others - he says that a Java "booster" might solve some of your problems; we're not convinced ... yet.

My suggestion would be to try and rein in the parameters of your problem: Are you simply interested in performance as your sole metric, or do you want a specific application that is running too slowly to work faster? The two problems are very different, so the solutions are likely to be too.

Either way, my advice would be to change as little as possible; and specifically:
Don't trade design for performance.

HIH

Winston
 
Tom Storm
Ranch Hand
Posts: 31
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for you replies.

I would like to reply to some of your answers. Firstly the reason I would like the program to slow down is because if the CPU is at 100% the CPU is either operating at the very optimum level or else the actual load is closer to 120%. It is only by slowing down the program that one would see that. Secondly to rein in my parameters - I have created the problem and would like to find the optimum solution to solve it. I have no specific application. I have simply created a very heavy application and am attempting to find the best way to "slow it down".

I understand the scheduler and will see how it affects usage.

Thanks,
TS
 
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic