473,781 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bug in C++ .Net heap management

I believe I have found a serious performance
problem with heap management in C++ .Net.

I have an application does the following
thousands of times:
vint = new int[nn];
vchar = new char[nn];

The performance of these vector creations varies by
a factor of 40 or more. It is also sensitive to the
size and ratios in which the vectors are created.

Has anyone else had this problem?

I wrote a minimum console application to isolate
and validate the problem. Cycle this program 10 times
or more. The run time is about 0.3 seconds for the
first few iterations, then it increases to 16 seconds
per cycle and remains there.

This is NOT a problem with memory leak or memory
paging (the code is simple enough to rule this out).
I have also validated that no memory is leaking and
no paging occurs.

I speculate that if heap memory gets fragmented in
a certain way, then memory being released is not
properly consolidated, and allocation becomes slow.

I found no obvious way to send this bug report to
Microsoft. Any hints? Would they even want it?

/*************** *************** *************
C++ test program for "new" (constructor).
Test C++ .Net heap memory management.
*************** *************** *************/

#include <windows.h>
#include <conio.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, char * argv[])
{
int Nv = 500000; // pointer count
int mdd = 200; // max int vector
int mcc = 200; // max. char vector
int Nii = 1; // int arrays to allocate
int Ncc = 50; // char arrays to allocate
void ** pvoid;
int * pint;
char * pchar;
int dd, cc, seed, bytes, ii, jj, check, errs;
double secs = 0.0;
char YN;

start:
printf("\n C++ memory management test \n");
printf("loading memory \n");

seed = 4444;
srand(seed);
bytes = 0;
secs = double(GetTickC ount()); // start timer

pvoid = new void * [Nv]; // allocate pointers
bytes += 8 + Nv * 4;

for (ii = 0; ii < Nv; ) // index thru pointers
{
for (jj = ii; (jj < ii + Nii) && (jj < Nv); jj++)
{
dd = 2 + rand() % mdd;
pint = new int[dd]; // allocate ints
pvoid[jj] = (void *) pint; // set pointer
check = jj;
pint[0] = check; // add check data
pint[dd-1] = -check;
bytes += 8 + dd * 4;
}
ii = jj;

for (jj = ii; (jj < ii + Ncc) && (jj < Nv); jj++)
{
cc = 2 + rand() % mcc;
pchar = new char[cc]; // allocate chars
pvoid[jj] = (void *) pchar; // set pointer
check = jj % 128;
pchar[0] = check; // add check data
pchar[cc-1] = -check;
bytes += 8 + cc+2;
}
ii = jj;
}

secs = (double(GetTick Count()) - secs) / 1000.0;
printf("seconds : %.3f bytes: %d \n",secs,bytes) ;

printf("releasi ng memory \n");

srand(seed); // re-generate same randoms
errs = 0;
secs = double(GetTickC ount());

for (ii = 0; ii < Nv; ) // index thru pointers
{
for (jj = ii; (jj < ii + Nii) && (jj < Nv); jj++)
{
dd = 2 + rand() % mdd;
pint = (int *) pvoid[jj]; // get ints
check = jj; // validate
if (pint[0] != check) errs++;
if (pint[dd-1] != -check) errs++;
delete [] pint; // delete
}
ii = jj;

for (jj = ii; (jj < ii + Ncc) && (jj < Nv); jj++)
{
cc = 2 + rand() % mcc;
pchar = (char *) pvoid[jj]; // get chars
check = jj % 128; // validate
if (pchar[0] != check) errs++;
if (pchar[cc-1] != -check) errs++;
delete [] pchar; // delete
}
ii = jj;
}

delete [] pvoid; // delete array of pointers

secs = (double(GetTick Count()) - secs) / 1000.0;
printf("seconds : %.3f \n",secs);
printf("errors: %d \n\n",errs);

printf("enter Y to test again ");
YN = getche();
if ((YN == 'Y') || (YN == 'y')) goto start;

return 0;
}
Nov 16 '05 #1
7 1749
Michael Cornelison wrote:
I believe I have found a serious performance
problem with heap management in C++ .Net.


After 100 or more iterations, I didn't notice any increase in running time
for your program.

What compiler version are you using (7.0 or 7.1)? What command-line
options? What OS? Service pack?

-cd

Nov 16 '05 #2
No problems here too.

50 iterations, 0 increase time
VS 7.1, WinXP SP1

Cheers,
Stoyan
"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Michael Cornelison wrote:
I believe I have found a serious performance
problem with heap management in C++ .Net.


After 100 or more iterations, I didn't notice any increase in running time
for your program.

What compiler version are you using (7.0 or 7.1)? What command-line
options? What OS? Service pack?

-cd

Nov 16 '05 #3
Carl,

Thanks for checking this out.

Looks like something is amiss in my system, but it is completely
up to date, and this leaves me clueless.

My system:
- Win XP Pro with all the MS patches installed as of Oct. 1.
- Visual C++ .Net standard edition. There are no updates, I believe.
(Help says it is Version 7.0.9466)
- compiler options:
/D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D"_MBCS"
/GF /FD /EHsc /MT /GS /Gy /YX"stdafx.h" /Fp"./C++ new tester.pch"
/Fo"./" /Fd"./vc70.pdb" /W3 /nologo /c /Wp64 /TP

Are you running the professional edition? Could this be the difference?

Mike C.

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Michael Cornelison wrote:
I believe I have found a serious performance
problem with heap management in C++ .Net.


After 100 or more iterations, I didn't notice any increase in running time
for your program.

What compiler version are you using (7.0 or 7.1)? What command-line
options? What OS? Service pack?

-cd

