Sample Java code to simulate out of memory situation [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am basically a Weblogic admin and want to simulate out of memory situation through deploying a very simple Java code(war/ear file) in my Weblogic instance.
I have a very little knowledge about Java coding so can someone please provide me a sample code which I can easily pack as war and deploy?

This should be enough:
long[][] ary = new long[Integer.MAX_VALUE][Integer.MAX_VALUE];
This will try to allocate 2^31 + 1 memory blocks, each of size 2^34 bytes.

You can do final long[] l = new long[Integer.MAX_VALUE]; It will allocate 16Gb - 8 bytes.
Or you can just throw new OutOfMemoryError();

To simulate the memory being consumed over time try:
List<long[]> list = new LinkedList<long[]>();
while (true) {
list.add(new long[65536]); // an arbitrary number
// sleep(1) perhaps?
}

Related

Fastest way to find an integer in array of integers without sorting and multithreading [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
This question was asked before and looked that without sorting and multithreading one simply needs to iterate through all elements. Is there any other idea? Java implementation is fine
If there are no other prerequisites for the input array, then no: You have to iterate through all the elements until you find the one you are looking for (thus, in worst and average case that would be O(n)).
If you have something else (like a heap, a search tree or any otherwise sorted array) you can use smarter and much faster techniques, of course (e.g. binary search).

in the code snip below, the compiler won't compile [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
code will not compile
byte a = 127;
byte b = 1;
byte c;
c = (a + b); //eror
System.out.println("The value of c=" + c);
the compiler won't compile the code.
Java stores numeric types as int by default. Try:
c = (byte)(a+b);
The error occurs because an integer (4 bytes) cannot be contained within a single byte without losing information, which is why the compiler requests that you make this conversion explicit.
Additionally, FYI - single short and byte declarations are actually stored as a single word in memory (which is 4 bytes), so you are not saving memory while restricting the size of the number. This is not the case with arrays, where each slot in an array of byte or short will actually have 1 or 2 bytes respectively.

code for buildShortArray for audiomixing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to android and i just want to create a dj player. but for that the starting step is mixing two files. The rough code for that i found on the following link but in that i did not understand how to code for buildShortArray(music1).
I already tried this code but got stuck in the above mentioned method's code.
thank you in advance for help.
Docs here:Mix two files audio wav on android use short array
The code from the link does not show the buildShortArray method.
You need to transform List<Short> into array short[]:
List<Short> music1 = ...;
short[] arrayMusic1 = buildShortArray(music1);
You could write the method buildShortArray like this:
public short[] buildShortArray(List<Short> list) {
short[] array = new short[list.size()];
for(int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
return array;
}
However, i'd like to warn you, copy-pasting code is never a good idea.

How to know file size from bytes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have byte array, how to know file size ? (in JAVA)
File file = new File("patch");
file.length() // <--- it's good, but I haven't original file... (( I get file in bytes from DataBase !
Thanks
You have an array with you and every array has a length. That's it.
byteArray.length;
And
1kb = 1024 bytes

How to store long range number [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to store this value.but eclipse shows error on that.
private final long cal_To_eV = 26131952998320000000L;
plz tell me how to store this.
Have a look at the class BigInteger.
You can use String not long. Also you can use BigInteger or BigDouble.
private final BigInteger cal_To_eV = new BigInteger("26131952998320000000");

Categories