Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- // try to perform an operation using a huge in-memory array
- byte[] massiveArray = new byte[BIG_NUMBER];
- }
- catch (OutOfMemoryError oome) {
- // perform the operation in some slower but less
- // memory intensive way...
- }
- if (Runtime.getRuntime().freeMemory() > SOME_MEMORY) {
- // quick memory-intensive approach
- }
- else {
- // slower, less demanding approach
- }
- -Xmx
- java -Xmx8g some.name.YourMemConsumingApp
- if( isEnoughMemory( SOME_MEMORY ) ){
- strategy = new InMemoryStrategy();
- }else{
- strategy = new DiskStrategy();
- }
- strategy.performTheAction();
- ...
- strategy = new ImaginaryCloudComputingStrategy();
- ...
- while( !isDone() ) {
- if( isMemoryLow() ){ //Runtime.getRuntime().freeMemory() < SOME_MEMORY + some other validations
- swapToDisk(); // and make sure resources are GC'able
- }
- byte [] array new byte[PREDEFINED_BUFFER_SIZE];
- process( array );
- process( array );
- }
- cleanUp();
Add Comment
Please, Sign In to add comment