Results 1 to 4 of 4
  1. #1
    xetrus is offline Member
    Join Date
    Mar 2017
    Posts
    1
    Rep Power
    0

    Default Dynamic memory allocation in Java

    Hi All,

    I am new to Java however I have done a lot in C++. I have got a quite simple question however couldn't find an answer so far in any Java documentation.

    Java allows to dynamically allocate memory at run time for newly created objects or arrays using the new keyword. The same is normally done in C++. However in Java the programmer is not responsible for freeing this memory. It is done by the system as soon as the variable (array or object pointer) is out of the scope. In contrast in C++, the programmer uses the delete keyword to explicitly release the allocated memory. Therefore in C++ the same pointer variable can be used many times to allocate and release the memory using multiple new - delete statements. So I asked a question what happens if the same pointer variable is used many times to allocate memory using the new. It looks like each call to the new allocates a new block of memory but does not release the previous block. this is demonstrated by the following simple program:

    public class Class_M
    {

    public static void main(String[] args)
    {
    int[] pA;
    int[] pB;
    int i;

    pA = new int[10]; //Allocate and initialise array pA
    for(i=0; i<10;++i)
    pA[i] = i;

    pB = pA; //Assign pA array pointer to pB pointer

    //Print size and elements of array pB
    System.out.println("First Array");
    System.out.println("pB Size = "+pB.length);
    for(i=0; i<pB.length;++i)
    System.out.println("pB["+i+"] = "+pB[i]);

    //Re-initialise array pA and print elements of array pB
    System.out.println("Second Array");
    for(i=0; i<10;++i)
    pA[i] = 2*i;
    for(i=0; i<pB.length;++i)
    System.out.println("pB["+i+"] = "+pB[i]);

    //Re-allocate memory for array pA and print elements of array pB
    pA = new int[20];
    for(i=0; i<20;++i)
    pA[i] = 3*i;
    System.out.println("Third Array");
    System.out.println("pB Size = "+pB.length);
    for(i=0; i<pB.length;++i)
    System.out.println("pB["+i+"] = "+pB[i]);

    }

    }
    The output follows:
    First Array
    pB Size = 10
    pB[0] = 0
    pB[1] = 1
    pB[2] = 2
    pB[3] = 3
    pB[4] = 4
    pB[5] = 5
    pB[6] = 6
    pB[7] = 7
    pB[8] = 8
    pB[9] = 9
    Second Array
    pB[0] = 0
    pB[1] = 2
    pB[2] = 4
    pB[3] = 6
    pB[4] = 8
    pB[5] = 10
    pB[6] = 12
    pB[7] = 14
    pB[8] = 16
    pB[9] = 18
    Third Array
    pB Size = 10
    pB[0] = 0
    pB[1] = 2
    pB[2] = 4
    pB[3] = 6
    pB[4] = 8
    pB[5] = 10
    pB[6] = 12
    pB[7] = 14
    pB[8] = 16
    pB[9] = 18

    It follows from this output that the pointer pB points to a block of memory that is no longer associated with initial pointer pA used for allocation and this block is available for reading from it.
    So if using multiple allocation and large arrays, eventually the system will be out of memory.

    Finally, my question is if it is possible to implement in Java multiple rounds of allocation of objects or arrays to the same variable that provides the release of previously allocated memory?

    Many thanks

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,342
    Rep Power
    12

    Default Re: Dynamic memory allocation in Java

    First off, java doesn't have pointers. Everything is pass by value. Second, garbage collection is responsible for cleanup, and you have little control over that. Garbage collection will kick in when the JVM sees fit, if it's running low on memory, as part of a cycle, or when you call System.gc() (not generally recommended).

    eventually the system will be out of memory.
    Eventually yes, assuming all those arrays are still in active scope. Garbage collection follows a particular chain, and if your object is no longer reachable, it is flagged for collection. You can overflow the heap if you create a HUGE number of unique objects, but repeated allocation should just eventually get garbage collected.

    P.S. Please use [code] ... [/code] tags around your code samples you post here for easy reading!
    Last edited by quad64bit; 03-10-2017 at 02:53 AM.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Dynamic memory allocation in Java

    pB points to a block of memory that is no longer associated with initial pointer pA
    Not true. pA and PB both point to the same block of memory until they are assigned a new value.

    is possible to implement in Java multiple rounds of allocation of objects or arrays to the same variable that provides the release of previously allocated memory
    Yes. When a new value is assigned to a variable, the association with its old value is gone. If no other variables refer to that value, it can be collected.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Dynamic memory allocation in Java

    If you want to have a little more control over what gets GC'd and when, I recommend you look at the various Reference implementations (WeakReference, and SoftReference).

    And just so there is no confusion your pointers are similar to Java references except that you can't have a reference to a primitive like a pointer in C. However to augment a previous statement, in both languages, arguments to methods are passed by value, even pointers in C.

    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. Huge memory allocation
    By mctommek in forum Advanced Java
    Replies: 2
    Last Post: 12-08-2011, 01:06 PM
  2. Memory Allocation
    By nathey in forum New To Java
    Replies: 1
    Last Post: 07-18-2011, 03:48 AM
  3. JNI Memory Allocation
    By Smokin' Caterpillar in forum Advanced Java
    Replies: 3
    Last Post: 09-16-2010, 05:00 PM
  4. Memory Allocation
    By zzpprk in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2010, 01:14 AM
  5. Memory Allocation
    By kishan in forum New To Java
    Replies: 3
    Last Post: 09-19-2009, 05:47 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
  •