Results 1 to 5 of 5

Thread: memory problems

  1. #1
    super_juanjo is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default memory problems

    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

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    To force garbage collection you can use System.gc()
    This is not usually necessary - are you sure you aren't just trying to allocate an array that's too big anyway?

    Of course, if one of the things you are doing in "some stuff" makes the array reachable, the garbage collector won't throw it away.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Also if I am correct, I think that the GC will only release the memory if less than 40% of the heap is used...
    Who Cares... As Long As It Works...

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I don't think so, that would completely defeat the point of it.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Sorry... Trying to remember back to college is sometimes hard... I new that 40 was some number that had to do with garbage collection, so I did some quick research... And 40% and 70% have to do with resizing the heap when the JVM preforms gc();

    Tuning Garbage Collection with the 1.4.2 Java[tm] Virtual Machine
    Who Cares... As Long As It Works...

Similar Threads

  1. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  2. JVM memory usage
    By lardum in forum New To Java
    Replies: 7
    Last Post: 06-26-2008, 03:30 AM
  3. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 08:55 AM
  4. Memory
    By mew in forum CLDC and MIDP
    Replies: 0
    Last Post: 12-28-2007, 11:02 AM
  5. Consumption of memory
    By Daniel in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 09:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •