• First10 (unregistered)

    First, First, First, First, First, First, First, First, First, First.

  • Roman (unregistered)

    Heh.

    P.S. No, my comment is not spam!

  • First10 (unregistered)

    First, Frist,First, Frist,First, Frist,First, Frist,First, Frist,

  • snoofle (cs)

    I did something vaguely like that a very long time ago, but not in a loop. Our system needed to slurp up pretty much all the memory our (then) modest Unix box could muster. Depending upon what you were doing, the right combination of inputs could cause enough stuff to be loaded into cache that we'd get an out of memory error.

    Bigger boxes were ordered, but because of budgets, weren't going to arrive for a while.

    The requirement was that when it happened, we were to pop a dialog saying it was out of memory and that the system would exit, so the user wouldn't just sit there with a hung system. But how do you get the memory for a dialog if you're out of memory?

    Simple, pre-allocate an array to reserve enough bytes that, when freed, would allow the dialog to be created.

  • ekolis (cs)

    So, if it fails ten times, then you just give up and return a reference to the original object? Aargh!

  • BadCode (unregistered)

    Reminds me of an application I was asked to support, the original author had decided it would be easy to create 'upfront' 4096 objects of each needed type (WinForms, TextBoxes, ListBoxes, Panels, etc. etc. etc.) and store them in individual arrays, with ultra complex logic to determine which Form/control was available.

    The said logic for upfront creation was 'to ensure we do not fail later on' while creating objects due to Out Of Memory conditions)

  • dohpaz42 (cs) in reply to snoofle
    snoofle:
    The requirement was that when it happened, we were to pop a dialog saying it was out of memory and that the system would exit, so the user wouldn't just sit there with a hung system. But how do you get the memory for a dialog if you're out of memory?

    Simple, pre-allocate an array to reserve enough bytes that, when freed, would allow the dialog to be created.

    +1

  • Art (unregistered)

    It really annoys me to see code where the programmer obviously thought "OK, something might go horribly wrong here, but if it does, I'll just shrug it off and muddle on."

    Instead of looping 10 times and then fail, it should have been an infinite loop with sleep calls so that the program will simply pause until the required memory is available and then proceed to complete its assigned task.

  • no u (unregistered)
    Why 10?

    Because 9 didn't work.

  • no u (unregistered)
    Why 10?

    Because 9 didn't work.

  • VeryBestDeveloperNumber1 (unregistered)
    Why 10?

    Because seven eight nine.

  • what that guy said (unregistered) in reply to no u
    no u:
    Why 10?

    Because 9 didn't work.

    8 more to go

  • PS (unregistered)

    I like how it just returns the original after 10 tries.

    "Could you clone please? please? oh, no? Well, nevermind then..."

    Captcha: nisl

  • atk (unregistered) in reply to Art
    Art:
    It really annoys me to see code where the programmer obviously thought "OK, something might go horribly wrong here, but if it does, I'll just shrug it off and muddle on."

    Instead of looping 10 times and then fail, it should have been an infinite loop with sleep calls so that the program will simply pause until the required memory is available and then proceed to complete its assigned task.

    I agree that sleeps are better, but I do not agree about an infinite loop. If the system is unrecoverably out of memory, an infinite loop would look like a hang rather than providing opportunity to gracefully handle the failure. Graceful recovery would then require a finite number of tries. Perhaps a configurable timeout would have been useful ("after M minutes, we're really out of memory, so just give up").

    And 10 may simply have been good enough to recover the application in the environments where the problem exhibited itself. Not all problems are fully understood when they're patched - sometimes a tweak here or there hides a deeper, extremely difficult to diagnose problem. And if the product does what it's supposed to do, under the conditions in which it is used, is that not good enough until the problem is better understood? Or would you prefer a production-down situation to an ugly, temporary workaround?

    In such cases, it would be good to insert a comment, "Regarding the magic number 10: it just seems to work, and I don't have any better idea how to do this, right now." If the code reviewers don't have a better suggestion, and the component is not critical enough for an architect to look at, then it may well be good enough.

  • Wonk (unregistered)

    10 GC calls on the stack, 10 GC calls. pop on off, pray that it worked, 9 GC calls on the stack.....

  • monkeyPushButton (unregistered) in reply to BadCode
    BadCode:
    Reminds me of an application I was asked to support, the original author had decided it would be easy to create 'upfront' 4096 objects of each needed type (WinForms, TextBoxes, ListBoxes, Panels, etc. etc. etc.) and store them in individual arrays, with ultra complex logic to determine which Form/control was available.

    The said logic for upfront creation was 'to ensure we do not fail later on' while creating objects due to Out Of Memory conditions)

    I don't know why, but this made me just want to curl up under my desk.
  • Men without hair (unregistered)

    Say, Can we clone? Can we clone? Memory out of control Can we clone? Can we clone? We're doing it from wall to wall Can we clone? Can we clone? Everybody try again! Can we clone? Can we clone? Everybody do it again!

    SafelyClone() Is it safe to clone? Is it safe to clone?

  • Jon E. (unregistered)

    Luke, Han Solo and Princess Lea used the garbage collector to escape from imperial storm troopers, and storm troopers are clones. So there.

  • frits (cs)

    Added proper commenting:

    public static IClonable SafelyClone(IClonable a)
    {
        //Set b equal to a because the only thing worse than throwing exceptions is returning null references.
        IClonable b = a;
        //Try to clone object up to 10 times.  It should never take more than 9 times.
        for (int i = 0; i < 10; i++)
        {
            try
            {
                b = a.Clone();
                break;
            }
            //Should never happen.
            catch (OutOfMemoryException)
            {
                //Force garbage collection.
                GC.Collect(GC.MaxGeneration);
                //Added because every Microsoft garbage collection example shows a call to WaitForPendingFinalizers.
                GC.WaitForPendingFinalizers();
            }
        }
        //Return the "cloned" object. (heh)
        return b;
    }
    
  • TryAgain (unregistered)

    Dunno why the stupid programmer did not try again after the GC was invoked.

    I mean, we probably freed up some memory, lets try again 10 times ...

  • Matt Westwood (cs) in reply to Jon E.
    Jon E.:
    Luke, Han Solo and Princess Lea used the garbage collector to escape from imperial storm troopers, and storm troopers are clones. So there.

    +1 Classic.

  • Melnorme (cs)

    Send in the clones

  • frits (cs) in reply to Men without hair
    Men without hair:
    Say, Can we clone? Can we clone? Memory out of control Can we clone? Can we clone? We're doing it from wall to wall Can we clone? Can we clone? Everybody try again! Can we clone? Can we clone? Everybody do it again!

    SafelyClone() Is it safe to clone? Is it safe to clone?

    You forgot: C C, O O, D D, E E, S S, O O, D D

  • Tyler Durden (unregistered)

    I thought the first rule of optimization is: you do not talk about optimization.

  • STAR WARS FAN (unregistered) in reply to Jon E.
    Jon E.:
    Luke, Han Solo and Princess Lea used the garbage collector to escape from imperial storm troopers, and storm troopers are clones. So there.
    YOU FORGOT CHEWBACCA!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! HOW COULD YOU!?!?!?! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!!
  • STAR WARS FAN!!!!!!!!!!!! (unregistered) in reply to Matt Westwood
    Matt Westwood:
    Jon E.:
    Luke, Han Solo, [FUCKING CHEWBACCA] and Princess Lea used the garbage collector to escape from imperial storm troopers, and storm troopers are clones. So there.

    +1 Classic.

    NO!!!! MINUS 100000000000000!!!! HE FORGOT CHEWBACCA!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • C-Octothorpe (cs)

    I like how his cloning logic doesn't actually, well, you know, CLONE!!!

    Here, let me fix the code sample:

    public static IClonable SuperDuperSafeCloneIPromise(IClonable a)
    { return a; }

  • Adam (unregistered) in reply to TryAgain
    TryAgain:
    Dunno why the stupid programmer did not try again after the GC was invoked.

    I mean, we probably freed up some memory, lets try again 10 times ...

    That's what the code does. Tries again and again until no exception is thrown. Or 10 is hit.

  • ShatteredArm (cs)

    I've always said, the best "clone" method is one where you never know whether you actually got a clone.

  • ac (unregistered) in reply to snoofle
    snoofle:
    But how do you get the memory for a dialog if you're out of memory?

    Simple, pre-allocate an array to reserve enough bytes that, when freed, would allow the dialog to be created.

    Of course, the real WTF would be to pre-allocate the dialog, right?
  • CHEWBACCA FAN (unregistered) in reply to STAR WARS FAN
    STAR WARS FAN:
    Jon E.:
    Luke, Han Solo and Princess Lea used the garbage collector to escape from imperial storm troopers, and storm troopers are clones. So there.
    YOU FORGOT CHEWBACCA!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! HOW COULD YOU!?!?!?! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!! CCCHHHEEEWWWBBBAAACCCCCCAAA!!!!!!!!

    He didn't forget Chewbacca. Chewbacca did not "escape" from the storm troopers. He graciously allowed them to live, by taking an alternative route back to the ship.

    Nothing can kill a Wookie.

  • C-Octothorpe (cs) in reply to ac
    ac:
    snoofle:
    But how do you get the memory for a dialog if you're out of memory?

    Simple, pre-allocate an array to reserve enough bytes that, when freed, would allow the dialog to be created.

    Of course, the real WTF would be to pre-allocate the dialog, right?
    Isn't that what windows has always done?
  • R. A. Salvatore (unregistered) in reply to CHEWBACCA FAN
    CHEWBACCA FAN:

    He didn't forget Chewbacca. Chewbacca did not "escape" from the storm troopers. He graciously allowed them to live, by taking an alternative route back to the ship.

    Nothing can kill a Wookie.

    Except, y'know... having a moon land on top of them.

  • The Nerve (unregistered)

    Fixed?

    public static IClonable SafelyClone(IClonable a)
    {
        IClonable b = a;
        for (int i = 0; 
                 i < XmlConfigurationSingleton.getInstance().getIntProperty(XmlConfigurationSingleton.PROPERTY_SAFELY_CLONE_RETRIES); 
                 i++)
        {
            try
            {
                b = a.Clone();
                break;
            }
            catch (OutOfMemoryException)
            {
                GC.Collect(GC.MaxGeneration);
                GC.WaitForPendingFinalizers();
            }
        }
        return b;
    }
    
  • TheCPUWizard (cs) in reply to C-Octothorpe
    1. NEVER call GC.Collect from "Real" code. For any company/project/team I am involved with, that is an immediate firable effect.

    2. Do not think you relly know what the GC is doing (unless you really are an expert). In fact from a purist perspective there is no Grbage Collector.

    Hint: Temporary Objects to NOT cause issues, they prevent them. The overhead of GC is based much more of the complexity of the LIVE object graph. The frequency of GC is dependant on the total allocation rate...

    So, would you rather have 1 GC that runs for 200mS, or 100 GC's that each run for 25uS?????

  • OB1 (unregistered) in reply to R. A. Salvatore
    R. A. Salvatore:
    CHEWBACCA FAN:

    He didn't forget Chewbacca. Chewbacca did not "escape" from the storm troopers. He graciously allowed them to live, by taking an alternative route back to the ship.

    Nothing can kill a Wookie.

    Except, y'know... having a moon land on top of them.

    That's no moon...

  • Llamas (unregistered)

    Can anyone come up with a reason (sane or not) for making the assignment to b on the method's first line?!

  • aptent (unregistered) in reply to TheCPUWizard
    TheCPUWizard:
    1) NEVER call GC.Collect from "Real" code. For any company/project/team I am involved with, that is an immediate firable effect.
    1. Do not think you relly know what the GC is doing (unless you really are an expert). In fact from a purist perspective there is no Grbage Collector.

    Hint: Temporary Objects to NOT cause issues, they prevent them. The overhead of GC is based much more of the complexity of the LIVE object graph. The frequency of GC is dependant on the total allocation rate...

    So, would you rather have 1 GC that runs for 200mS, or 100 GC's that each run for 25uS?????

    How much do you pay for rent in your ivory tower?

  • STAR WARS FAN!!!!!!!!!!!! (unregistered) in reply to STAR WARS FAN!!!!!!!!!!!!
    Comment held for moderation.
  • Llamas (unregistered) in reply to Llamas
    Comment held for moderation.
  • Star Wars N3rd (unregistered) in reply to OB1
    OB1:
    R. A. Salvatore:
    CHEWBACCA FAN:

    He didn't forget Chewbacca. Chewbacca did not "escape" from the storm troopers. He graciously allowed them to live, by taking an alternative route back to the ship.

    Nothing can kill a Wookie.

    Except, y'know... having a moon land on top of them.

    That's no moon...
    ...that's a space station.

    That's one of the things that has always confused me...why did they continually refer to it as a "station?" Never in the first movie did it even orbit a planet. It was mobile, so how was it not simply a space ship?

  • Coyne (cs)

    Dr Evil: (evil laugh) Clones!

    Dr Evil: (even more evil laugh) Ve must haf more clones!

    Dr Evil: Arrrrrrrrgggggghhh!!!!!! Out of memory??!! Vhy must my evil plans always be foiled???!!

  • C-Octothorpe (cs) in reply to aptent
    aptent:
    TheCPUWizard:
    1) NEVER call GC.Collect from "Real" code. For any company/project/team I am involved with, that is an immediate firable effect.
    1. Do not think you relly know what the GC is doing (unless you really are an expert). In fact from a purist perspective there is no Grbage Collector.

    Hint: Temporary Objects to NOT cause issues, they prevent them. The overhead of GC is based much more of the complexity of the LIVE object graph. The frequency of GC is dependant on the total allocation rate...

    So, would you rather have 1 GC that runs for 200mS, or 100 GC's that each run for 25uS?????

    How much do you pay for rent in your ivory tower?
    The blood of one junior developer... at the 1st of every month.

  • I think I'm a clone now (unregistered)

    There's always 10 of me just a-hanging around.

    (Until we're garbage collected.)

  • C-Octothorpe (cs) in reply to I think I'm a clone now
    I think I'm a clone now:
    There's always 10 of me just a-hanging around.

    (Until we're garbage collected.)

    Damnit! Now I'll have that song stuck in my head for the rest of the day...

  • Coyne (cs) in reply to CHEWBACCA FAN
    CHEWBACCA FAN:
    Nothing can kill a Wookie.

    Not even the Wookie Monster?

  • Nagesh (unregistered)
    Comment held for moderation.
  • CHEWBACCA!!!!!!!!~! (unregistered) in reply to Star Wars N3rd
    Star Wars N3rd:
    OB1:
    That's no moon...
    ...that's a space station.

    That's one of the things that has always confused me...why did they continually refer to it as a "station?" Never in the first movie did it even orbit a planet. It was mobile, so how was it not simply a space ship?

    Because the Death Star is so fucking awesome that it doesn't move through the universe, the universe moves around IT!!!!!

  • Zylon (cs) in reply to Star Wars N3rd
    Star Wars N3rd:
    That's one of the things that has always confused me...why did they continually refer to it as a "station?" Never in the first movie did it even orbit a planet. It was mobile, so how was it not simply a space ship?
    ISO naming standards, doi.
  • Star Wars N3rd (unregistered) in reply to CHEWBACCA!!!!!!!!~!
    CHEWBACCA!!!!!!!!~!:
    Star Wars N3rd:
    OB1:
    That's no moon...
    ...that's a space station.

    That's one of the things that has always confused me...why did they continually refer to it as a "station?" Never in the first movie did it even orbit a planet. It was mobile, so how was it not simply a space ship?

    Because the Death Star is so fucking awesome that it doesn't move through the universe, the universe moves around IT!!!!!
    That's the other thing: why wasn't it called "Death Moon" or "Death Planet"? Everyone thought it looked like a moon and nothing orbited it. The only time it got shiny was when firing lazers.

Leave a comment on “Clone Collector”

Log In or post as a guest

Replying to comment #:

« Return to Article