473,544 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do you use a garbage collector?

I followed a link to James Kanze's web site in another thread and was
surprised to read this comment by a link to a GC:

"I can't imagine writing C++ without it"

How many of you c.l.c++'ers use one, and in what percentage of your
projects is one used? I have never used one in personal or professional
C++ programming. Am I a holdover to days gone by?
Apr 10 '08
350 11480
Razii wrote:
On Thu, 10 Apr 2008 21:43:45 -0400, Arne Vajhřj <ar**@vajhoej.d k>
wrote:
>I can not imagine any C++ runtime that makes an operating system
call for each new.

The runtime allocates huge chunks from the OS and then manage
it internally.

Testing the keyword "new"

Time: 2125 ms (C++)
Time: 328 ms (java)

Explain that. What I am doing different in java than in c++? Code
below..
If you can prove that the only way to write inefficient code is to make
OS calls, then you have made your point.

But as everyone knows it is not, so your argument is completely bogus.

Arne
Apr 11 '08 #11
Sam
Razii writes:
On Thu, 10 Apr 2008 21:43:45 -0400, Arne Vajhøj <ar**@vajhoej.d k>
wrote:
>>I can not imagine any C++ runtime that makes an operating system
call for each new.

The runtime allocates huge chunks from the OS and then manage
it internally.
Testing the keyword "new"

Time: 2125 ms (C++)
Time: 328 ms (java)

Explain that. What I am doing different in java than in c++? Code
below..
What you're doing different in Java is that you're using the part of the
language that it's optimized for. Java is, generally, optimized for fast
instantiation of discrete objects on the heap, because creating a huge
number of objects is unavoidable in Java, and they all have to be allocated
on the heap, due to the nature of the language itself.

On the other case, in most use cases C++ does not require instantiation of
as many discrete objects on the heap that Java does, for implementing an
equivalent task. In C++, most -- if not all -- of these objects can be
easily allocated on the stack.

So, if you were to do a fair comparison, you should benchmark instantiation
of Java objects, on the heap, against instantiation of C++ objects on the
stack.

And then benchmark the comparable memory usage, to boot :-)

In your C++ example, there was no reason whatsoever to instantiate your C++
test object on the heap. What does that accomplish, besides a memory leak?
That's just the Java way of doing things, but, in C++ you have the option of
instantiating objects on the stack, so try benchmarking this instead:

int main(int argc, char *argv[]) {
clock_t start=clock();
for (int i=0; i<=10000000; i++)
{
Test testObj(i);

if (i % 5000000 == 0)
cout << &testObj << endl;
}

clock_t endt=clock();
std::cout <<"Time: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
}

See how well /that/ benchmarks against Java :-)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH/s0Xx9p3GYHlUOIR AvBpAJ9++ag+arL m2Tw7MUDh1C9Rse FvCACdG01b
bJVz24MY+1330Sy ZGmIWYbk=
=5Qsh
-----END PGP SIGNATURE-----

Apr 11 '08 #12
On Fri, 11 Apr 2008 02:04:18 GMT, "Mike Schilling"
<ms************ *@hotmail.comwr ote:
In fact, given how highly
optimized Java allocators are these days, the fact that C++ is taking less
than 7 times as long proves that it's *not* making an OS call.
with java -server it's 12 times slower...

Time: 2125 ms (g++)
Time: 172 ms (java - server)

Apr 11 '08 #13
On Thu, 10 Apr 2008 19:24:34 -0700, Arne Vajhøj <ar**@vajhoej.d kwrote:
[...]
But as everyone knows it is not, so your argument is completely bogus.
So what else is new?

What I can't figure out is, why do people continue to feed this troll,
especially when he keeps cross-posting these dopey threads to both the
C++ and Java newsgroups?

I've got as many thread filters to junk stuff he started as I do for
spam. He has really suckered a bunch of you in (including people I'm
surprised could be suckered).
Apr 11 '08 #14
On Fri, 11 Apr 2008 14:41:58 +1200, Ian Collins <ia******@hotma il.com>
wrote:
>Does the Java allocator/GC combination recycle the objects in the loop?
I doubt it (not sure though). It ends too fast for that. If it was
longer running program, then probably GC will kick in.
Apr 11 '08 #15
On Thu, 10 Apr 2008 21:29:44 -0500, Sam <sa*@email-scan.comwrote:
>See how well /that/ benchmarks against Java :-)

That is not the topic. The topic is how the keyword "new" behaves.

