The Artima Developer Community
Sponsored Link

Weblogs Forum
The Positive Legacy of C++ and Java

210 replies on 15 pages. Most recent reply: May 9, 2009 2:50 AM by Daesung Park

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 210 replies on 15 pages [ « | 1 ... 2 3 4 5 6 7 8 9 10 ... 15  | » ]
James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 1:50 PM
Reply to this message Reply
Advertisement
> Whatever the reason, we had no problem with memory
> consumption before using a garbage collected environment.
> Or after that, for that matter. In fact, there was an
> engineer in our team who worked on implementing a machine
> translation in Java before and warned us up front that
> they had to re-write their system in C (I think) because
> it was consuming too much memory, but his experience was
> ignored.

Most "memory problems" I have seen in Java programs are related to the application code and have nothing to do with Java per se. There is some overhead to using Java but most of the time the real issue is doing things like taking each object you create and adding it to a collection and then iterating over that collection instead of working with each object as you create it. Just through simple changes like that you can easily reduce the memory footprint of a java app by 10X, even 100X and stabilize the heap size.

Again I don't know C#, but one big gotcha with Strings in Java is that when you substring a large string, the underlying array is shared between the old String and the new String. So your original String object is released and GC'd but the underlying char array is not. If you have lots of big Strings, it looks like your Strings are not being GC'd because the array is the bulk of the memory used by the String. If C# Strings work the same way, I'd give you even odds that that was your problem.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 3:07 PM
Reply to this message Reply
> Again I don't know C#, but one big gotcha with Strings in
> Java is that when you substring a large string, the
> underlying array is shared between the old String and the
> new String. So your original String object is released
> and GC'd but the underlying char array is not. If you
> have lots of big Strings, it looks like your Strings are
> not being GC'd because the array is the bulk of the memory
> used by the String. If C# Strings work the same way, I'd
> give you even odds that that was your problem.

Indeed. Together with the old behaviour of StringBuffer, this has been the underlying cause of many String related performance issues.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 3:17 PM
Reply to this message Reply
> If C# Strings work the same way, I'd
> give you even odds that that was your problem.

