java的限制内存_jcgroup一个用java开发的用来限制jvm的io,内存,cpu的包

jcgroup

457270e7976f7f9f75c2932b86d5a7a0.png28d2c7798218d074b101015137f38d8e.png

jcgroup is a cgroup wrapper on JVM. You could use this library to limit the CPU shares, Disk I/O speed, Network bandwidth and etc of a thread.

Subsystems

☑ blkio

☑ common

☑ cpu

☑ cpuacct

☑ cpuset

☑ devices

☑ freezer

☑ memory

☑ net_cls

☑ net_prio

Example

This code snippet create two threads and set different cpu shares of them. One is 512 while another is 2048.

5148066f586957c6f3814323a20d888f.png

public class ExampleTest {

private static final Logger LOG = LoggerFactory.getLogger(ExampleTest.class);

private static Admin admin;

private static Group root;

private static Group one;

private static Group two;

@BeforeClass

public static void setUpClass() {

try {

admin = new Admin(Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);

root = admin.getRootGroup();

one = admin.createGroup("one", Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);

two = admin.createGroup("two", Constants.SUBSYS_CPUSET | Constants.SUBSYS_CPU);

} catch (IOException e) {

LOG.error("Create cgroup Failed.", e);

assertTrue(false);

}

}

@AfterClass

public static void tearDownClass() {

try {

admin.umount();

} catch (IOException e) {

LOG.error("Umount cgroup failed.", e);

assertTrue(false);

}

}

@Test

public void testCpu() {

try {

one.getCpuset().setCpus(new int[]{0});

two.getCpuset().setCpus(new int[]{0});

one.getCpuset().setMems(new int[]{0});

two.getCpuset().setMems(new int[]{0});

one.getCpu().setShares(512);

two.getCpu().setShares(2048);

final Group oneTmp = one;

final Group twoTmp = two;

new Thread(){

@Override

public void run() {

int id = Threads.getThreadId();

LOG.info("Thread id:" + id);

try {

oneTmp.getCpu().addTask(id);

while (true);

} catch (IOException e) {

LOG.error("Test cpu failed.", e);

assertTrue(false);

}

}

}.start();

new Thread(){

@Override

public void run() {

int id = Threads.getThreadId();

LOG.info("Thread id:" + id);

try {

twoTmp.getCpu().addTask(id);

while (true);

} catch (IOException e) {

LOG.error("Test cpu failed.", e);

assertTrue(false);

}

}

}.start();

Thread.sleep(60000l);

} catch (Exception e) {

LOG.error("Test cpu failed.", e);

assertTrue(false);

}

}

}

The wrapper requires a configuration file that defines the user and the password used to create the tasks. That user must be able to run sudo. You must add a user_conf file to the classpath, e.g. in the /src/main/resources or the /src/test/resources project folder, using the following format:

{

"name" : "james",

"password" : "bond"

}

Requirements

Linux version (>= 2.6.18)

cgroups management tools. In Ubuntu or Debian, you may install the tools using: sudo apt-get install cgroup-bin

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. See accompanying LICENSE file.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值