9

With its built-in garbage collection, Java allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse.

AFAIK Garbage Collector usually runs when your app runs out of memory. it holds a graph that represents the links between the objects and isolated objects can be freed.

Though we have System.gc(), but if you write System.gc() in your code the Java VM may or may not decide at runtime to do a garbage collection at that pointas explained by this post System.gc() in Java

So I was having some doubts regarding the Garbage collection process of java.

  • I wonder if there is such method in java like free() as such in C language, that we could invoke when we explicitly want to free the part of memory allocated by a new operator.

  • Also does new performs the same operation as do malloc() or calloc()?

  • Are there any alternates for delete(), free(), malloc(), calloc() and sizeof() methods in java.

Community
  • 1
  • 1
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
  • 1
    Java handles everything... Regarding memory management there isn't much you can do without seriously causing trouble. Just make sure all your references are nulled and the gc will do its job. – initramfs Feb 06 '14 at 09:15
  • 3
    If you **need** to manage memory yourself, then Java is not the right technology for you. If you just **want** to manage memory yourself, then you should first find out whether you really **need** that. For most applications, you don't. – linac Feb 06 '14 at 10:47

4 Answers4

7

No, there aren't. Java is not c, and you're not supposed to manage memory explicitly.

Andres
  • 10,561
  • 4
  • 45
  • 63
  • 4
    could you please provide some explanation according to the question. – Sahil Mahajan Mj Feb 06 '14 at 09:26
  • 2
    though you have provided a small but a very good answer, but this thing I already know. Thats why I posted this question to know how it could be managed, because their could be some memory crucial applications. But thanks anyways – Sahil Mahajan Mj Feb 06 '14 at 09:41
  • You can manage memory indirectly through JVM and GC configuration. But you should not do it on an explicit way. – Andres Feb 06 '14 at 09:45
6

AFAIK Garbage Collector usually runs when your app runs out of memory.

Little disagree on that. No. It runs asynchronously and collects the referenced memory locations.

I wonder if there is such method in java like free() as such in C language, that we could invoke when we explicitly want to free the part of memory allocated by a new operator.

Again System.gc() is your call then, but not 100% sure of memory clear immediately.

Also does new performs the same operation as do malloc() or calloc()?

If you mean allocation memory, then yes for that Object

Are there any alternates for delete(), free(), malloc(), calloc() and sizeof() methods in java.

AFAIK there is no direct functions to do so.

On top of my head, you need not to worry about such things and Modern JVM's are smart enoguh to handle these things.

An interesting thread here found on SO, to when GC decides to run. Hope that helps.

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • 1
    I would mention `finalize()` but one should **never** use it. – initramfs Feb 06 '14 at 09:19
  • +1 good shot, but it would be better if you could provide with some explanation about **When the System.gc() is supposed to perform garbage collection**. – Sahil Mahajan Mj Feb 06 '14 at 09:20
  • 2
    @CPUTerminator `finalize` doesn't release the memory. It performs whatever actions need to be done for an object before it's memory can be released. – Cruncher Feb 06 '14 at 14:23
  • `new` is not at all similar to `malloc()`; it is much more lightweight. I recall that `new Object()` compiles into *eight* machine instructions on a modern JIT – compare that to `malloc()` which takes hundreds of instructions. – ntoskrnl Feb 06 '14 at 15:07
  • @Cruncher Hmm okay... Thought it was the method gc used to clean memory. – initramfs Feb 06 '14 at 15:08
  • @CPUTerminator finalize is a last attempt to fix broken state. To close streams, files and sockets that should have been closed by the program but weren't or freeing native window handles from AWT, etc. . This happens in a different thread, no guaranteed order, no guarantee when or even if they are called at all. Finalize also slows down GC since objects with finalize have to be enqueued to run it and cannot be immediately cleaned up. – josefx Feb 09 '14 at 20:46
1

I haven't worked on this particularly but I have read it as my knowleadge enhancement of java nio. In nio we have a bytebuffer what it seemed to me it can be java version of malloc.

A buffer is essentially a block of memory into which you can write data, which you can then later read again. This memory block is wrapped in a NIO Buffer object, which provides a set of methods that makes it easier to work with the memory block.

Syntax:

ByteBuffer buf = ByteBuffer.allocate(24);

For more reading ByteBuffer.

Helios
  • 851
  • 2
  • 7
  • 22
  • Right, but it's not very different from `new byte[24]` or `new ArrayList(24)`. You can't free the `ByteBuffer` memory and it's not "general purpose memory", e.g., you can't use it for objects. – maaartinus Feb 06 '14 at 15:26
  • we can use direct ByteBuffer and then we can use `Cleaner` to free the memory. Correct me if I am wrong? – Helios Feb 07 '14 at 08:33
0

In Java, we have System.gc() which is basically used for invoking garbage collection explicitly. But, one should potentially avoid that since it shows the gaps un-filled. You can probably have a look at this: stackoverflow

However, Java performs this task of garbage collection itself when the system runs out of memory. All you can do on application level is, to assign null to all your unused variables and objects which make them unuable and allows JVM to perform garbage collection.

Community
  • 1
  • 1
Bhaskar
  • 337
  • 6
  • 21
  • yes, thats why System.gc() is avoided sometimes. But does assigning null to unused variables guarantees that Garbage collector will be called for sure at that point.? – Sahil Mahajan Mj Feb 06 '14 at 09:23
  • Hi Sahil, no it doesn't gurantee that garbage collector will be called. Just say, that it makes an object eligible for being shelled out of memory whenver garbage collector in running. – Bhaskar Feb 06 '14 at 09:40
  • @SahilMahajanMj: No it's actually unrelated. But you may be lucky and the JVM can find out that the object lives only during the method call and allocate it on stack. Not every JVM does it and there's no guarantee. You can't depend on it and you needn't care. – maaartinus Feb 06 '14 at 15:24