In a context where exceptions are caught, I ran
the fragment:
   cerr << "allocating" << endl;
   char*    arr[100];
   for (int jj = 0; jj < 10; ++jj)
   {
      cerr << "jj = " << jj << endl;
      arr[jj] = new char[2000000000];
      sleep (30);
   }
   sleep (10);
   for (int jj = 0; jj < 10; ++jj)
      delete[] arr[jj];
   cerr << "deleted" << endl;
The exception was caught with jj = 1, i.e., on the second allocation.
But on top, I see:
top - 14:08:46 up  5:21, 10 users,  load average: 0.04, 0.06, 0.08
Tasks: 158 total,   1 running, 157 sleeping,   0 stopped,   0 zombie
Cpu(s):  2.7%us,  2.0%sy,  0.0%ni, 94.5%id,  0.6%wa,  0.1%hi,  0.1%si,  
0.0%st
Mem:   1941908k total,  1231804k used,   710104k free,   138372k buffers
Swap:  3899384k total,        0k used,  3899384k free,   876020k cached
and the "710104k free" suggests that I should have failed on the first
allocation.  Furthermore, the allocation did not alter the values
in top (except for a little jitter).
Wherein do I err?
[~]$ uname -a
Linux xxxxx 2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST 2011 i686 
i686 i386 GNU/Linux
Thanks,
Mike.
Michael D. Berger wrote:> In a context where exceptions are caught, I ran > the fragment: > > cerr << "allocating" << endl; > char* arr[100]; > for (int jj = 0; jj < 10; ++jj) > {<snip>> Wherein do I err?It would have been caught on 0 if that was jj++, *not* ++jj (increment *after* the loop, not before). mark
centos-bounces at centos.org wrote:> In a context where exceptions are caught, I ran > the fragment: > > cerr << "allocating" << endl; > char* arr[100]; > for (int jj = 0; jj < 10; ++jj) > { > cerr << "jj = " << jj << endl; > arr[jj] = new char[2,000,000,000]; // This line changed by me tounderscore what you're doing.> sleep (30); > } > sleep (10); > for (int jj = 0; jj < 10; ++jj) > delete[] arr[jj]; > cerr << "deleted" << endl; > > The exception was caught with jj = 1, i.e., on the second allocation. > > But on top, I see: > > top - 14:08:46 up 5:21, 10 users, load average: 0.04, 0.06, 0.08 > Tasks: 158 total, 1 running, 157 sleeping, 0 stopped, 0 zombie > Cpu(s): 2.7%us, 2.0%sy, 0.0%ni, 94.5%id, 0.6%wa, 0.1%hi, > 0.1%si, > 0.0%st > Mem: 1941908k total, 1231804k used, 710104k free, 138372k buffers > Swap: 3899384k total, 0k used, 3899384k free, 876020k cached > > and the "710104k free" suggests that I should have failed on the first > allocation. Furthermore, the allocation did not alter the values > in top (except for a little jitter). > > Wherein do I err?Holy RAMbo, batman! How many GB of RAM do you intend to allocate? Once you allocate 2GB like you did, you MUST be running a bigmem or x64 kernel to allocate another 2GB. You won't see 'new'd memory as "taken" in top(8) because malloc() is a bug. Bits are set in a page table, but no memory is actually written to, and nothing really changes, until the program attempts to write. What you did was fall off the end of a bit string keeping track of malloc()'d pages, that's all. (ps mark, the ++jj or jj++ takes place after the first loops' action, not before .:. ++jj and jj++ have identical effect). Insert spiffy .sig here //me ******************************************************************* This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept for the presence of computer viruses. www.Hubbell.com - Hubbell Incorporated**
On Mar 3, 2011, at 2:21 PM, "Michael D. Berger" <m_d_berger_1900 at yahoo.com> wrote:> In a context where exceptions are caught, I ran > the fragment: > > cerr << "allocating" << endl; > char* arr[100]; > for (int jj = 0; jj < 10; ++jj) > { > cerr << "jj = " << jj << endl; > arr[jj] = new char[2000000000]; > sleep (30); > } > sleep (10); > for (int jj = 0; jj < 10; ++jj) > delete[] arr[jj]; > cerr << "deleted" << endl; > > The exception was caught with jj = 1, i.e., on the second allocation.In 32-bit a process can only allocate 3GB, 1GB is reserved for the kernel. I don't know what your trying to do, but I'm sure your could do it a whole lot better the pre-allocating all available memory. Don't go by top it only shows committed memory, not reserved. -Ross