Home arrow static arrow Java Programming [Archive] - WANTED: java.io.IOException: Cannot allocate memory
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - WANTED: java.io.IOException: Cannot allocate memory
This topic has 17 replies on 2 pages.    1 | 2 | Next »

Posts:10
Registered: 1/8/01
WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 5:20 AM



 
hi all!

anybody seen/knows about this stacktrace:

java.io.IOException: Cannot allocate memory at
java.net.PlainDatagramSocketImpl.send(Native Method) at
java.net.DatagramSocket.send(DatagramSocket.java:581) at

I have search the corr. PlainDatagramSocketImpl.c impl and didnt find anything about this message?

Facts: It has nothing to do with max packet size (65k) or so..

best regards

michael
 

Posts:19,725
Registered: 9/26/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 5:24 AM (reply 1 of 17)



 
Just sounds like what it says... You've chewed up all the memory by the time it gets to that call.
 

Posts:10
Registered: 1/8/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 5:26 AM (reply 2 of 17)



 
which memory? how can i tune it? ...jvm runtime heap mem is healthy and not the problem!

mb
 

Posts:18,384
Registered: 21.03.00
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 5:29 AM (reply 3 of 17)



 
Hi,

Are you sure? You would not get an out of memory, unless you were out of memory. Run the program in a memory profiler.

/Kaj
 

Posts:10
Registered: 1/8/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 5:44 AM (reply 4 of 17)



 
Hints/Facts:

Runtime: 1.4.1_05
OS: Redhat Linux Enterprise 3
Environment/Application Container: inside Bea WL (cluster 2 machines)

Background:

1. a Service (MBean) contin. polls the database and broadcast's this data via MulticastSocket.send
2. avg packet size 35k
3. send intervall <= 1 minute
4. there are 4-5 of such Services ...they all use a different port / group for broadcasting (maybe this is too much?)

 

Posts:3,055
Registered: 18/06/98
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 6:31 AM (reply 5 of 17)



 
I believe that you're mentioning this function in j2se/src/solaris/native/java/net/PlainDatagramSocketImpl.c
(although the directory mentions "solaris" the code is used in Linux as well). It's a failure of "malloc", that tries to allocate memory in the Native Heap.

