Results 1 to 4 of 4
  1. #1
    diolu is offline Member
    Join Date
    Jan 2017
    Posts
    26
    Rep Power
    0

    Default Where to declare array for optimal memory management.

    I have made a simple GUI app. One of the functions I have defined needs an array for its internal purpose. For example:

    void next() {
    int[] var = new int[1000];
    ...
    }
    Now I wonder what truly happens. Does the virtual machine allocate a new array each time the method is invoked? In this case, I should move the big array elsewhere. Or is the array allocated only once; in which case, my code should be fine? Where is the right place to declare an array?

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    286
    Rep Power
    11

    Default Re: Where to declare array for optimal memory management.

    Yes it does allocate memory every time, but at the same time it becomes eligible for gc as well, when the method call is over.
    Swastik

  3. #3
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    286
    Rep Power
    11

    Default Re: Where to declare array for optimal memory management.

    Please see the answer of your other post.
    Swastik

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Where to declare array for optimal memory management.

    If you are concerned about over allocating then use ArrayList. It reallocates as necessary and is easier to use than arrays.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 0
    Last Post: 12-12-2013, 08:25 AM
  2. Replies: 0
    Last Post: 10-14-2013, 11:37 AM
  3. Jotm (Java Open Transaction Management)- Heap Memory Issues
    By hariprashanth in forum Advanced Java
    Replies: 0
    Last Post: 12-18-2008, 11:42 AM
  4. Servlet memory management
    By chilljava in forum Java Servlet
    Replies: 0
    Last Post: 06-23-2008, 10:53 PM
  5. [SOLVED] Servlet memory management
    By chilljava in forum Java Servlet
    Replies: 0
    Last Post: 06-23-2008, 10:33 PM

Tags for this Thread

Posting Permissions

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