I hate to belabor the point here but a lot time when people are new to Java (and I expect C#) and have a memory issue, they focus on trying to get 'garbage collection to work' usually by attempting to force GC.

That approach is a huge waste of time. The garbage collector works (although I've read that Microsoft's is not very sophisticated in relation to various JVMs but that's just hearsay) just fine. What they should be doing is figuring out why their objects are still reachable. The result is a bunch of futility and either giving up or putting in some hacks and never resolving the root issue.

Paul M. Dubuc

Posts: 19
Nickname: pmd
Registered: Oct, 2004

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 8:13 PM
Reply to this message Reply
>Most "memory problems" I have seen in Java programs are >related to the application code and have nothing to do with >Java per se.

I can say the same for C++. It uses less overhead and gives more control over resource deallocation.

C++ is still my favorite language, especially now that there is a standard library and the Boost libraries. Too many projects gave it up too soon for Java, I think.

Nemanja Trifunovic

Posts: 172
Nickname: ntrif
Registered: Jun, 2004

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 9:46 PM
Reply to this message Reply
James,

Instead of guessing what was wrong in the code you never saw and I hardly remember, I suggest you do a simple experiment. Write a piece of code that looks like:

while (true)
{
String s = GenerateSomeRandomString();
}

and watch the memory consumption grow. Now, this is not going to lead to a memory exhaustion, but watch the amount of virtual memory that it consumes before the GC kicks in. Even better, look at the number of page faults.

Mike O'Keefe

Posts: 16
Nickname: kupci2
Registered: Mar, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 10:37 PM
Reply to this message Reply
> > <p>Because I was on the C++ Standards Committee, I saw
> > these decisions
> So we know committees don’t work even if they are
> comprised of experts. I’d have thought that the community

I was wondering if anyone was going to catch this point. Seems that the author doesn't like features if he wasn't part of the "design by committee", take his stance on checked exceptions, there are very good points on the opposing side. The debate continues, and pleasing everybody is, well, read on ...

> process would be a great thing because of “community”
> involvement. I don’t know much about JCP but even with
> that model it’s like a hit or miss thing – Concurrency JSR
> I think was a success but few say that about Generics JSR.

The interesting thing about the Concurrency libs is that they were mostly written by a few people, and then brought into Java 1.5, whereas with generics, while there were various competing strategies, I don't think anything out there had quite as established and proven as util.concurrent. So the Generics debate went back and forth, in more of a "design by committee" fashion, trying to please everybody, and so they made some technical shortcuts, mainly to ensure they wouldn't have to change the JVM, almost exactly the same sort of thing that made CORBA bindings overly complicated, because of some limitations imposed by IBM in the standards.

One other interesting point on the recent closures debate, going on in a similar fashion to the generics thing, is that Gosling originally planned to have closures in the language (I'm sure this is one of Eckel's complaints, given his Ruby leanings).

I'm not against the JCP, in fact I'm a member, but I'd rather have a clean design, without shortcuts, and keeping to the original, simple philosophy of Java, than a "try and please everybody". This gets harder and harder as the language gets to be as important and significant as Java. As another example, look at the difficulty with Microsoft and Windows, changes are not easy.

Somehow Linus manages, it seems, better with Linux, but perhaps because he doesn't have shareholders breathing down his neck, and keeps to his "release it when it's ready" philosophy. And with his hard-edged no-nonsense personality, he probably could care less about his detractors, no matter how much they complain. As you can see with Eckel's and Generics, they are never satisfied.

Mike O'Keefe

Posts: 16
Nickname: kupci2
Registered: Mar, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 11:06 PM
Reply to this message Reply
> experiment. Write a piece of code that looks like:
>
> while (true)
> {
> String s = GenerateSomeRandomString();
> }
>
> and watch the memory consumption grow. Now, this is not

Send in your GenerateSomeRandomString method (by the way, in Java, methods start with lower case by convention, and one should avoid endless loops, not good practice in general), and maybe we can help find your memory leak ;)

But on your other post, about the engineer advising you to rewrite in C because "Java uses too much memory", sure, we've all heard that ad-nauseum so to speak, to use Eckel's words, it's tedious, tiresome and wrong. Surely you're not going to assert that there are less memory leaks in C++ code than Java?

Too bad, because I've worked with C coders learning Java and they've said the same thing, the same misunderstanding about the language and wrote very poor code, because they were afraid of creating objects, was one example I remember. And who hasn't worked on an 8000 line C Style Java class?

And yes, I know C++ programmers, who, once they learned Java, have never looked back, so you are in the minority. And these are folks writing transaction systems for the banks (when you swipe your credit card, for example), so performance and stability is key.

But hey, it's a free country, enjoy. I like working in Qt myself.

Mike O'Keefe

Posts: 16
Nickname: kupci2
Registered: Mar, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 18, 2009 11:41 PM
Reply to this message Reply
> > <p>It also fooled the Java designers who didn't
> understand
> > C++ well
> > enough.
[snip]
> >
> I'm not sure where you got the impression that the choice
> of leaving out operator overloading was because someone
> didn't do their homework. I remember asking this Gosling
> in one of my interviews of him why he left out operator
> overloading (I don't think that question and answer ended
> up getting published). What he basically said to me, is
> what I heard him say in other contexts: Gosling felt that
> the level of operator overloading abuse he had seen in
> practice in other languages (I'm not sure if this was just
> C++, but certainly included C++) had outweighed the
> benefits of it. That's all. It was a subjective design
> choice.
>
I agree on the other languages part. Eckels seems to be unfortunately perpetuating the myth that Java is based on C++, and they just rewrote the language leaving stuff out, and adding the gc, i.e. it's not Java, it's C++ ++, etc: and because he's written some books on the subject, and should understand the language differences, you'd think he'd know this than believe the urban legends and whatnot.

However, many get this wrong. Despite the fact that Java on the surface looks similar to C++, this is a superficial resemblance only.

http://cs.gmu.edu/~offutt/classes/642/Resources/java-objc.html

">> Java's *syntax* may resemble C++, but it has no similarity to C++
>> as a language. Java's chief *semantics* are dynamically-bound and
>> use single inheritance, class objects, and an extensive runtime system.
>> C++ and Modula-3 are as far away from this model as any object-oriented
>> language can be.
>> Java is clearly semantically derivative of Smalltalk and other
>> languages related to it. Most notably, NeXT's
>> Objective-C is almost uncannily similar to Java: single inheritance,
>> dynamic binding, dynamic loading, "class" objects, interfaces,
>> and now methods stored as data (a-la Java's "reflection" library),
>> all-virtual functions, you name it. It's almost weird.

[snip]
this case, they are right on. When I left Sun to go to NeXT, I thought
Objective-C was the coolest thing since sliced bread, and I hated C++.
So, naturally when I stayed to start the (eventually) Java project, Obj-C
had a big influence. James Gosling, being much older than I was, he had
lots of experience with SmallTalk and Simula68, which we also borrowed
from liberally.

"

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 4:48 AM
Reply to this message Reply
> while (true)
> {
> String s = GenerateSomeRandomString();
> }
>
> and watch the memory consumption grow. Now, this is not

You really should try such suggestions yourself. Here is a complete example which I ran using default parameters with Java 1.6u10

import java.lang.management.MemoryMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
 
import java.util.Random;
 
public class TestGC
{
	public static void main(String[] args)
	{
		Random rand = new Random();
		MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
		MemoryUsage usage = memory.getHeapMemoryUsage();
		System.out.println("init="+usage.getInit());
		System.out.println("used="+usage.getUsed());
		System.out.println("committed="+usage.getCommitted());
		System.out.println("max="+usage.getMax());
		for (int i=0; i<10; i++)
		{
			int iteration=0;
			long t0 = System.currentTimeMillis();
			while (true)
			{
				String s = String.valueOf(rand.nextInt());
				if (++iteration == 100)
				{
					iteration = 0;
					if (System.currentTimeMillis()-t0 > 15000)
						break;
				}
			}
			usage = memory.getHeapMemoryUsage();
			System.out.println();
			System.out.println("used="+usage.getUsed());
			System.out.println("committed="+usage.getCommitted());
		}
	}
}


init=0
used=439552
committed=5177344
max=66650112

used=988008
committed=51773 44

used=241416
committed=5177344

etc
The committed memory did not increase. The used memory oscillated up and down in the range 180K up to about 1M.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 4:53 AM
Reply to this message Reply
> The interesting thing about the Concurrency libs is that
> they were mostly written by a few people, and then brought
> into Java 1.5, whereas with generics, while there were
> various competing strategies, I don't think anything out
> there had quite as established and proven as
> util.concurrent. So the Generics debate went back and
> forth, in more of a "design by committee" fashion, trying
> to please everybody, and so they made some technical

It is much easier try proposed libraries in real applications than language changes. So most people ignored the generics debate altogether, some watched from the sidelines, and very select few actually tried it out.

John Buckley

Posts: 1
Nickname: nhoj
Registered: Mar, 2009

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 5:12 AM
Reply to this message Reply
> the way to get the masses of C programmers to move to objects was to make the move transparent: to allow them to compile their C code unchanged under C++. This was a huge constraint, and has always been C++'s greatest strength ... and its bane. It's what made C++ as successful as it was, and as complex as it is.

Bruce,

Perhaps I misunderstand what you mean by compatibility with C, but I think it's interesting to compare C++ with Objective-C. Both were started around the same sort of time and both had 100% compatibility with C as a design goal. However Objective-C turned out to be a very small and simple, but powerful, superset of C; whilst C++ turned out to be much more complex and difficult to use as you describe. I have programmed in both languages and have no doubt at all which language I prefer (Objective-C).

So perhaps compatibility with C need not have been the huge constraint that you suggest it was. Were there other design goals which lead to the language's complexity and "stiffness"?

John

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 5:18 AM
Reply to this message Reply
I collected some extra information from my test.
The test runs for approximately 150s

Garbage collection
Copy: 49151 collections, 3881ms
MarkSweepCompact: 0 collections, 0ms
characterCount=7732603027, stringCount=774609800

stringCount is the total number of strings generated, and characterCount is the number of characters in those strings.

Krisztian Sinka

Posts: 30
Nickname: skrisz
Registered: Mar, 2009

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 5:53 AM
Reply to this message Reply
> Too bad, because I've worked with C coders learning Java
> and they've said the same thing, the same misunderstanding
> about the language and wrote very poor code, because they
> were afraid of creating objects, was one example I
> remember. And who hasn't worked on an 8000 line C Style
> Java class?

I think everyone can write poor code on any language. I think this should not be a surprise for anyone.

> And yes, I know C++ programmers, who, once they learned
> Java, have never looked back, so you are in the minority.
> And these are folks writing transaction systems for the
> e banks (when you swipe your credit card, for example), so
> performance and stability is key.

I am working with telecomm server applications. There are some points where we are close to the hardware and the OS. Our preferences are performance and stability as well. But we happily manage using C++.

Disregarding the thing that most of the hardwer and 3rd party component vendors ships their library in C, I think it would not be impossible to use Java as well.

The languange can predefine many things but I think the more important point is in that:
how the one can understand the problem, the languange selected, the design principles and the platform.

This makes the difference between coders and engineers.

John Wellbelove

Posts: 72
Nickname: garibaldi
Registered: Mar, 2008

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 6:51 AM
Reply to this message Reply
> I don't remember explicitly calling
> delete once in the last five years or so: exclusive
> ownership + deterministic destructors work just fine.

That is my experience as well.
'new' & 'delete' are rare occurrences in my apps nowadays.

Dare I say that C++ programmers who took to Java with such enthusiasm were maybe not writing that good C++ in the first place. Their inappropriate or overuse of manual memory allocation in C++ was translated into normal syntax in Java.

Mark Thornton

Posts: 275
Nickname: mthornton
Registered: Oct, 2005

Re: The Positive Legacy of C++ and Java Posted: Mar 19, 2009 7:26 AM
Reply to this message Reply
> > I don't remember explicitly calling
> > delete once in the last five years or so: exclusive
> > ownership + deterministic destructors work just fine.
>
> That is my experience as well.
> 'new' & 'delete' are rare occurrences in my apps
> nowadays.
Whereas in my work (graph algorithms, vehicle routing, computational geometry), if I was using C++ they would be routine and unavoidable.

Flat View: This topic has 210 replies on 15 pages [ « | 2  3  4  5  6  7  8  9  10 | » ]
Topic: Should I use a netbook as my main development platform? Previous Topic   Next Topic Topic: Social Newsfeeds: The Next Big Thing


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us