JNIEXPORT void JNICALLJava_java_net_PlainDatagramSocketImpl_send(JNIEnv *env, jobject this,					   jobject packet) {...	/* When JNI-ifying the JDK's IO routines, we turned	 * read's and write's of byte arrays of size greater	 * than 2048 bytes into several operations of size 2048.	 * This saves a malloc()/memcpy()/free() for big	 * buffers.  This is OK for file IO and TCP, but that	 * strategy violates the semantics of a datagram protocol.	 * (one big send) != (several smaller sends).  So here	 * we *must* alloc the buffer.  Note it needn't be bigger	 * than 65,536 (0xFFFF) the max size of an IP packet.	 * Anything bigger should be truncated anyway.	 *	 * We may want to use a smarter allocation scheme at some	 * point.	 */...	fullPacket = (char *)malloc(packetBufferLen); 	if (!fullPacket) {	    JNU_ThrowOutOfMemoryError(env, "heap allocation failed");	    return;...


Unfortunately the message "heap allocation failed" disappeared somewhere in the JDK's Java source code and you're left with the "Out of memory" message. You need to find some reference to the -X and -XX options to adjust the size of the Native Heap (malloc failed because not all memory is available for JNI allocations; Java JVM reserves itself the most of it because it needs a contiguous area. Even if you have lots and lots of memory, if you don't pass the correct flags, the native heap could be too small for your needs. Native Heap is used as well by the compiled JIT code, so your performance can suffer if the native heap size is too small.
 

Posts:1,577
Registered: 00-02-25
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 6:37 AM (reply 6 of 17)



 
It's being thrown in the native code. Assuming that you're running on a Unix variant, the underlying call is send(3), which is documented here for Solaris: http://docs.sun.com/db/doc/806-0628/6j9vie803?a=view#indexterm-1568

This doc indicates that there's two memory-related errors: EMSGSIZE or ENOMEM. The latter I think refers to kernel memory, not application memory, so looking at the Java heap won't help you.

FWIW, on Unix at least, java.lang.OutOfMemoryError is thrown for a number of different causes, not all of them memory related. One that i've seen is running out of threads.
 

Posts:3,055
Registered: 18/06/98
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 6:44 AM (reply 7 of 17)



 
kdgregory has a good answer.
So you will have to reconfigure (relink?) the kernel and set some network parameters - I'm not a Linux wizard, but kdgregory's explanation sounds good for me.
 

Posts:1,577
Registered: 00-02-25
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 6:52 AM (reply 8 of 17)



 
lol, I was actually thinking you had the better answer
 

Posts:10
Registered: 1/8/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 7:06 AM (reply 9 of 17)



 
@kdgregory,edsonw: thank you ..!

@edsonw: the part of the code you showed is only called if MAX_PACKET_LEN (64k) is reached...but this is not the case. what are the tuning options for native heap...do yo have a link?

@kdgregory: sounds good...(or bad to us because kernel patching is not fun...at least for me)..
...can you imagine why this is happening only irregulary (for 1-5 minutes every few hour's) and not permanent...PLUS IF THE EXCEPTION HAPPENS THEN ON ALL MACHINES OF THE CLUSTER AT THE SAME TIME!!

mc

 

Posts:10
Registered: 1/8/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 4, 2004 7:16 AM (reply 10 of 17)



 
Could it be that this is the call/place where the error occurs? (because the java/io/IOException is thrown here!)

PlainDatagramSocketImpl send call's [Linux 2.4.21] socket.c sys_sendto (see below).

java/net/PlainDatagramSocketImpl.c

/*
* Send the datagram.
*
* If we are connected it's possible that sendto will return
* ECONNREFUSED indicating that an ICMP port unreachable has
* received.
*/
ret = NET_SendTo(fd, fullPacket, packetBufferLen, 0,
(struct sockaddr *)rmtaddrP, len);

if (ret < 0) {
switch (ret) {
case JVM_IO_ERR :
if (errno == ECONNREFUSED) {
JNU_ThrowByName(env, JNU_JAVANETPKG "PortUnreachableException",
"ICMP Port Unreachable");
} else {
NET_ThrowByNameWithLastError(env, "java/io/IOException", "sendto failed");
}
break;

case JVM_IO_INTR:
JNU_ThrowByName(env, "java/io/InterruptedIOException",
"operation interrupted");
break;
}
}

if (mallocedPacket) {
free(fullPacket);
}
return;

Linux 2.4.21, socket.c:

asmlinkage long sys_sendto(int fd, void * buff, size_t len, unsigned flags,
struct sockaddr *addr, int addr_len)
{
struct socket *sock;
char address[MAX_SOCK_ADDR];
int err;
struct msghdr msg;
struct iovec iov;

sock = sockfd_lookup(fd, &err);
if (!sock)
goto out;
iov.iov_base=buff;
iov.iov_len=len;
msg.msg_name=NULL;
msg.msg_iov=&iov;
msg.msg_iovlen=1;
msg.msg_control=NULL;
msg.msg_controllen=0;
msg.msg_namelen=0;
if(addr)
{
err = move_addr_to_kernel(addr, addr_len, address);
if (err < 0)
goto out_put;
msg.msg_name=address;
msg.msg_namelen=addr_len;
}
if (sock->file->f_flags & O_NONBLOCK)
flags |= MSG_DONTWAIT;
msg.msg_flags = flags;
err = sock_sendmsg(sock, &msg, len);

out_put:
sockfd_put(sock);
out:
return err;
}

int sock_sendmsg(struct socket *sock, struct msghdr *msg, int size)
{
int err;
struct scm_cookie scm;

err = scm_send(sock, msg, &scm);
if (err >= 0) {
err = sock->ops->sendmsg(sock, msg, size, &scm);
scm_destroy(&scm);
}
return err;
}

 

Posts:10
Registered: 1/8/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 5, 2004 4:44 AM (reply 11 of 17)



 
THE ANSWER IS (SHORTFORM): Never ask a multicastsocket if it is connected and if not reopen a new without closing the first!

so both of your answer are right....is there a way to assign ddollars to`2 answer's ?

mc

 

Posts:319
Registered: 11/8/00
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 5, 2004 4:47 AM (reply 12 of 17)



 
that depends are there any duke dollars in it?

no just kidding, click on the +Duke icon and you should beable to assign a given number of dollars. then click on the icon next to someone else and you should be able to assign more dukes.
 

Posts:19,725
Registered: 9/26/01
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 5, 2004 4:51 AM (reply 13 of 17)



 
THE ANSWER IS (SHORTFORM): Never ask a multicastsocket
if it is connected and if not reopen a new without
closing the first!
If you weren't closing your sockets, then you caused a leak, hence it leads into my first reply, doesn't it?
 

Posts:3,055
Registered: 18/06/98
Re: WANTED: java.io.IOException: Cannot allocate memory   
Aug 5, 2004 5:53 AM (reply 14 of 17)



 
Cool, I just want to know how you could deal with the problem, because if I would need to use multicast sockets, I know that it's not an open bug in Bug Database (and so being a deterrent factor for avoid using multicast sockets in Java apps. I've just started to search the Bug Database for "PlainDatagramSocketImpl" and read a lot of bug descriptions...).
Having the C source code available is very good, but it is not the cure for all illnesses. Good luck!
 
This topic has 17 replies on 2 pages.    1 | 2 | Next »