1

I want to code/design an algorithm in java in which i started with an input of

variable int i=0;

while (i==0){i==1;}

create or start again with var i in new memory location set value again to 1; loop to assign value 1 to var and jump to next memory location.
until heap memory full
then Calculate total variables created and exist in memory , by using loop

System.out.println(total var created);

Can i done this in Java?

i have done with "how much time to create fixed variable count in java"

1 Answers1

0

If I get you right, you want to fill the heap memory. I guess creating String literals in a loop will do that;

int i = 0;
while(i < 1000){

    System.out.println("2 times" + i + "equals" + i * 2);
    i = i + 1;
}

will create 1000 string literals in the constant string pool. These literals will not be canditates to be garbage collected for some time -enough for filling the memory- for purposes of reuse. So they will fill up the memory eventually. If you increase value 1000, you'll get more literals.

For further reading, here's a blog post

arkantos
  • 497
  • 3
  • 14
  • your program prints this: 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 2 times0equals0 i added this line in while loop: i=i+1; now it prints: 2 times1equals2 2 times2equals4 ...................................... 2 times999equals1998 2 times1000equals2000 –  Sep 02 '18 at 13:16
  • i want to allocate value to var. var have to store every value which initialized by loop on every cycle and values/vars have to remain in memory, loop has to run until memory full and i will get total count of variables which occupied on heap memory.. –  Sep 02 '18 at 13:27
  • Alright it seems my answer doesn't fulfill your requirements. As long as you use the same variable i, you wont get an increasing number of variables, so I can't figure out another solution. Anyway I won't delete my post to keep your question popular. Maybe somebody solves your problem. – arkantos Sep 02 '18 at 13:39