0x12ff5c
0x12ff5c
0x12ff5c
Time: 62 ms

All the references are the same -- not the same output as in the last
version (or java version).

Apr 11 '08 #16
On Thu, 10 Apr 2008 21:29:44 -0500, Sam <sa*@email-scan.comwrote:
>Java is, generally, optimized for fast
instantiatio n of discrete objects on the heap, because creating a huge
number of objects is unavoidable in Java, and they all have to be allocated
on the heap, due to the nature of the language itself.
As I said, Java allocates new memory blocks on it's internal heap
which is allocated in huge chunks from the OS. That's why the "new" in
Java is 12 times faster than C++ version. If there is any other
explanation, post it (I haven't seen it).

Apr 11 '08 #17
On Thu, 10 Apr 2008 20:31:49 -0500, Razii
<DO************ *@hotmail.comwr ote, quoted or indirectly quoted
someone who said :
>Creating 10000000 new objects with the keyword 'new' in tight loop.
All Java has to do is add N (the size of the object) to a counter and
zero out the object. In C++ it also has to look for a hole the right
size and record it is some sort of collection. C++ typically does not
move objects once allocated. Java does.

In my C++ days, we used NuMega to find leaks, objects that were
allocated butw never deleted even after there were no references to
them. We never got anywhere near nailing them all. With Java this is
automatic. You can't make that sort of screw up, though you can
packrat. See http://mindprod.com/jgloss/packratting.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Apr 11 '08 #18

"Razii" <DO************ *@hotmail.comwr ote in message
news:sq******** *************** *********@4ax.c om...
On Fri, 11 Apr 2008 02:04:18 GMT, "Mike Schilling"
<ms************ *@hotmail.comwr ote:
>In fact, given how highly
optimized Java allocators are these days, the fact that C++ is
taking less
than 7 times as long proves that it's *not* making an OS call.

with java -server it's 12 times slower...

Time: 2125 ms (g++)
Time: 172 ms (java - server)
Which doesn't change the conclusion.
Apr 11 '08 #19
On Fri, 11 Apr 2008 15:39:36 +1200, Ian Collins <ia******@hotma il.com>
wrote:
>runs in 90mS on my box (compared to ~940mS with default new).
Doesn't compile..
new.cpp:4:21: error: bad_alloc: No such file or directory
new.cpp:10: error: expected identifier before numeric constant
new.cpp:10: error: expected ',' or '...' before numeric constant
new.cpp: In static member function 'static void* Test::operator
new(size_t)':
new.cpp:19: error: invalid operands of types 'unsigned int' and 'const
unsigned
int ()(int)' to binary 'operator*'
new.cpp:22: error: ISO C++ forbids comparison between pointer and
integer
Apr 11 '08 #20

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

Similar topics

1
2297
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest...
4
2029
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in the set can connect to objects in the set. Every object inherits a interface to allow for a mark and sweep garbage collector (GC) and my...
10
2010
by: pachanga | last post by:
The Hans-Boehm garbage collector can be successfully used with C and C++, but not yet a standard for C++.. Is there talks about Garbage Collector to become in the C++ standard?
5
5481
by: Ben | last post by:
Could someone please verify if what I am doing as follow is corrected: 1. when dealing with custom class objects: ..... public myObject as myClass myObject as New myClass .......here I am going to fill up myObject with info....tons of them myObject = nothing System.GC.Collect()
13
3777
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system can manage resources as well as objects. The programming style is clear and easy, conforming to RAII (Resource Acquisition Is Initialization) idiom...
28
3137
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions...
142
6701
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory chunks and recycles them. This memory allocation strategy has been adapted to C (and C++) by the library written by Hans J Boehm and Alan J Demers. ...
8
1775
by: Paul.Lee.1971 | last post by:
Hi everyone, A program that I'm helping to code seems to slow down drastically during initialisation, and looking at the profiling graph, it seems to be the garbage collector thats slowing things down. I must point out that a heck of a lot of data are being read in and manipulated, and I was wondering if there were any metrics to show much of...
56
3621
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
46
2137
by: Carlo Milanesi | last post by:
Hello, traditionally, in C++, dynamically allocated memory has been managed explicitly by calling "delete" in the application code. Now, in addition to the standard library strings, containers, and auto_ptrs, gurus suggest that may be better to use a reference-counted smart pointer, or a garbage-collector. But in which cases it is better...
0
7414
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7598
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. ...
0
7757
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7360
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...
0
7699
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...
0
5895
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3398
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1833
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
1
982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.