Sign in with
Sign up | Sign in
Your question

Filling the memory

Last response: in Applications
Share
December 5, 2012 8:06:58 AM

Hello,
i want my application to give no memory available error.So i have to fill the memory somehow..how do i do it?
just running a program which allocates a lot of memory will do the rick?

More about : filling memory

a b L Programming
December 5, 2012 10:38:13 AM

This does the trick if you're using C/C++:

  1. while (true)
  2. {
  3. int* p = new int;
  4. }


Make sure your PC doesn't hang, though!
m
0
l
a b L Programming
a b } Memory
December 5, 2012 11:08:37 AM

I wouldn't expect that program to use any memory. Any decent compiler will realize that the statement has no effect and will optimize it to an empty loop.
m
0
l
Related resources
a b L Programming
December 5, 2012 11:38:55 AM

Ijack said:
I wouldn't expect that program to use any memory. Any decent compiler will realize that the statement has no effect and will optimize it to an empty loop.


That will not happen. Neither GCC, not Visual C++ does that.
m
0
l
a b L Programming
December 6, 2012 10:13:39 AM

Are you sure? I know that the C# compiler certainly will for a similar piece of code when building with optimisations, although it will leave it if you don't enable optimisations. eg.

  1. class Program
  2. {
  3. private static void Main(string[] args)
  4. {
  5. while (true)
  6. {
  7. int p = new int();
  8. }
  9. }
  10. }


Spits out this CIL without optimisations:

  1. .method private hidebysig static
  2. void Main (
  3. string[] args
  4. ) cil managed
  5. {
  6. // Method begins at RVA 0x2050
  7. // Code size 11 (0xb)
  8. .maxstack 1
  9. .entrypoint
  10. .locals init (
  11. [0] int32 p,
  12. [1] bool CS$4$0000
  13. )
  14.  
  15. IL_0000: nop
  16. IL_0001: br.s IL_0007
  17. // loop start (head: IL_0007)
  18. IL_0003: nop
  19. IL_0004: ldc.i4.0
  20. IL_0005: stloc.0
  21. IL_0006: nop
  22.  
  23. IL_0007: ldc.i4.1
  24. IL_0008: stloc.1
  25. IL_0009: br.s IL_0003
  26. // end loop
  27. } // end of method Program::Main


And with optimisations:

  1. .method private hidebysig static
  2. void Main (
  3. string[] args
  4. ) cil managed
  5. {
  6. // Method begins at RVA 0x2050
  7. // Code size 2 (0x2)
  8. .maxstack 8
  9. .entrypoint
  10.  
  11. // loop start
  12. IL_0000: br.s IL_0000
  13. // end loop
  14. } // end of method Program::Main


Obviously the compilers are different, but I'd imagine that those two that you mentioned would do something similar providing you enable the right optimisations.

@OP: Can't you just throw the exception manually? :lol: 
m
0
l
a b L Programming
December 6, 2012 1:21:45 PM

C# works differently than C++. C# that code wouldn't make sense due to garbage collector. In C#, you allocate and deallocate, which in the end doesn't change anything. In C++, all you do is allocate more and more. The heap keeps growing and it's changing.

In C++, however, there's no such thing.

Code compiled with full release optimizations:

  1. void main()
  2. {
  3. while (true)
  4. {
  5. int* p = new int;
  6. }
  7. }


  1. 000E1002 xor eax,offset __imp_operator new (0E20A0h)
  2. 000E1007 push 4
  3. 000E1009 call esi
  4. 000E100B add esp,4
  5. 000E100E jmp main+7 (0E1007h)


The first line is initializing function main.

The second line allocates 4 bytes for int* p pointer variable.

The third line calls "operator new".

The fourth line deallocates 4 bytes of the pointer, since its scope expires.

And the final line commands the program to return to line 2.

That's it. It loops until it fills the memory and crashes.
m
0
l
a b L Programming
a b } Memory
December 13, 2012 7:37:39 PM

I've tested your code in VS and you are correct; the compiler doesn't optimize it.

There is a snag though. The assembler code that you list indicates that you compiled this as a 32-bit program. So at most it can only use 2GB of memory. So the heap only grows as far as 2GB and then the program crashes. Compile it as a 64-bit program and it will use as much memory as anyone here is likely to have.
m
0
l
a b L Programming
December 13, 2012 8:13:35 PM

Gonna have to try it again. Tried compiling as 64 bit, however, it woulld still crash at 2 GB. Up until now, I used a .bat file to launch itself and the program. That way any PC would crash within 2 secs :) .
m
0
l
a b } Memory
December 17, 2012 12:02:09 PM

Most compilers I've used allow you to turn optimizations on or off.
m
0
l
December 24, 2012 4:22:34 AM

randomizer said:
Are you sure? I know that the C# compiler certainly will for a similar piece of code when building with optimisations, although it will leave it if you don't enable optimisations. eg.

  1. class Program
  2. {
  3. private static void Main(string[] args)
  4. {
  5. while (true)
  6. {
  7. int p = new int();
  8. }
  9. }
  10. }


Spits out this CIL without optimisations:

  1. .method private hidebysig static
  2. void Main (
  3. string[] args
  4. ) cil managed
  5. {
  6. // Method begins at RVA 0x2050
  7. // Code size 11 (0xb)
  8. .maxstack 1
  9. .entrypoint
  10. .locals init (
  11. [0] int32 p,
  12. [1] bool CS$4$0000
  13. )
  14.  
  15. IL_0000: nop
  16. IL_0001: br.s IL_0007
  17. // loop start (head: IL_0007)
  18. IL_0003: nop
  19. IL_0004: ldc.i4.0
  20. IL_0005: stloc.0
  21. IL_0006: nop
  22.  
  23. IL_0007: ldc.i4.1
  24. IL_0008: stloc.1
  25. IL_0009: br.s IL_0003
  26. // end loop
  27. } // end of method Program::Main


And with optimisations:

  1. .method private hidebysig static
  2. void Main (
  3. string[] args
  4. ) cil managed
  5. {
  6. // Method begins at RVA 0x2050
  7. // Code size 2 (0x2)
  8. .maxstack 8
  9. .entrypoint
  10.  
  11. // loop start
  12. IL_0000: br.s IL_0000
  13. // end loop
  14. } // end of method Program::Main


Obviously the compilers are different, but I'd imagine that those two that you mentioned would do something similar providing you enable the right optimisations.

@OP: Can't you just throw the exception manually? :lol: 

C# would just dump the memory every time the loop starts.
m
0
l
January 1, 2013 2:02:15 AM

When i try the above method,my system hangs and wen i allocate a calculated value of memory, my application waits till the memory is available and does its work without throwing any out of memory error.
What i want to do is- since evry process will be allocated a part of memory from os,my application wil also have its set of memory.I want to access its memory from an other process so that wen my application tries to further allocate its memory,there s no memory available and it throws the error.
m
0
l
a b L Programming
January 1, 2013 2:20:30 PM

That's not how it works. If you're out of memory, your system WILL HANG, instead of giving the error.
m
0
l
January 19, 2013 2:36:30 PM

With one extra line of code (really a change to an existing line of code) you could deliberately make it leak allocated memory each loop.
m
0
l
!