Nov 16 '05 #4
Michael Cornelison wrote:
Carl,

Thanks for checking this out.

Looks like something is amiss in my system, but it is completely
up to date, and this leaves me clueless.

My system:
- Win XP Pro with all the MS patches installed as of Oct. 1.
- Visual C++ .Net standard edition. There are no updates, I
believe. (Help says it is Version 7.0.9466)
- compiler options:
/D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D"_MBCS"
/GF /FD /EHsc /MT /GS /Gy /YX"stdafx.h" /Fp"./C++ new
tester.pch" /Fo"./" /Fd"./vc70.pdb" /W3 /nologo /c /Wp64 /TP

Are you running the professional edition? Could this be the
difference?


I'm running Visual C+ .NET 2003 Enterprise Architect (Version 7.1.3088).
I'm also on Win XP SP1 with up to date patches.

I didn't do an optimized build, so my results should be comparable with
yours (it's the same compiler in standard edition except that standard
edition lacks the optimizer. The libraries are identical).

Perhaps you're seeing something that was fixed in VC7.1? I have another
machine with VC7 and Windows 2000 - I'll give it a try there to see if maybe
it's a VC7 thing.

-cd
Nov 16 '05 #5
Michael Cornelison wrote:
Carl,

Thanks for checking this out.

Looks like something is amiss in my system, but it is completely
up to date, and this leaves me clueless.

My system:
- Win XP Pro with all the MS patches installed as of Oct. 1.
- Visual C++ .Net standard edition. There are no updates, I
believe. (Help says it is Version 7.0.9466)
- compiler options:
/D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D"_MBCS"
/GF /FD /EHsc /MT /GS /Gy /YX"stdafx.h" /Fp"./C++ new
tester.pch" /Fo"./" /Fd"./vc70.pdb" /W3 /nologo /c /Wp64 /TP

Are you running the professional edition? Could this be the
difference?


I was able to reproduce the behavior you're seeing - the key is /MT. This
repros on Windows 2000 and Windows XP, with VC7 and VC7.1.

Apparently the multi-threaded heap is getting into a very bad way with this
allocation pattern. Windows XP implements something they call the "Low
Fragmentation Heap", which may be helpful in this case. I don't have time
to tryu it at the moment, but you might want to look the LFH up on MSDN and
see if it makes a difference.

-cd
Nov 16 '05 #6
Carl,

Will do. Thanks again.

I was able to reproduce the behavior you're seeing - the key is /MT. This
repros on Windows 2000 and Windows XP, with VC7 and VC7.1.

Apparently the multi-threaded heap is getting into a very bad way with this allocation pattern. Windows XP implements something they call the "Low
Fragmentation Heap", which may be helpful in this case. I don't have time
to tryu it at the moment, but you might want to look the LFH up on MSDN and see if it makes a difference.

-cd

Nov 16 '05 #7
Carl,

See KB article 323635, "Heap Performance Problems in Windows 2000 and
Windows XP"

I did what this article advised, and my "slowdown" problem has disappeared.

Looks like MS shot themselves in the foot when they removed the small block
heap management within the C runtime library. Fortunately they still allow
it to be re-engaged on request. My understanding is that malloc() can
allocate large blocks and use these for small block allocations which it
manages itself.

Thanks for your help.

Mike C.

"Carl Daniel [VC++ MVP]" <cp******@nospa m.mvps.org> wrote in message
news:ON******** *******@TK2MSFT NGP10.phx.gbl.. .
I was able to reproduce the behavior you're seeing - the key is /MT. This
repros on Windows 2000 and Windows XP, with VC7 and VC7.1.

Apparently the multi-threaded heap is getting into a very bad way with this allocation pattern. Windows XP implements something they call the "Low
Fragmentation Heap", which may be helpful in this case. I don't have time
to tryu it at the moment, but you might want to look the LFH up on MSDN and see if it makes a difference.

-cd

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
5052
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
3
1972
by: Aravindakumar Venugopalan | last post by:
Hi , A Windows form application which interacting with the unmanaged C++ codes . In unmanaged c++ code we allocate around 130MB on the heap for annalysing high resolution images . Earlier during the processing ee do lot of process on the image and the memory reaches high at one point of time to 1.2GB , after that we clear all the memory being used so the memory in the task manager comes to really low. Also I am calling CompactHeap...
24
2896
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as are structs, where classes are on the heap... when value types go out of scope the memory is re- allocated, object remain in memory waiting to be cleaned up by the garbage collector, etc, but he responded 'so why not just put say a class on the...
16
4451
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
1
3615
by: Chris Mullins | last post by:
We've been using the SSLStream class found in System.Net.Security to build a giant Sockets server that provides TLS encryption at the channel leve. Before .Net 2.0, we used an open-source encryption channel from Mentalis, and have even looked at the Mono implementation for doing this. The problem comes from the SSLStream not doing any buffer management. None. Zero. In the "no buffer management" case, each SSLStream allocates bufferes...
53
26403
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
11
5216
by: Andy Watson | last post by:
I have an application that scans and processes a bunch of text files. The content I'm pulling out and holding in memory is at least 200MB. I'd love to be able to tell the CPython virtual machine that I need a heap of, say 300MB up front rather than have it grow as needed. I've had a scan through the archives of comp.lang.python and the python docs but cannot find a way to do this. Is this possible to configure the PVM this way? ...
9
3174
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. In contrast, malloc() always takes memory in the heap. Now, let's consider the code as follows: int get_buffer() {
5
24804
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS (I am new to Windows so not sure). We are currently bouncing the instance to overcome this error. This generally happen at the end of business day only (So maybe memory might be getting used up?). We have already increased the statement heap & ...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.