我正在尝试进行一些内存管理,在其中分配了一个大的,文件支持的内存,对它进行了一些处理,然后我想忘记它-也就是说,将其从RAM中清除掉因此我可以对其他文件进行一些处理。我过去曾经成功使用过from synthesizer import Player, Synthesizer, Waveform
import numpy as np
import time
BITRATE = 44100 #number of frames per second/frameset.
CHUNK = 8192
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 44100
SAMPLE_LENGTH = 5
# open audio device
py_audio = pyaudio.PyAudio()
# generate audio
synthesizer = Synthesizer(osc1_waveform=Waveform.square, osc1_volume=1.0, use_osc2=False, rate=BITRATE)
out_wave = synthesizer.generate_constant_wave(440.0, SAMPLE_LENGTH)
def stream_callback(wave, frame):
audio_data = (wave * float(2 ** 15 - 1)).astype(np.int16).tobytes()
index = 0
def callback(in_data, frame_count, time_info, status):
nonlocal audio_data
nonlocal index
nonlocal frame
print("got data!")
frame.append(in_data)
data_length = frame_count * CHANNELS * 2
data = audio_data[index:(index+data_length)]
index += data_length
return (data, pyaudio.paContinue)
return callback
frame_data = []
stream_options = {
"format": FORMAT,
"channels": CHANNELS,
"rate": BITRATE,
"output_device_index": 7,
"input_device_index": 2,
"output": True,
"input": True,
"stream_callback": stream_callback(out_wave, frame_data)
}
# Open Streams
stream_out = py_audio.open(**stream_options)
stream_out.start_stream()
# wait for stream to finish (5)
while stream_out.is_active():
time.sleep(0.1)
print("Done!")
stream_out.stop_stream()
stream_out.close()
py_audio.terminate()
,但是在这种情况下,它会取消映射文件,但不会刷新RAM。我有代码,基本上可以做到这一点:
madvise
map_addr=mmap( start, size,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
file_no,
file_offset);
modified_list = modify_data(map_addr, ...);
for (mem in modified_list) {
printf("vma:%p, pagemap:%"PRIu64"\n",
mem.ptr, getpagemap(addr));
madvise(mem.ptr, mem.size, MADV_DONTNEED);
printf(" after: pagemap:%"PRIx64"\n",
getpagemap(addr));
}
// sleep for a few seconds
mincore(modified_list[0].ptr,modified_list[0].size,vec);
显示在调用mincore
之后页面仍在系统中。 printf显示:在madvise之前,页面映射既显示当前位,又显示独占位集(madvise
),但是在调用madvise之后,页面映射变为vma:0xf6bb7000, pagemap:a10000000001dd07
。这意味着该页面不再存在(未映射);但是由于某种原因,页面似乎卡在了RAM中。 (注意:对该文件的0
调用也显示该文件未映射到其他pid)。此后,它在RAM中保留了很长时间,这对我来说是个问题。
有没有办法强制刷新RAM,或者至少知道为什么不刷新RAM?