473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

producer-consumer? no, a race to stop memory filling up!

dh
Runtime.getRuntime().exec() ... mad buffering of stdout ...

It buffers stdout without limit ...
so if your Java program doesn't keep up with the Process output then
memory fills up!
I wish it just blocked instead because my Java program is loosing
the race against the C program that it exec()s.

The pattern I want is "producer-consumer"
and i'm suprised that it seems imposible.

what can I do?

Here is a small example that illustrates the issue ...

5523:fosters:~: cat Test.java

class Test {
public static void main(String[] args) {
try {
Process p =
Runtime.getRuntime().exec("/usr/bin/yes");
for (;;) {
Thread.sleep(1000);
System.out.println("tick");
Thread.sleep(1000);
System.out.println("tock");
}
}
catch (Exception x) {
x.printStackTrace();
}
}
}
5524:fosters:~: rm *.class
5525:fosters:~: /usr/local/IBMJava2-13/bin/javac Test.java
5526:fosters:~: /usr/local/IBMJava2-13/bin/java Test
tick
tock
tick
tock
java.lang.OutOfMemoryError
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
[etc....]
Jul 17 '05 #1
3 2727
What do you mean by "loosing the race" if you perform sleeps and do not even
attemt to read the proces output?

Silvio Bierman

"dh" <dh@digitalbrain.com> wrote in message
news:c7*************************@posting.google.co m...
Runtime.getRuntime().exec() ... mad buffering of stdout ...

It buffers stdout without limit ...
so if your Java program doesn't keep up with the Process output then
memory fills up!
I wish it just blocked instead because my Java program is loosing
the race against the C program that it exec()s.

The pattern I want is "producer-consumer"
and i'm suprised that it seems imposible.

what can I do?

Here is a small example that illustrates the issue ...

5523:fosters:~: cat Test.java

class Test {
public static void main(String[] args) {
try {
Process p =
Runtime.getRuntime().exec("/usr/bin/yes");
for (;;) {
Thread.sleep(1000);
System.out.println("tick");
Thread.sleep(1000);
System.out.println("tock");
}
}
catch (Exception x) {
x.printStackTrace();
}
}
}
5524:fosters:~: rm *.class
5525:fosters:~: /usr/local/IBMJava2-13/bin/javac Test.java
5526:fosters:~: /usr/local/IBMJava2-13/bin/java Test
tick
tock
tick
tock
java.lang.OutOfMemoryError
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
tock
tick
[etc....]

Jul 17 '05 #2
"Silvio Bierman" <sb******@idfix.nl> writes:
What do you mean by "loosing the race" if you perform sleeps and do not even
attemt to read the proces output?


He means that "yes" is printing output like mad, and Java is reading
it into a buffer, leading to an OOM.

He expected "yes" to stop producing output when Java didn't feel like
reading it anymore.

If you do "yes | more" then yes will print 24 lines of output, and
more will show them. Then more waits for the user to hit space,
causing the yes process to stall. When the user hits space, then
more reads another 24 lines and stops. This causes yes again to
stall. Even if the user never hits space, then memory never gets
full, because the yes process is prevented from running.

But in the Java case, the yes process keeps running, and Java keeps
buffering its output. Why?
I'm not an expert, however, and I might be misinterpreting the
situation. I wonder what the situation really is ;-)

Kai
Jul 17 '05 #3
I ran your test on Wonka and it works fine (even with -Xmx=4M), so it looks
like this is a bug in IBM's implementation (and maybe in Sun's too).

Normally a process writing to a pipe will block when there is a certain
amount of unread data in the pipe, so they must indeed be deliberately
buffering inside the VM heap. Report a bug.

--
Chris Gray ch***@kiffer.eunet.be
/k/ Embedded Java Solutions

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Wai Yip Tung | last post by:
I'm attempting to turn some process than uses callback to return result into a more user friendly generator. I'm hitting some road block so any pointer would be appriciated. Let say there is an...
2
by: mag31 | last post by:
This is a technically challenging question but it would help me significantly if anyone can answer it. In essence I have the following: A factory class, which can create producer classes which...
3
by: Vaddina Prakash Rao | last post by:
Hello every one, In our Linux Encoding system, a copy of helix dna producer is running ..... i wrote an applicatoin which can remotely communicate with the encoding machine ... Its function...
4
by: Reinhold Schalk | last post by:
Hello, somewhere i've read that using strong names does assure two things: 1. Assure that the content of the assembly is not modified (that's ok in my opinion) 2. Assure that the assembly is...
1
by: Jakub Okaj | last post by:
I would like to use ASP like PageProducer in Delphi CGI aplication. How can I do this? Please help. QBA
5
by: cozsmin | last post by:
hello , as u know wait() and notify() will not thow an exception if the method that calls them has the lock , or esle i misundrestood java :P this is the code that throws (unwanted) ...
2
by: dwhall | last post by:
I have 2 python scripts: examples of a producer and a filter, respectively: #! /usr/bin/env python import sys, time if __name__ == "__main__": while True: sys.stdout.write("hello.\r\n")...
12
by: test63 | last post by:
with which program, i can make a 3d game(like world of warcraft, alladin.....)? chears-
4
hemantbasva
by: hemantbasva | last post by:
We have designed an aspx page having five ajax tab in it. the data of first four are designed on page whereas for the fifth tab it renders a user control named MYDOMAIN. in the tab container's even...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.