-1

Have written a Java code which sends mail when whole system RAM reaches > 95%.

I want to write a Java code to test this scenario. Have written few(recursive etc...), but those are crashing the JVM but not System.

Any help please ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Molay
  • 1,154
  • 2
  • 19
  • 42
  • so you are able to see the RAM usage or not! what are using for it! sigar api or something else please provide more detail I think I have a way to do it for you! – Rizwan Atta May 04 '18 at 11:31
  • 3
    Good thing about java, it's doesn't have access to the system but only the JVM. You will find it complicated to crash it completely, of course this doesn't mean this is not possible, but you are on an android device so you will be limited based on the permission, if you have SU access... – AxelH May 04 '18 at 11:32
  • Wouldn't mocking be a better way of unit testing your code? Rather than trying to crash the system it runs on? – Antho Christen May 04 '18 at 11:34
  • You will not be able to start multiple app since Android will claimed paused application if it needs some resource (like memory).... android and java are design to be robust since we don't want to manage that level of resource. Alternative : Run your code on a JVM, ask the JVM for the maximum RAM you can get and run your code. You can also reduce the limit (95%) to get the trigger without getting that far. – AxelH May 04 '18 at 11:35
  • 1
    Last thing, recursive calls will not used much ram, you will get more success creating arrays of `new byte[Integer.MAX_VALUE]` and keeping the instance in a `List`. – AxelH May 04 '18 at 11:38
  • If you want to 'crash' the system, you can simply add a few lines of code that start an executable file shutdown.exe, which is present in C:\WINDOWS\system32 folder in Windows 7 & XP. –  May 04 '18 at 11:55
  • You could probalby make use of the `Unsafe` class – Lino May 04 '18 at 11:56
  • @AxelH Wont you run `OutOfMemory`before? – Antho Christen May 04 '18 at 11:57
  • Before what @anchreg ? – AxelH May 04 '18 at 11:58
  • 1
    @JanHabjan, this is tag with [tag:android]. I doubt you will found a `C:\WINDOWS\system32` folder or even be able to execute an `exe`. – AxelH May 04 '18 at 11:58
  • @AxelH tried with this code, throwing : Exception in thread "main" java.lang.OutOfMemoryError: Java heap space, byte b[] = new byte[1048576]; list.add(b); Runtime rt = Runtime.getRuntime(); – Molay May 04 '18 at 12:00
  • @AlexH before the System crashes? – Antho Christen May 04 '18 at 12:00
  • You should take a look at mockito (just google it) great tool for mocking. Testing your program should NOT!!! depend on crashing your system. – Tobias Götz May 04 '18 at 12:00
  • 1
    I never said it will work, I said it will use more memories than filling the call stack... – AxelH May 04 '18 at 12:00
  • @AxelH my bad:) –  May 04 '18 at 18:11

1 Answers1

0

UN_ORTHODOX SOLUTION but it works anyway

NOTE! I Used WINDOWS 8.1 host machine

I found this DOC on myself too! in old days; about JVM and host system access issues. I used this code to get details about host system!

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

private static void printUsage() {
  OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
  for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
    method.setAccessible(true);
    if (method.getName().startsWith("get")
        && Modifier.isPublic(method.getModifiers())) {
            Object value;
        try {
            value = method.invoke(operatingSystemMXBean);
        } catch (Exception e) {
            value = e;
        } // try
        System.out.println(method.getName() + " = " + value);
    } // if
  } // for
}

NOTE !

that's not the real way To do but I did it by opening the Default browser (in most of cases I know most of the time its GOOGLE CHROME) , had 8GB ram those days so opening a few tabs with some random youtube and other links it helped me to reach up the memory usage unto 90% in no time! because it eats the RAM (No offence to CHROME people!) doing that I was able to achieve the test you are trying to get. :-)

TO Open a default browser just take a look on this thread its quite nice with different ways to do it!

well if you are using it for android well read about proguard rules for memory management and try using any external library which takes up too much memory like any dummy faceRecog library or simply just accessing some NDK features or so check this link for more

Rizwan Atta
  • 3,222
  • 2
  • 19
  • 31
  • oh that will be more easy! you know! android devices have this builtin webView api which is totally a great mock of CHROMe engine! so just make one in your app and try to run it there! – Rizwan Atta May 04 '18 at 11:53
  • Don't this will just throw an `OutOfMemoryError` on the WebView ? The webview is a distinct application that follows the same rules as any application. I doubt you will be able to crash the device with that. (Can't test, won't test ;) ) – AxelH May 04 '18 at 11:55