Why deliberately consume Cpu and memory? In project delivery may choose to apply for a certain project code of the resources of the deployment, tend to set aside some Cpu and memory, so that subsequent extension project can have enough resources, but sometimes I often is very strict, will from time to time to check your server’s Cpu and memory utilization rate have reached a certain requirements, such as Cpu utilization rate is above 60%, If the memory is not up to 70%, you may recycle the configuration to save resources, such as reducing the amount of CPU and memory. Based on this, you can use Java programs to run idle and consume CPU and memory.
1. Go straight to the code
Just one Java class, as follows:
import java.util.Vector;
/ * * * Cpu part of the code reference https://blog.csdn.net/java2000_net/article/details/3136124 * @ author JXLHL * * /
public class ResouceManageService {
// Indicates whether running consumes memory code. Default: no, that is, running consumes Cpu code
private static boolean memConsume = false;
public static void main(String[] args) throws Exception {
final ResouceManageService service = new ResouceManageService();
int num = 1;
for (int i = 0; i < args.length; i++) {
// if -m is specified, memory consumption is specified. If -c is specified or not, Cpu consumption is specified.
if ("-c".equals(args[i])) {
} else if ("-m".equals(args[i])) {
memConsume = true;
num = Integer.parseInt(args[i + 1]); i++; }}if(memConsume){
service.memConsume(num);
}else{ service.cpuConsume(); }}// Call this method only for memory consumption
@SuppressWarnings("unchecked")
public void memConsume(int num){
// Execute a for loop to request memory for the new generation
for(int i=0; i<num *10; i++){ @SuppressWarnings("rawtypes")
Vector v = new Vector();
byte b1[] = new byte[104857600]; //100M
v.add(b1);
/*System.out.println(v); Runtime rt = Runtime.getRuntime(); System.out.println("free memory"+ rt.freeMemory() ); * /
}
// After the application is complete, the JVM executes a death-loop hibernation to keep the applied memory occupied
while(true) {try {
Thread.sleep(3600000l);
} catch(InterruptedException e) { e.printStackTrace(); }}}// CPU consumption method
public void cpuConsume(a){
// Angle split
final double SPLIT = 0.01;
//
// The number of 2PI splits, which is 2/0.01, is exactly one week
final int COUNT = (int) (2 / SPLIT);
final double PI = Math.PI;
// Time interval
final int INTERVAL = 200;
long[] busySpan = new long[COUNT];
long[] idleSpan = new long[COUNT];
int half = INTERVAL / 2;
double radian = 0.0;
for (int i = 0; i < COUNT; i++) {
busySpan[i] = (long) (half + (Math.sin(PI * radian) * half));
idleSpan[i] = INTERVAL - busySpan[i];
radian += SPLIT;
}
long startTime = 0;
int j = 0;
while (true) {
j = j % COUNT;
startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < busySpan[j])
;
try {
Thread.sleep(idleSpan[j]);
// I added the if control here because I want the Cpu to keep working above 70% (< 70 do not sleep), if annotation will use the Cpu in a sinusoidal trend
if(idleSpan[j]<70){ Thread.sleep(idleSpan[j]); }}catch(InterruptedException e) { e.printStackTrace(); } j++; }}}Copy the code
2. Run the Cpu consumption command
Because it is Cpu intensive, the Jvm was configured to start with a minimum of 64M, as shown below
java -XX:InitialHeapSize=64m -XX:MaxHeapSize=64m -XX:NewSize=32m -XX:MaxNewSize=32m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=10m -cp "." ResouceManageService &
Copy the code
3. Run the memory consumption command
java -XX:InitialHeapSize=4096m -XX:MaxHeapSize=4096m -XX:NewSize=3072m -XX:MaxNewSize=3072m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=500m -cp "." ResouceManageService -m 1 &
Copy the code
Parameter description: -m indicates memory consumption. If this parameter is not displayed, Cpu consumption is displayed. 1 after -m indicates 1 GB memory consumption
Principle: When the command is started, the maximum heap memory is applied for 4096Mb, and the new generation memory is applied for 3072M. When the program is started, the memory data generated after -m is put in the list, and then after the application is finished, it has been sleeping. In this way to achieve the consumption of memory is not released. Therefore, the data next to -m cannot exceed the maximum memory size of the new generation. If the maximum memory size and the new generation memory size need to be exceeded, add the maximum memory size and the new generation memory size before starting the system. If 6Gb memory size needs to be deleted at one time, the following example configuration can be used:
java -XX:InitialHeapSize=8192m -XX:MaxHeapSize=8192m -XX:NewSize=7168m -XX:MaxNewSize=7168m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=500m -cp “.” ResouceManageService -m 6 &
XX: set PretenureSizeThreshold into 500 MB, this way, because of my code byte b1 [] = new byte [104857600]; / / 100 m, inside an application memory is 100 m, in order to avoid the application of the object into old age, to XX: PretenureSizeThreshold setting
The above command can be saved in shell for easy execution, such as: ####vi start-cpu.sh#! /bin/sh java -XX:InitialHeapSize=64m -XX:MaxHeapSize=64m -XX:NewSize=32m -XX:MaxNewSize=32m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=10m -cp "." ResouceManageService &
####vi start-mem.sh#! /bin/sh java -XX:InitialHeapSize=4096m -XX:MaxHeapSize=4096m -XX:NewSize=3072m -XX:MaxNewSize=3072m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:PretenureSizeThreshold=500m -cp "." ResouceManageService -m 1 &
Copy the code