memory problems

Discussion in 'Java' started by jj, Apr 17, 2009.

  1. jj

    jj Guest

    Hi



    I have a very simple function that creates memory, do something with
    it, and returns:

    static void test(int k)
    {
    byte [] buff = new byte[k];

    //do some stuff with buff[]

    buff=null;

    }

    After a few calls to this function, it runs out memory
    In C++ I would use delete at the end, and here in java I've been told
    that GC takes care of it, but it seems that it does not
    Am I doing something wrong? how can I free this temp memory after I
    have used it?

    many thanks
    jj, Apr 17, 2009
    #1
    1. Advertising

  2. jj wrote:
    > Hi
    >
    >
    >
    > I have a very simple function that creates memory, do something with
    > it, and returns:
    >
    > static void test(int k)
    > {
    > byte [] buff = new byte[k];
    >
    > //do some stuff with buff[]
    >
    > buff=null;
    >
    > }
    >
    > After a few calls to this function, it runs out memory
    > In C++ I would use delete at the end, and here in java I've been told


    > that GC takes care of it, but it seems that it does not

    It does. Whatever is going wrong is due to some aspect which does not
    appear in the outline code you have provided. Possibly because you are
    retaining a reference to the allocated memory somewhere other than in
    the 'buff' variable.

    Mark Thornton
    Mark Thornton, Apr 17, 2009
    #2
    1. Advertising

  3. jj

    Mark Space Guest

    jj wrote:

    > Am I doing something wrong?


    Yes and no. It is not normally necessary to null out references to
    release them.

    static void test(int k)
    {
    byte [] buff = new byte[k];
    //do some stuff with buff[]
    }

    Works fine.


    > how can I free this temp memory after I
    > have used it?



    The GC does it for you. As mentioned already, the problem with memory
    is not here. Look elsewhere in your code.
    Mark Space, Apr 17, 2009
    #3
  4. jj

    Roedy Green Guest

    On Fri, 17 Apr 2009 14:02:32 -0700 (PDT), jj
    <> wrote, quoted or indirectly quoted
    someone who said :

    >I have a very simple function that creates memory, do something with
    >it, and returns:
    >
    >static void test(int k)
    >{
    > byte [] buff = new byte[k];
    >
    > //do some stuff with buff[]
    >
    > buff=null;
    >
    >}



    Write yourself an SSCCE. You will discover a program like that above
    fleshed out does NOT run out of memory, even if you leave out the
    buff=null.

    see http://mindprod.com/jgloss/sscce.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Now for something completely different:
    http://www.youtube.com/watch?v=9lp0IWv8QZY
    Roedy Green, Apr 18, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mahesh Prasad
    Replies:
    1
    Views:
    697
    Tom Wells
    Feb 22, 2004
  2. Cy Huckaba
    Replies:
    1
    Views:
    1,138
    Xie Xiao
    Jun 26, 2003
  3. Julián Sanz García

    RAM Memory or virual memory

    Julián Sanz García, Nov 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    890
    Julián Sanz García
    Nov 12, 2004
  4. humbleaptience
    Replies:
    0
    Views:
    5,478
    humbleaptience
    Feb 22, 2006
  5. Nathan Bates
    Replies:
    1
    Views:
    1,206
    Tim Peters
    Jul 4, 2006
Loading...

Share This Page