user1563047
user1563047

Reputation: 51

simulating memory usage in Java

Is there any Java api available which would help in simulating a fixed amount of memory being used ??

I am building a dummy application that contains no implementations in its methods. All i would like to do within this methods is simulate a certain amount of memory being used up - is this at all possible?

Upvotes: 3

Views: 2500

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

The simplest way to consume a fixed amount of memory is to create a byte array of that size and retain it.

byte[] bytes = new byte[1000*1000]; // use 1 MB of memory.

Upvotes: 5

dardo
dardo

Reputation: 4960

Could get tricky with the way Java handles memory, considering applications are run through the runtime environment, don't know if it's going to the heap, etc.

One simple way might be loading text files into memory of the specific sizes you want, then somehow making sure they don't get garbage collected once the method returns.

Upvotes: 1

Related Questions